#include <bits/stdc++.h>
#define FOR(i,p,k) for(int i=(p);i<=(k);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) (int(x.size()))
#define all(x) (x).begin(),(x).end()
using namespace std;
int main(){
int n;
scanf("%d", &n);
vector<vector<int>> g(n+1);
REP(i, n-1){
int a, b;
scanf("%d%d", &a, &b);
g[a].emplace_back(b);
g[b].emplace_back(a);
}
vector<int> czy(n+1);
scanf("\n");
FOR(i, 1, n){
char c;
scanf("%c", &c);
czy[i] = c=='1';
}
vector<int> oj(n+1);
vector<int> dp = czy;
function<void(int, int)> dfs = [&](int w, int o){
int t = -czy[w];
for(int i : g[w]) if(i != o) oj[i] = w, dfs(i, w), t += dp[i];
dp[w] = max(dp[w], t);
};
dfs(1, 0);
int wyn = dp[1];
FOR(i, 2, n) wyn = max(wyn, dp[i]+czy[oj[i]]);
printf("%d", wyn);
return 0;
}
Compilation message
power.cpp: In function 'int main()':
power.cpp:10:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
10 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
power.cpp:14:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
14 | scanf("%d%d", &a, &b);
| ~~~~~^~~~~~~~~~~~~~~~
power.cpp:19:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | scanf("\n");
| ~~~~~^~~~~~
power.cpp:22:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | scanf("%c", &c);
| ~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
500 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
436 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
500 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
436 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
552 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
1 ms |
444 KB |
Output is correct |
27 |
Correct |
2 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
564 KB |
Output is correct |
33 |
Correct |
1 ms |
548 KB |
Output is correct |
34 |
Correct |
1 ms |
424 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
36 |
Correct |
1 ms |
344 KB |
Output is correct |
37 |
Correct |
1 ms |
600 KB |
Output is correct |
38 |
Correct |
2 ms |
344 KB |
Output is correct |
39 |
Correct |
1 ms |
448 KB |
Output is correct |
40 |
Correct |
1 ms |
560 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
604 KB |
Output is correct |
43 |
Correct |
1 ms |
604 KB |
Output is correct |
44 |
Correct |
1 ms |
604 KB |
Output is correct |
45 |
Correct |
1 ms |
604 KB |
Output is correct |
46 |
Correct |
1 ms |
604 KB |
Output is correct |
47 |
Correct |
1 ms |
604 KB |
Output is correct |
48 |
Correct |
1 ms |
604 KB |
Output is correct |
49 |
Correct |
1 ms |
604 KB |
Output is correct |
50 |
Correct |
1 ms |
604 KB |
Output is correct |
51 |
Correct |
1 ms |
604 KB |
Output is correct |
52 |
Correct |
1 ms |
448 KB |
Output is correct |
53 |
Correct |
1 ms |
604 KB |
Output is correct |
54 |
Correct |
1 ms |
604 KB |
Output is correct |
55 |
Correct |
1 ms |
604 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
604 KB |
Output is correct |
58 |
Correct |
1 ms |
604 KB |
Output is correct |
59 |
Correct |
1 ms |
604 KB |
Output is correct |
60 |
Correct |
1 ms |
676 KB |
Output is correct |
61 |
Correct |
1 ms |
604 KB |
Output is correct |
62 |
Correct |
1 ms |
604 KB |
Output is correct |
63 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
500 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
436 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
552 KB |
Output is correct |
25 |
Correct |
1 ms |
600 KB |
Output is correct |
26 |
Correct |
1 ms |
444 KB |
Output is correct |
27 |
Correct |
2 ms |
348 KB |
Output is correct |
28 |
Correct |
1 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
564 KB |
Output is correct |
33 |
Correct |
1 ms |
548 KB |
Output is correct |
34 |
Correct |
1 ms |
424 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
36 |
Correct |
1 ms |
344 KB |
Output is correct |
37 |
Correct |
1 ms |
600 KB |
Output is correct |
38 |
Correct |
2 ms |
344 KB |
Output is correct |
39 |
Correct |
1 ms |
448 KB |
Output is correct |
40 |
Correct |
1 ms |
560 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
604 KB |
Output is correct |
43 |
Correct |
1 ms |
604 KB |
Output is correct |
44 |
Correct |
1 ms |
604 KB |
Output is correct |
45 |
Correct |
1 ms |
604 KB |
Output is correct |
46 |
Correct |
1 ms |
604 KB |
Output is correct |
47 |
Correct |
1 ms |
604 KB |
Output is correct |
48 |
Correct |
1 ms |
604 KB |
Output is correct |
49 |
Correct |
1 ms |
604 KB |
Output is correct |
50 |
Correct |
1 ms |
604 KB |
Output is correct |
51 |
Correct |
1 ms |
604 KB |
Output is correct |
52 |
Correct |
1 ms |
448 KB |
Output is correct |
53 |
Correct |
1 ms |
604 KB |
Output is correct |
54 |
Correct |
1 ms |
604 KB |
Output is correct |
55 |
Correct |
1 ms |
604 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
1 ms |
604 KB |
Output is correct |
58 |
Correct |
1 ms |
604 KB |
Output is correct |
59 |
Correct |
1 ms |
604 KB |
Output is correct |
60 |
Correct |
1 ms |
676 KB |
Output is correct |
61 |
Correct |
1 ms |
604 KB |
Output is correct |
62 |
Correct |
1 ms |
604 KB |
Output is correct |
63 |
Correct |
1 ms |
348 KB |
Output is correct |
64 |
Correct |
125 ms |
16692 KB |
Output is correct |
65 |
Correct |
90 ms |
16588 KB |
Output is correct |
66 |
Correct |
102 ms |
16748 KB |
Output is correct |
67 |
Correct |
88 ms |
16540 KB |
Output is correct |
68 |
Correct |
94 ms |
16468 KB |
Output is correct |
69 |
Correct |
90 ms |
16476 KB |
Output is correct |
70 |
Correct |
100 ms |
16360 KB |
Output is correct |
71 |
Correct |
93 ms |
16516 KB |
Output is correct |
72 |
Correct |
76 ms |
16536 KB |
Output is correct |
73 |
Correct |
116 ms |
34232 KB |
Output is correct |
74 |
Correct |
110 ms |
34132 KB |
Output is correct |
75 |
Correct |
75 ms |
16464 KB |
Output is correct |
76 |
Correct |
85 ms |
16212 KB |
Output is correct |
77 |
Correct |
78 ms |
16284 KB |
Output is correct |
78 |
Correct |
125 ms |
16396 KB |
Output is correct |
79 |
Correct |
130 ms |
16360 KB |
Output is correct |
80 |
Correct |
85 ms |
16324 KB |
Output is correct |
81 |
Correct |
69 ms |
16692 KB |
Output is correct |
82 |
Correct |
70 ms |
16588 KB |
Output is correct |
83 |
Correct |
125 ms |
23752 KB |
Output is correct |
84 |
Correct |
99 ms |
26708 KB |
Output is correct |
85 |
Correct |
119 ms |
26196 KB |
Output is correct |
86 |
Correct |
81 ms |
18260 KB |
Output is correct |
87 |
Correct |
84 ms |
17488 KB |
Output is correct |
88 |
Correct |
99 ms |
17968 KB |
Output is correct |
89 |
Correct |
125 ms |
18084 KB |
Output is correct |
90 |
Correct |
80 ms |
17744 KB |
Output is correct |
91 |
Correct |
74 ms |
18004 KB |
Output is correct |
92 |
Correct |
93 ms |
18372 KB |
Output is correct |
93 |
Correct |
81 ms |
22168 KB |
Output is correct |
94 |
Correct |
90 ms |
24356 KB |
Output is correct |
95 |
Correct |
114 ms |
22468 KB |
Output is correct |
96 |
Correct |
116 ms |
20056 KB |
Output is correct |
97 |
Correct |
79 ms |
17744 KB |
Output is correct |
98 |
Correct |
73 ms |
17172 KB |
Output is correct |
99 |
Correct |
76 ms |
16724 KB |
Output is correct |
100 |
Correct |
89 ms |
21332 KB |
Output is correct |
101 |
Correct |
104 ms |
18596 KB |
Output is correct |
102 |
Correct |
76 ms |
17224 KB |
Output is correct |
103 |
Correct |
114 ms |
16976 KB |
Output is correct |
104 |
Correct |
66 ms |
17352 KB |
Output is correct |
105 |
Correct |
125 ms |
29780 KB |
Output is correct |
106 |
Correct |
55 ms |
16948 KB |
Output is correct |
107 |
Correct |
56 ms |
17096 KB |
Output is correct |
108 |
Correct |
135 ms |
35136 KB |
Output is correct |
109 |
Correct |
65 ms |
17152 KB |
Output is correct |
110 |
Correct |
145 ms |
35852 KB |
Output is correct |
111 |
Correct |
74 ms |
16212 KB |
Output is correct |