#include <bits/stdc++.h>
using namespace std;
const int sz = 1e6 + 5;
#define int long long
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
struct DSU{
vector<int>e;
DSU(int n){
e.assign(n + 1, -1);
}
int _find(int v){
if(e[v] < 0){
return v;
}
return e[v] = _find(e[v]);
}
int _union(int u,int v){
u = _find(u);
v = _find(v);
if(u != v){
if(e[u] > e[v]){
swap(u, v);
}
e[u] += e[v];
e[v] = u;
return 1;
}
return 0;
}
};
string s;
int n, res;
vector<vector<int>>v(sz);
vector<int>dp(sz);
void dfs(int u,int p){
int sum = 0;
int mx = 0;
for(auto i : v[u]){
if(i == p){
continue;
}
dfs(i, u);
sum += dp[i];
mx = max(mx, dp[i]);
}
dp[u] = max((int)(s[u] == '1'), sum - (s[u] == '1'));
res = max(res, sum - (int)(s[u] == '1'));
res = max(res, mx + (int)(s[u] == '1'));
}
void test_case(int testcase){
cin >> n;
for(int i = 0; i < n - 1; ++i){
int a, b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
cin >> s;
s = '#' + s;
dfs(1, -1);
cout << res << endl;
}
signed main(){
int T = 1;
for(int test = 1; test <= T; ++test){
test_case(test);
}
}
/*
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
31580 KB |
Output is correct |
2 |
Correct |
13 ms |
31580 KB |
Output is correct |
3 |
Correct |
15 ms |
31580 KB |
Output is correct |
4 |
Correct |
14 ms |
31764 KB |
Output is correct |
5 |
Correct |
13 ms |
31580 KB |
Output is correct |
6 |
Correct |
13 ms |
31528 KB |
Output is correct |
7 |
Correct |
14 ms |
31576 KB |
Output is correct |
8 |
Correct |
15 ms |
31580 KB |
Output is correct |
9 |
Correct |
15 ms |
31580 KB |
Output is correct |
10 |
Correct |
13 ms |
31516 KB |
Output is correct |
11 |
Correct |
17 ms |
31580 KB |
Output is correct |
12 |
Correct |
16 ms |
31580 KB |
Output is correct |
13 |
Correct |
14 ms |
31580 KB |
Output is correct |
14 |
Correct |
14 ms |
31548 KB |
Output is correct |
15 |
Correct |
14 ms |
31580 KB |
Output is correct |
16 |
Correct |
15 ms |
31580 KB |
Output is correct |
17 |
Correct |
14 ms |
31636 KB |
Output is correct |
18 |
Correct |
15 ms |
31580 KB |
Output is correct |
19 |
Correct |
14 ms |
31636 KB |
Output is correct |
20 |
Correct |
14 ms |
31580 KB |
Output is correct |
21 |
Correct |
13 ms |
31576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
31580 KB |
Output is correct |
2 |
Correct |
13 ms |
31580 KB |
Output is correct |
3 |
Correct |
15 ms |
31580 KB |
Output is correct |
4 |
Correct |
14 ms |
31764 KB |
Output is correct |
5 |
Correct |
13 ms |
31580 KB |
Output is correct |
6 |
Correct |
13 ms |
31528 KB |
Output is correct |
7 |
Correct |
14 ms |
31576 KB |
Output is correct |
8 |
Correct |
15 ms |
31580 KB |
Output is correct |
9 |
Correct |
15 ms |
31580 KB |
Output is correct |
10 |
Correct |
13 ms |
31516 KB |
Output is correct |
11 |
Correct |
17 ms |
31580 KB |
Output is correct |
12 |
Correct |
16 ms |
31580 KB |
Output is correct |
13 |
Correct |
14 ms |
31580 KB |
Output is correct |
14 |
Correct |
14 ms |
31548 KB |
Output is correct |
15 |
Correct |
14 ms |
31580 KB |
Output is correct |
16 |
Correct |
15 ms |
31580 KB |
Output is correct |
17 |
Correct |
14 ms |
31636 KB |
Output is correct |
18 |
Correct |
15 ms |
31580 KB |
Output is correct |
19 |
Correct |
14 ms |
31636 KB |
Output is correct |
20 |
Correct |
14 ms |
31580 KB |
Output is correct |
21 |
Correct |
13 ms |
31576 KB |
Output is correct |
22 |
Correct |
14 ms |
31836 KB |
Output is correct |
23 |
Correct |
15 ms |
31836 KB |
Output is correct |
24 |
Correct |
19 ms |
31832 KB |
Output is correct |
25 |
Correct |
16 ms |
31836 KB |
Output is correct |
26 |
Correct |
16 ms |
31836 KB |
Output is correct |
27 |
Correct |
15 ms |
31832 KB |
Output is correct |
28 |
Correct |
14 ms |
31616 KB |
Output is correct |
29 |
Correct |
15 ms |
31836 KB |
Output is correct |
30 |
Correct |
15 ms |
31772 KB |
Output is correct |
31 |
Correct |
15 ms |
31860 KB |
Output is correct |
32 |
Correct |
20 ms |
31692 KB |
Output is correct |
33 |
Correct |
16 ms |
31832 KB |
Output is correct |
34 |
Correct |
16 ms |
31836 KB |
Output is correct |
35 |
Correct |
14 ms |
31836 KB |
Output is correct |
36 |
Correct |
13 ms |
31836 KB |
Output is correct |
37 |
Correct |
13 ms |
31836 KB |
Output is correct |
38 |
Correct |
15 ms |
31840 KB |
Output is correct |
39 |
Correct |
15 ms |
31836 KB |
Output is correct |
40 |
Correct |
16 ms |
31836 KB |
Output is correct |
41 |
Correct |
16 ms |
31776 KB |
Output is correct |
42 |
Correct |
17 ms |
31836 KB |
Output is correct |
43 |
Correct |
16 ms |
31868 KB |
Output is correct |
44 |
Correct |
15 ms |
31836 KB |
Output is correct |
45 |
Correct |
15 ms |
31644 KB |
Output is correct |
46 |
Correct |
16 ms |
31832 KB |
Output is correct |
47 |
Correct |
19 ms |
31832 KB |
Output is correct |
48 |
Correct |
16 ms |
31844 KB |
Output is correct |
49 |
Correct |
16 ms |
31836 KB |
Output is correct |
50 |
Correct |
16 ms |
31836 KB |
Output is correct |
51 |
Correct |
16 ms |
32088 KB |
Output is correct |
52 |
Correct |
15 ms |
31836 KB |
Output is correct |
53 |
Correct |
15 ms |
31836 KB |
Output is correct |
54 |
Correct |
14 ms |
31832 KB |
Output is correct |
55 |
Correct |
16 ms |
31772 KB |
Output is correct |
56 |
Correct |
14 ms |
31832 KB |
Output is correct |
57 |
Correct |
14 ms |
31836 KB |
Output is correct |
58 |
Correct |
15 ms |
31832 KB |
Output is correct |
59 |
Correct |
14 ms |
31672 KB |
Output is correct |
60 |
Correct |
16 ms |
31768 KB |
Output is correct |
61 |
Correct |
14 ms |
31836 KB |
Output is correct |
62 |
Correct |
17 ms |
31836 KB |
Output is correct |
63 |
Correct |
15 ms |
31620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
31580 KB |
Output is correct |
2 |
Correct |
13 ms |
31580 KB |
Output is correct |
3 |
Correct |
15 ms |
31580 KB |
Output is correct |
4 |
Correct |
14 ms |
31764 KB |
Output is correct |
5 |
Correct |
13 ms |
31580 KB |
Output is correct |
6 |
Correct |
13 ms |
31528 KB |
Output is correct |
7 |
Correct |
14 ms |
31576 KB |
Output is correct |
8 |
Correct |
15 ms |
31580 KB |
Output is correct |
9 |
Correct |
15 ms |
31580 KB |
Output is correct |
10 |
Correct |
13 ms |
31516 KB |
Output is correct |
11 |
Correct |
17 ms |
31580 KB |
Output is correct |
12 |
Correct |
16 ms |
31580 KB |
Output is correct |
13 |
Correct |
14 ms |
31580 KB |
Output is correct |
14 |
Correct |
14 ms |
31548 KB |
Output is correct |
15 |
Correct |
14 ms |
31580 KB |
Output is correct |
16 |
Correct |
15 ms |
31580 KB |
Output is correct |
17 |
Correct |
14 ms |
31636 KB |
Output is correct |
18 |
Correct |
15 ms |
31580 KB |
Output is correct |
19 |
Correct |
14 ms |
31636 KB |
Output is correct |
20 |
Correct |
14 ms |
31580 KB |
Output is correct |
21 |
Correct |
13 ms |
31576 KB |
Output is correct |
22 |
Correct |
14 ms |
31836 KB |
Output is correct |
23 |
Correct |
15 ms |
31836 KB |
Output is correct |
24 |
Correct |
19 ms |
31832 KB |
Output is correct |
25 |
Correct |
16 ms |
31836 KB |
Output is correct |
26 |
Correct |
16 ms |
31836 KB |
Output is correct |
27 |
Correct |
15 ms |
31832 KB |
Output is correct |
28 |
Correct |
14 ms |
31616 KB |
Output is correct |
29 |
Correct |
15 ms |
31836 KB |
Output is correct |
30 |
Correct |
15 ms |
31772 KB |
Output is correct |
31 |
Correct |
15 ms |
31860 KB |
Output is correct |
32 |
Correct |
20 ms |
31692 KB |
Output is correct |
33 |
Correct |
16 ms |
31832 KB |
Output is correct |
34 |
Correct |
16 ms |
31836 KB |
Output is correct |
35 |
Correct |
14 ms |
31836 KB |
Output is correct |
36 |
Correct |
13 ms |
31836 KB |
Output is correct |
37 |
Correct |
13 ms |
31836 KB |
Output is correct |
38 |
Correct |
15 ms |
31840 KB |
Output is correct |
39 |
Correct |
15 ms |
31836 KB |
Output is correct |
40 |
Correct |
16 ms |
31836 KB |
Output is correct |
41 |
Correct |
16 ms |
31776 KB |
Output is correct |
42 |
Correct |
17 ms |
31836 KB |
Output is correct |
43 |
Correct |
16 ms |
31868 KB |
Output is correct |
44 |
Correct |
15 ms |
31836 KB |
Output is correct |
45 |
Correct |
15 ms |
31644 KB |
Output is correct |
46 |
Correct |
16 ms |
31832 KB |
Output is correct |
47 |
Correct |
19 ms |
31832 KB |
Output is correct |
48 |
Correct |
16 ms |
31844 KB |
Output is correct |
49 |
Correct |
16 ms |
31836 KB |
Output is correct |
50 |
Correct |
16 ms |
31836 KB |
Output is correct |
51 |
Correct |
16 ms |
32088 KB |
Output is correct |
52 |
Correct |
15 ms |
31836 KB |
Output is correct |
53 |
Correct |
15 ms |
31836 KB |
Output is correct |
54 |
Correct |
14 ms |
31832 KB |
Output is correct |
55 |
Correct |
16 ms |
31772 KB |
Output is correct |
56 |
Correct |
14 ms |
31832 KB |
Output is correct |
57 |
Correct |
14 ms |
31836 KB |
Output is correct |
58 |
Correct |
15 ms |
31832 KB |
Output is correct |
59 |
Correct |
14 ms |
31672 KB |
Output is correct |
60 |
Correct |
16 ms |
31768 KB |
Output is correct |
61 |
Correct |
14 ms |
31836 KB |
Output is correct |
62 |
Correct |
17 ms |
31836 KB |
Output is correct |
63 |
Correct |
15 ms |
31620 KB |
Output is correct |
64 |
Correct |
138 ms |
42212 KB |
Output is correct |
65 |
Correct |
130 ms |
42216 KB |
Output is correct |
66 |
Correct |
138 ms |
42124 KB |
Output is correct |
67 |
Correct |
146 ms |
42216 KB |
Output is correct |
68 |
Correct |
133 ms |
42208 KB |
Output is correct |
69 |
Correct |
125 ms |
42208 KB |
Output is correct |
70 |
Correct |
132 ms |
42208 KB |
Output is correct |
71 |
Correct |
162 ms |
42136 KB |
Output is correct |
72 |
Correct |
135 ms |
42196 KB |
Output is correct |
73 |
Correct |
157 ms |
53800 KB |
Output is correct |
74 |
Correct |
142 ms |
53624 KB |
Output is correct |
75 |
Correct |
136 ms |
43556 KB |
Output is correct |
76 |
Correct |
141 ms |
43448 KB |
Output is correct |
77 |
Correct |
144 ms |
42116 KB |
Output is correct |
78 |
Correct |
129 ms |
42208 KB |
Output is correct |
79 |
Correct |
160 ms |
42208 KB |
Output is correct |
80 |
Correct |
132 ms |
42204 KB |
Output is correct |
81 |
Correct |
122 ms |
42184 KB |
Output is correct |
82 |
Correct |
133 ms |
41936 KB |
Output is correct |
83 |
Correct |
140 ms |
47140 KB |
Output is correct |
84 |
Correct |
156 ms |
49184 KB |
Output is correct |
85 |
Correct |
133 ms |
48924 KB |
Output is correct |
86 |
Correct |
123 ms |
43296 KB |
Output is correct |
87 |
Correct |
132 ms |
42784 KB |
Output is correct |
88 |
Correct |
136 ms |
42768 KB |
Output is correct |
89 |
Correct |
146 ms |
42780 KB |
Output is correct |
90 |
Correct |
135 ms |
42684 KB |
Output is correct |
91 |
Correct |
131 ms |
43012 KB |
Output is correct |
92 |
Correct |
184 ms |
43256 KB |
Output is correct |
93 |
Correct |
176 ms |
45484 KB |
Output is correct |
94 |
Correct |
140 ms |
47044 KB |
Output is correct |
95 |
Correct |
149 ms |
45976 KB |
Output is correct |
96 |
Correct |
167 ms |
43988 KB |
Output is correct |
97 |
Correct |
117 ms |
42532 KB |
Output is correct |
98 |
Correct |
139 ms |
42276 KB |
Output is correct |
99 |
Correct |
122 ms |
42324 KB |
Output is correct |
100 |
Correct |
134 ms |
45088 KB |
Output is correct |
101 |
Correct |
128 ms |
42964 KB |
Output is correct |
102 |
Correct |
135 ms |
42412 KB |
Output is correct |
103 |
Correct |
129 ms |
42468 KB |
Output is correct |
104 |
Correct |
149 ms |
42692 KB |
Output is correct |
105 |
Correct |
129 ms |
50440 KB |
Output is correct |
106 |
Correct |
113 ms |
42436 KB |
Output is correct |
107 |
Correct |
117 ms |
42504 KB |
Output is correct |
108 |
Correct |
159 ms |
54500 KB |
Output is correct |
109 |
Correct |
121 ms |
42624 KB |
Output is correct |
110 |
Correct |
147 ms |
54812 KB |
Output is correct |
111 |
Correct |
128 ms |
42164 KB |
Output is correct |