没有上司的舞会——树形DP
没有上司的舞会
https://ac.nowcoder.com/acm/problem/51178
题意
最大点权独立集
思路
树形dp维护每个点取or 不取的状态
WA的点
转移方程错误:
u不取,子结点可以取或者不取,导致wa了2发。
题目链接
//#pragma GCC optimize(2) //#pragma GCC target ("sse4") #include<bits/stdc++.h> //typedef long long ll; #define ull unsigned long long #define int long long #define F first #define S second #define endl "\n"//<<flush #define eps 1e-6 #define base 131 #define lowbit(x) (x&(-x)) #define db double #define PI acos(-1.0) #define inf 0x3f3f3f3f #define MAXN 0x7fffffff #define INF 0x3f3f3f3f3f3f3f3f #define ferma(a,b) pow(a,b-2) #define mod(x) (x%mod+mod)%mod #define pb push_back #define decimal(x) cout << fixed << setprecision(x); #define all(x) x.begin(),x.end() #define rall(x) x.rbegin(),x.rend() #define memset(a,b) memset(a,b,sizeof(a)); #define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0); using namespace std; #ifndef ONLINE_JUDGE #include "local.h" #endif template<typename T> inline T fetch(){T ret;cin >> ret;return ret;} template<typename T> inline vector<T> fetch_vec(int sz){vector<T> ret(sz);for(auto& it: ret)cin >> it;return ret;} template<typename T> inline void makeUnique(vector<T>& v){sort(v.begin(), v.end());v.erase(unique(v.begin(), v.end()), v.end());} void file() { #ifdef ONLINE_JUDGE #else freopen("D:/LSNU/codeforces/duipai/data.txt","r",stdin); // freopen("D:/LSNU/codeforces/duipai/WA.txt","w",stdout); #endif } const int N=6e3+5; struct node { int v,next; }s[N<<1]; int head[N],cnt=0,h[N],dp[N][2]; void add(int u,int v) { s[++cnt].next=head[u]; s[cnt].v=v; head[u]=cnt; } void dfs(int u,int fat) { dp[u][1]=h[u]; for(int i=head[u];i;i=s[i].next) { int v=s[i].v; if(v==fat) continue; dfs(v,u); dp[u][0]+=max(dp[v][1],dp[v][0]); dp[u][1]+=dp[v][0]; } } signed main() { IOS; file(); int n; cin>>n; for(int i=1;i<=n;i++) cin>>h[i]; for(int i=0;i<n;i++) { int u,v; cin>>u>>v; if(u) add(u,v),add(v,u); } dfs(1,0); cout<<max(dp[1][0],dp[1][1])<<endl; return 0; }