标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 6401 Accepted Submission(s): 2862
#include <cstdio> #include <iostream> #include <sstream> #include <cmath> #include <cstring> #include <cstdlib> #include <string> #include <vector> #include <map> #include <set> #include <queue> #include <stack> #include <algorithm> using namespace std; #define ll long long #define _cle(m, a) memset(m, a, sizeof(m)) #define repu(i, a, b) for(int i = a; i < b; i++) #define repd(i, a, b) for(int i = b; i >= a; i--) #define sfi(n) scanf("%d", &n) #define sfl(n) scanf("%lld", &n) #define pfi(n) printf("%d\n", n) #define pfl(n) printf("%lld\n", n) #define MAXN 6005 int w[MAXN]; bool f[MAXN] = {false}; vector<int> v[MAXN]; int d[MAXN][2]; bool vis[MAXN] = {false}; void dp(int root) { if(vis[root]) return ; vis[root] = true; int siz = v[root].size(); repu(i, 0, siz) dp(v[root][i]); repu(i, 0, siz) { d[root][0] += max(d[v[root][i]][1], d[v[root][i]][0]); d[root][1] += d[v[root][i]][0]; } return ; } int main() { int n; while(~sfi(n)) { _cle(vis, false); _cle(f, false); repu(i, 1, n + 1) { sfi(w[i]); v[i].clear(); d[i][0] = 0; d[i][1] = w[i]; } int x, y; while(sfi(x), sfi(y), x + y) { v[y].push_back(x); f[x] = true; } int root; repu(i, 1, n + 1) if(!f[i]) { root = i; break; } dp(root); pfi(max(d[root][0], d[root][1])); //repu(i, 1, n + 1) printf("%d %d\n", d[i][0], d[i][1]); } return 0; }
标签:
原文地址:http://www.cnblogs.com/sunus/p/4681051.html