#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
#define foru(i, l, r) for(int i=(l); i<=(r); ++i)
#define ford(i, l, r) for(int i=(l); i>=(r); --i)
#define fore(x, v) for(auto &x : v)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int)x.size()
void setIO() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if (fopen("input.inp", "r")) {
freopen("input.inp", "r", stdin);
freopen("input.out", "w", stdout);
}
}
const int N = 2e5+5;
int n, m, e[N];
string s;
vector<int> tree[N];
int ans, dp[N];
void dfs(int u, int p) {
dp[u] = e[u];
int sum = 0, ma = 0;
fore(v, tree[u]) if (v != p) {
dfs(v, u);
sum += dp[v];
ma = max(ma, dp[v]);
}
dp[u] = max(dp[u], sum - e[u]);
ans = max(ans, max(sum - e[u], ma + e[u]));
}
int main() {
setIO();
cin >> n;
foru(i, 2, n) {
int u, v;
cin >> u >> v;
tree[u].push_back(v);
tree[v].push_back(u);
}
cin >> s;
foru(i, 1, n) e[i] = (s[i-1] == '1');
dfs(1, 0);
cout << ans;
}
Compilation message
power.cpp: In function 'void setIO()':
power.cpp:16:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | freopen("input.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
power.cpp:17:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | freopen("input.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
5724 KB |
Output is correct |
2 |
Correct |
1 ms |
5724 KB |
Output is correct |
3 |
Correct |
1 ms |
5752 KB |
Output is correct |
4 |
Correct |
2 ms |
5724 KB |
Output is correct |
5 |
Correct |
1 ms |
5724 KB |
Output is correct |
6 |
Correct |
1 ms |
5724 KB |
Output is correct |
7 |
Correct |
3 ms |
5724 KB |
Output is correct |
8 |
Correct |
2 ms |
5720 KB |
Output is correct |
9 |
Correct |
1 ms |
5720 KB |
Output is correct |
10 |
Correct |
1 ms |
6032 KB |
Output is correct |
11 |
Correct |
1 ms |
5720 KB |
Output is correct |
12 |
Correct |
1 ms |
5876 KB |
Output is correct |
13 |
Correct |
1 ms |
5724 KB |
Output is correct |
14 |
Correct |
1 ms |
5976 KB |
Output is correct |
15 |
Correct |
1 ms |
5724 KB |
Output is correct |
16 |
Correct |
1 ms |
5724 KB |
Output is correct |
17 |
Correct |
2 ms |
5724 KB |
Output is correct |
18 |
Correct |
1 ms |
5724 KB |
Output is correct |
19 |
Correct |
1 ms |
5724 KB |
Output is correct |
20 |
Correct |
1 ms |
5724 KB |
Output is correct |
21 |
Correct |
1 ms |
5724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
5724 KB |
Output is correct |
2 |
Correct |
1 ms |
5724 KB |
Output is correct |
3 |
Correct |
1 ms |
5752 KB |
Output is correct |
4 |
Correct |
2 ms |
5724 KB |
Output is correct |
5 |
Correct |
1 ms |
5724 KB |
Output is correct |
6 |
Correct |
1 ms |
5724 KB |
Output is correct |
7 |
Correct |
3 ms |
5724 KB |
Output is correct |
8 |
Correct |
2 ms |
5720 KB |
Output is correct |
9 |
Correct |
1 ms |
5720 KB |
Output is correct |
10 |
Correct |
1 ms |
6032 KB |
Output is correct |
11 |
Correct |
1 ms |
5720 KB |
Output is correct |
12 |
Correct |
1 ms |
5876 KB |
Output is correct |
13 |
Correct |
1 ms |
5724 KB |
Output is correct |
14 |
Correct |
1 ms |
5976 KB |
Output is correct |
15 |
Correct |
1 ms |
5724 KB |
Output is correct |
16 |
Correct |
1 ms |
5724 KB |
Output is correct |
17 |
Correct |
2 ms |
5724 KB |
Output is correct |
18 |
Correct |
1 ms |
5724 KB |
Output is correct |
19 |
Correct |
1 ms |
5724 KB |
Output is correct |
20 |
Correct |
1 ms |
5724 KB |
Output is correct |
21 |
Correct |
1 ms |
5724 KB |
Output is correct |
22 |
Correct |
3 ms |
5720 KB |
Output is correct |
23 |
Correct |
2 ms |
5720 KB |
Output is correct |
24 |
Correct |
2 ms |
5720 KB |
Output is correct |
25 |
Correct |
3 ms |
5980 KB |
Output is correct |
26 |
Correct |
2 ms |
5724 KB |
Output is correct |
27 |
Correct |
2 ms |
5812 KB |
Output is correct |
28 |
Correct |
2 ms |
5724 KB |
Output is correct |
29 |
Correct |
2 ms |
5784 KB |
Output is correct |
30 |
Correct |
2 ms |
5724 KB |
Output is correct |
31 |
Correct |
2 ms |
5724 KB |
Output is correct |
32 |
Correct |
2 ms |
5724 KB |
Output is correct |
33 |
Correct |
2 ms |
5720 KB |
Output is correct |
34 |
Correct |
2 ms |
5724 KB |
Output is correct |
35 |
Correct |
2 ms |
5724 KB |
Output is correct |
36 |
Correct |
2 ms |
5724 KB |
Output is correct |
37 |
Correct |
2 ms |
5724 KB |
Output is correct |
38 |
Correct |
2 ms |
5724 KB |
Output is correct |
39 |
Correct |
2 ms |
5780 KB |
Output is correct |
40 |
Correct |
2 ms |
5980 KB |
Output is correct |
41 |
Correct |
2 ms |
5720 KB |
Output is correct |
42 |
Correct |
2 ms |
5780 KB |
Output is correct |
43 |
Correct |
2 ms |
5724 KB |
Output is correct |
44 |
Correct |
2 ms |
5724 KB |
Output is correct |
45 |
Correct |
2 ms |
5724 KB |
Output is correct |
46 |
Correct |
2 ms |
5788 KB |
Output is correct |
47 |
Correct |
2 ms |
5724 KB |
Output is correct |
48 |
Correct |
2 ms |
5824 KB |
Output is correct |
49 |
Correct |
2 ms |
5860 KB |
Output is correct |
50 |
Correct |
2 ms |
5724 KB |
Output is correct |
51 |
Correct |
2 ms |
5724 KB |
Output is correct |
52 |
Correct |
2 ms |
5724 KB |
Output is correct |
53 |
Correct |
2 ms |
5724 KB |
Output is correct |
54 |
Correct |
2 ms |
5724 KB |
Output is correct |
55 |
Correct |
2 ms |
5724 KB |
Output is correct |
56 |
Correct |
2 ms |
5724 KB |
Output is correct |
57 |
Correct |
3 ms |
5984 KB |
Output is correct |
58 |
Correct |
2 ms |
5720 KB |
Output is correct |
59 |
Correct |
2 ms |
5724 KB |
Output is correct |
60 |
Correct |
2 ms |
5724 KB |
Output is correct |
61 |
Correct |
2 ms |
5724 KB |
Output is correct |
62 |
Correct |
2 ms |
5788 KB |
Output is correct |
63 |
Correct |
2 ms |
5724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
5724 KB |
Output is correct |
2 |
Correct |
1 ms |
5724 KB |
Output is correct |
3 |
Correct |
1 ms |
5752 KB |
Output is correct |
4 |
Correct |
2 ms |
5724 KB |
Output is correct |
5 |
Correct |
1 ms |
5724 KB |
Output is correct |
6 |
Correct |
1 ms |
5724 KB |
Output is correct |
7 |
Correct |
3 ms |
5724 KB |
Output is correct |
8 |
Correct |
2 ms |
5720 KB |
Output is correct |
9 |
Correct |
1 ms |
5720 KB |
Output is correct |
10 |
Correct |
1 ms |
6032 KB |
Output is correct |
11 |
Correct |
1 ms |
5720 KB |
Output is correct |
12 |
Correct |
1 ms |
5876 KB |
Output is correct |
13 |
Correct |
1 ms |
5724 KB |
Output is correct |
14 |
Correct |
1 ms |
5976 KB |
Output is correct |
15 |
Correct |
1 ms |
5724 KB |
Output is correct |
16 |
Correct |
1 ms |
5724 KB |
Output is correct |
17 |
Correct |
2 ms |
5724 KB |
Output is correct |
18 |
Correct |
1 ms |
5724 KB |
Output is correct |
19 |
Correct |
1 ms |
5724 KB |
Output is correct |
20 |
Correct |
1 ms |
5724 KB |
Output is correct |
21 |
Correct |
1 ms |
5724 KB |
Output is correct |
22 |
Correct |
3 ms |
5720 KB |
Output is correct |
23 |
Correct |
2 ms |
5720 KB |
Output is correct |
24 |
Correct |
2 ms |
5720 KB |
Output is correct |
25 |
Correct |
3 ms |
5980 KB |
Output is correct |
26 |
Correct |
2 ms |
5724 KB |
Output is correct |
27 |
Correct |
2 ms |
5812 KB |
Output is correct |
28 |
Correct |
2 ms |
5724 KB |
Output is correct |
29 |
Correct |
2 ms |
5784 KB |
Output is correct |
30 |
Correct |
2 ms |
5724 KB |
Output is correct |
31 |
Correct |
2 ms |
5724 KB |
Output is correct |
32 |
Correct |
2 ms |
5724 KB |
Output is correct |
33 |
Correct |
2 ms |
5720 KB |
Output is correct |
34 |
Correct |
2 ms |
5724 KB |
Output is correct |
35 |
Correct |
2 ms |
5724 KB |
Output is correct |
36 |
Correct |
2 ms |
5724 KB |
Output is correct |
37 |
Correct |
2 ms |
5724 KB |
Output is correct |
38 |
Correct |
2 ms |
5724 KB |
Output is correct |
39 |
Correct |
2 ms |
5780 KB |
Output is correct |
40 |
Correct |
2 ms |
5980 KB |
Output is correct |
41 |
Correct |
2 ms |
5720 KB |
Output is correct |
42 |
Correct |
2 ms |
5780 KB |
Output is correct |
43 |
Correct |
2 ms |
5724 KB |
Output is correct |
44 |
Correct |
2 ms |
5724 KB |
Output is correct |
45 |
Correct |
2 ms |
5724 KB |
Output is correct |
46 |
Correct |
2 ms |
5788 KB |
Output is correct |
47 |
Correct |
2 ms |
5724 KB |
Output is correct |
48 |
Correct |
2 ms |
5824 KB |
Output is correct |
49 |
Correct |
2 ms |
5860 KB |
Output is correct |
50 |
Correct |
2 ms |
5724 KB |
Output is correct |
51 |
Correct |
2 ms |
5724 KB |
Output is correct |
52 |
Correct |
2 ms |
5724 KB |
Output is correct |
53 |
Correct |
2 ms |
5724 KB |
Output is correct |
54 |
Correct |
2 ms |
5724 KB |
Output is correct |
55 |
Correct |
2 ms |
5724 KB |
Output is correct |
56 |
Correct |
2 ms |
5724 KB |
Output is correct |
57 |
Correct |
3 ms |
5984 KB |
Output is correct |
58 |
Correct |
2 ms |
5720 KB |
Output is correct |
59 |
Correct |
2 ms |
5724 KB |
Output is correct |
60 |
Correct |
2 ms |
5724 KB |
Output is correct |
61 |
Correct |
2 ms |
5724 KB |
Output is correct |
62 |
Correct |
2 ms |
5788 KB |
Output is correct |
63 |
Correct |
2 ms |
5724 KB |
Output is correct |
64 |
Correct |
56 ms |
15508 KB |
Output is correct |
65 |
Correct |
74 ms |
16108 KB |
Output is correct |
66 |
Correct |
61 ms |
16060 KB |
Output is correct |
67 |
Correct |
57 ms |
16016 KB |
Output is correct |
68 |
Correct |
62 ms |
15964 KB |
Output is correct |
69 |
Correct |
71 ms |
16016 KB |
Output is correct |
70 |
Correct |
57 ms |
15976 KB |
Output is correct |
71 |
Correct |
60 ms |
16036 KB |
Output is correct |
72 |
Correct |
56 ms |
16012 KB |
Output is correct |
73 |
Correct |
73 ms |
28560 KB |
Output is correct |
74 |
Correct |
72 ms |
28560 KB |
Output is correct |
75 |
Correct |
57 ms |
16012 KB |
Output is correct |
76 |
Correct |
86 ms |
15768 KB |
Output is correct |
77 |
Correct |
55 ms |
15948 KB |
Output is correct |
78 |
Correct |
65 ms |
16016 KB |
Output is correct |
79 |
Correct |
57 ms |
15772 KB |
Output is correct |
80 |
Correct |
86 ms |
15824 KB |
Output is correct |
81 |
Correct |
53 ms |
16368 KB |
Output is correct |
82 |
Correct |
52 ms |
16208 KB |
Output is correct |
83 |
Correct |
65 ms |
21144 KB |
Output is correct |
84 |
Correct |
67 ms |
23316 KB |
Output is correct |
85 |
Correct |
67 ms |
22924 KB |
Output is correct |
86 |
Correct |
68 ms |
17300 KB |
Output is correct |
87 |
Correct |
59 ms |
16768 KB |
Output is correct |
88 |
Correct |
74 ms |
17160 KB |
Output is correct |
89 |
Correct |
62 ms |
17312 KB |
Output is correct |
90 |
Correct |
71 ms |
17200 KB |
Output is correct |
91 |
Correct |
60 ms |
17372 KB |
Output is correct |
92 |
Correct |
73 ms |
17608 KB |
Output is correct |
93 |
Correct |
64 ms |
20068 KB |
Output is correct |
94 |
Correct |
68 ms |
21800 KB |
Output is correct |
95 |
Correct |
61 ms |
20296 KB |
Output is correct |
96 |
Correct |
77 ms |
18684 KB |
Output is correct |
97 |
Correct |
58 ms |
16936 KB |
Output is correct |
98 |
Correct |
56 ms |
16580 KB |
Output is correct |
99 |
Correct |
57 ms |
16232 KB |
Output is correct |
100 |
Correct |
88 ms |
19580 KB |
Output is correct |
101 |
Correct |
59 ms |
17396 KB |
Output is correct |
102 |
Correct |
57 ms |
16724 KB |
Output is correct |
103 |
Correct |
58 ms |
16664 KB |
Output is correct |
104 |
Correct |
51 ms |
16844 KB |
Output is correct |
105 |
Correct |
80 ms |
25492 KB |
Output is correct |
106 |
Correct |
44 ms |
16836 KB |
Output is correct |
107 |
Correct |
47 ms |
16832 KB |
Output is correct |
108 |
Correct |
92 ms |
29260 KB |
Output is correct |
109 |
Correct |
45 ms |
16836 KB |
Output is correct |
110 |
Correct |
78 ms |
29584 KB |
Output is correct |
111 |
Correct |
54 ms |
15780 KB |
Output is correct |