【树形DP】P1352 没有上司的舞会
作者:互联网
1 #include<iostream> 2 #include<string> 3 #include<queue> 4 #include<stack> 5 #include<vector> 6 #include<map> 7 #include<cstdio> 8 #include<cstdlib> 9 #include<algorithm> 10 #include<set> 11 #include<list> 12 #include<iomanip> 13 #include<cstring> 14 #include<cmath> 15 #include<limits> 16 using namespace std; 17 18 #define au auto 19 #define debug(i) cout<<"<debug> "<<i<<"<\debug>"<<endl 20 #define mfor(i,a,b) for(register int i=(a);i<=(b);i++) 21 #define mrep(i,a,b) for(register int i=(a);i>=(b);i--) 22 #define LLL __int128 23 #define Re register 24 #define il inline 25 #define mem(a,b) memset(a,(b),sizeof(a)) 26 typedef pair<int, int> intpair; 27 typedef long long int LL; 28 const int INF = 0x3f3f3f3f; 29 const long long int INFLL = 0x3f3f3f3f3f3f3f3f; 30 31 int cnt; 32 const int maxn = 6010; 33 int n; 34 int r[maxn]; 35 int f[maxn][2]; 36 bool vis[maxn]; 37 38 struct Edge 39 { 40 int u, nxt; 41 }e[maxn]; 42 43 int head[maxn]; 44 45 void add(int a, int b) 46 { 47 e[++cnt].u = b; 48 e[cnt].nxt = head[a]; 49 head[a] = cnt; 50 } 51 52 void dp(int x) 53 { 54 f[x][0] = 0; 55 f[x][1] = r[x]; 56 for (Re int i = head[x]; i != -1; i = e[i].nxt) 57 { 58 dp(e[i].u); 59 f[x][0] += max(f[e[i].u][0], f[e[i].u][1]); 60 f[x][1] += f[e[i].u][0]; 61 } 62 } 63 64 int main() 65 { 66 mem(head, -1); 67 cin >> n; 68 mfor(i, 1, n) cin >> r[i]; 69 mfor(i, 1, n - 1) 70 { 71 int a, b; 72 cin >> a >> b; 73 add(b, a); 74 vis[a] = true; 75 } 76 int root; 77 mfor(i, 1, n) 78 { 79 if (!vis[i]) 80 { 81 root = i; 82 break; 83 } 84 } 85 dp(root); 86 cout << max(f[root][0], f[root][1]); 87 return 0; 88 }View Code
标签:head,int,cnt,树形,maxn,P1352,include,DP,define 来源: https://www.cnblogs.com/thjkhdf12/p/11728379.html