1 HDU 1520 Anniversary party

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <algorithm>
 6 #include <queue>
 7 #include <vector>
 8 #include <stack>
 9 #include <map>
10 #include <set>
11 #include <cmath>
12 #include <cctype>
13 #include <ctime>
14 #include <bitset>
15 
16 using namespace std;
17 
18 #define REP(i, n) for (int i = 0; i < (n); ++i)
19 #define eps 1e-9
20 
21 typedef long long ll;
22 typedef pair<int, int> pii;
23 
24 const int INF = 0x7fffffff;
25 const int maxn = 1e5;
26 int n, edge;
27 int head[maxn], to[maxn], Next[maxn], dp[2][maxn], in[maxn];
28 
29 inline void init() { edge = 0; memset(head, -1, sizeof(head)); }
30 inline void add_edge(int u, int v) {
31     to[edge] = v; Next[edge] = head[u]; head[u] = edge++;
32 }
33 void dfs(int u, int fa) {
34     for (int i = head[u]; i != -1; i = Next[i]) {
35         if (to[i] == fa) { continue; } dfs(to[i], u);
36         dp[0][u] += max(dp[0][to[i]], dp[1][to[i]]);
37         dp[1][u] += dp[0][to[i]];
38     }
39 }
40 
41 int main() {
42 #ifdef __AiR_H
43     freopen("in.txt", "r", stdin);
44 //    freopen("out.txt", "w", stdout);
45 #endif // __AiR_H
46     while (scanf("%d", &n) != EOF) {
47         init(); int a, b; memset(dp, 0, sizeof(dp));
48         for (int i = 1; i <= n; ++i) { scanf("%d", &dp[1][i]); }
49         while (scanf("%d %d", &a, &b) != EOF) {
50             if (a == 0 && b == 0) { break; }
51             add_edge(a, b); add_edge(b, a); ++in[a];  //b是a的顶头上司
52         }
53         for (int i = 1; i <= n; ++i) {
54             if (in[i]) { continue; }
55             dfs(i, 0); printf("%d\n", max(dp[0][i], dp[1][i])); break;
56         }
57     }
58 #ifdef __AiR_H
59     printf("Time used = %.2fs\n", (double)clock() / CLOCKS_PER_SEC);
60 #endif // __AiR_H
61     return 0;
62 }
View Code