//#pragma GCC optimize("Ofast")
//#pragma GCC optimization("unroll-loops, no-stack-protector")
//#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define Snape int main()
#define lumos ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define nox return 0
#define ll long long
#define pb push_back
#define Size(v) (int)v.size()
#define FOR(i, x, y) for(int i = x; i <= y; ++i)
#define trav(a, x) for(auto &a : x)
using namespace std;
using namespace __gnu_pbds;
/*
"After all this time?"
"Always"
*/
const int nax = 2e5+5;
int n, dp[nax];
vector<int> g[nax];
string s;
int ans=0;
void dfs(int v, int p=0) {
trav(c, g[v]) {
if (c==p) continue;
dfs(c, v);
}
int val = (s[v]=='1');
dp[v]=max(dp[v],val);
int sum=0;
trav(c, g[v]) {
if (c==p) continue;
sum+=dp[c];
}
dp[v]=max(dp[v],sum-val);
ans=max(ans,dp[v]);
trav(c, g[v]) {
if (c==p) continue;
ans=max(ans,dp[c]+val);
}
}
//void calc(int v, int p=0) {
// trav(c, g[v]) {
// if (c==p) continue;
// calc(c, v);
// }
// int val = (s[v]=='1');
// if (val==0) {
// int sum=0;
// trav(c, g[v]) {
// if (c==p) continue;
// sum+=dp[c];
// }
// ans=max(ans,sum);
// }
// else {
// int sum=0;
// trav(c, g[v]) {
// if (c == p) continue;
// ans=max(ans,dp[c]+1);
// sum+=dp[c];
// }
// ans=max(ans,sum-1);
// }
//}
int main() {
lumos;
cin >> n;
FOR (i,1,n-1) {
int a,b; cin>>a>>b;
g[a].pb(b);
g[b].pb(a);
}
cin >> s; s = ' ' + s;
dfs(1);
// calc(1);
cout<<ans;
nox;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5120 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
3 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
3 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
3 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
4 ms |
4992 KB |
Output is correct |
16 |
Correct |
3 ms |
4992 KB |
Output is correct |
17 |
Correct |
3 ms |
4992 KB |
Output is correct |
18 |
Correct |
4 ms |
4992 KB |
Output is correct |
19 |
Correct |
4 ms |
4992 KB |
Output is correct |
20 |
Correct |
3 ms |
4992 KB |
Output is correct |
21 |
Correct |
5 ms |
4992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5120 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
3 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
3 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
3 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
4 ms |
4992 KB |
Output is correct |
16 |
Correct |
3 ms |
4992 KB |
Output is correct |
17 |
Correct |
3 ms |
4992 KB |
Output is correct |
18 |
Correct |
4 ms |
4992 KB |
Output is correct |
19 |
Correct |
4 ms |
4992 KB |
Output is correct |
20 |
Correct |
3 ms |
4992 KB |
Output is correct |
21 |
Correct |
5 ms |
4992 KB |
Output is correct |
22 |
Correct |
4 ms |
5120 KB |
Output is correct |
23 |
Correct |
4 ms |
5120 KB |
Output is correct |
24 |
Correct |
4 ms |
5120 KB |
Output is correct |
25 |
Correct |
4 ms |
5120 KB |
Output is correct |
26 |
Correct |
4 ms |
5248 KB |
Output is correct |
27 |
Correct |
4 ms |
5120 KB |
Output is correct |
28 |
Correct |
4 ms |
5120 KB |
Output is correct |
29 |
Correct |
5 ms |
5248 KB |
Output is correct |
30 |
Correct |
5 ms |
5248 KB |
Output is correct |
31 |
Correct |
5 ms |
5120 KB |
Output is correct |
32 |
Correct |
4 ms |
5120 KB |
Output is correct |
33 |
Correct |
4 ms |
5120 KB |
Output is correct |
34 |
Correct |
4 ms |
5120 KB |
Output is correct |
35 |
Correct |
5 ms |
5120 KB |
Output is correct |
36 |
Correct |
4 ms |
5120 KB |
Output is correct |
37 |
Correct |
4 ms |
5120 KB |
Output is correct |
38 |
Correct |
4 ms |
5120 KB |
Output is correct |
39 |
Correct |
5 ms |
5120 KB |
Output is correct |
40 |
Correct |
5 ms |
5120 KB |
Output is correct |
41 |
Correct |
4 ms |
5248 KB |
Output is correct |
42 |
Correct |
5 ms |
5120 KB |
Output is correct |
43 |
Correct |
4 ms |
5120 KB |
Output is correct |
44 |
Correct |
4 ms |
5120 KB |
Output is correct |
45 |
Correct |
4 ms |
5120 KB |
Output is correct |
46 |
Correct |
4 ms |
5120 KB |
Output is correct |
47 |
Correct |
4 ms |
5120 KB |
Output is correct |
48 |
Correct |
4 ms |
5248 KB |
Output is correct |
49 |
Correct |
4 ms |
5248 KB |
Output is correct |
50 |
Correct |
4 ms |
5120 KB |
Output is correct |
51 |
Correct |
4 ms |
5120 KB |
Output is correct |
52 |
Correct |
4 ms |
5120 KB |
Output is correct |
53 |
Correct |
4 ms |
5120 KB |
Output is correct |
54 |
Correct |
5 ms |
5120 KB |
Output is correct |
55 |
Correct |
4 ms |
5120 KB |
Output is correct |
56 |
Correct |
4 ms |
5120 KB |
Output is correct |
57 |
Correct |
4 ms |
5248 KB |
Output is correct |
58 |
Correct |
4 ms |
5120 KB |
Output is correct |
59 |
Correct |
4 ms |
5120 KB |
Output is correct |
60 |
Correct |
4 ms |
5248 KB |
Output is correct |
61 |
Correct |
4 ms |
5120 KB |
Output is correct |
62 |
Correct |
4 ms |
5248 KB |
Output is correct |
63 |
Correct |
4 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5120 KB |
Output is correct |
2 |
Correct |
3 ms |
4992 KB |
Output is correct |
3 |
Correct |
4 ms |
4992 KB |
Output is correct |
4 |
Correct |
4 ms |
4992 KB |
Output is correct |
5 |
Correct |
3 ms |
4992 KB |
Output is correct |
6 |
Correct |
4 ms |
4992 KB |
Output is correct |
7 |
Correct |
3 ms |
4992 KB |
Output is correct |
8 |
Correct |
4 ms |
4992 KB |
Output is correct |
9 |
Correct |
3 ms |
4992 KB |
Output is correct |
10 |
Correct |
4 ms |
4992 KB |
Output is correct |
11 |
Correct |
4 ms |
5120 KB |
Output is correct |
12 |
Correct |
4 ms |
4992 KB |
Output is correct |
13 |
Correct |
3 ms |
4992 KB |
Output is correct |
14 |
Correct |
4 ms |
4992 KB |
Output is correct |
15 |
Correct |
4 ms |
4992 KB |
Output is correct |
16 |
Correct |
3 ms |
4992 KB |
Output is correct |
17 |
Correct |
3 ms |
4992 KB |
Output is correct |
18 |
Correct |
4 ms |
4992 KB |
Output is correct |
19 |
Correct |
4 ms |
4992 KB |
Output is correct |
20 |
Correct |
3 ms |
4992 KB |
Output is correct |
21 |
Correct |
5 ms |
4992 KB |
Output is correct |
22 |
Correct |
4 ms |
5120 KB |
Output is correct |
23 |
Correct |
4 ms |
5120 KB |
Output is correct |
24 |
Correct |
4 ms |
5120 KB |
Output is correct |
25 |
Correct |
4 ms |
5120 KB |
Output is correct |
26 |
Correct |
4 ms |
5248 KB |
Output is correct |
27 |
Correct |
4 ms |
5120 KB |
Output is correct |
28 |
Correct |
4 ms |
5120 KB |
Output is correct |
29 |
Correct |
5 ms |
5248 KB |
Output is correct |
30 |
Correct |
5 ms |
5248 KB |
Output is correct |
31 |
Correct |
5 ms |
5120 KB |
Output is correct |
32 |
Correct |
4 ms |
5120 KB |
Output is correct |
33 |
Correct |
4 ms |
5120 KB |
Output is correct |
34 |
Correct |
4 ms |
5120 KB |
Output is correct |
35 |
Correct |
5 ms |
5120 KB |
Output is correct |
36 |
Correct |
4 ms |
5120 KB |
Output is correct |
37 |
Correct |
4 ms |
5120 KB |
Output is correct |
38 |
Correct |
4 ms |
5120 KB |
Output is correct |
39 |
Correct |
5 ms |
5120 KB |
Output is correct |
40 |
Correct |
5 ms |
5120 KB |
Output is correct |
41 |
Correct |
4 ms |
5248 KB |
Output is correct |
42 |
Correct |
5 ms |
5120 KB |
Output is correct |
43 |
Correct |
4 ms |
5120 KB |
Output is correct |
44 |
Correct |
4 ms |
5120 KB |
Output is correct |
45 |
Correct |
4 ms |
5120 KB |
Output is correct |
46 |
Correct |
4 ms |
5120 KB |
Output is correct |
47 |
Correct |
4 ms |
5120 KB |
Output is correct |
48 |
Correct |
4 ms |
5248 KB |
Output is correct |
49 |
Correct |
4 ms |
5248 KB |
Output is correct |
50 |
Correct |
4 ms |
5120 KB |
Output is correct |
51 |
Correct |
4 ms |
5120 KB |
Output is correct |
52 |
Correct |
4 ms |
5120 KB |
Output is correct |
53 |
Correct |
4 ms |
5120 KB |
Output is correct |
54 |
Correct |
5 ms |
5120 KB |
Output is correct |
55 |
Correct |
4 ms |
5120 KB |
Output is correct |
56 |
Correct |
4 ms |
5120 KB |
Output is correct |
57 |
Correct |
4 ms |
5248 KB |
Output is correct |
58 |
Correct |
4 ms |
5120 KB |
Output is correct |
59 |
Correct |
4 ms |
5120 KB |
Output is correct |
60 |
Correct |
4 ms |
5248 KB |
Output is correct |
61 |
Correct |
4 ms |
5120 KB |
Output is correct |
62 |
Correct |
4 ms |
5248 KB |
Output is correct |
63 |
Correct |
4 ms |
5120 KB |
Output is correct |
64 |
Correct |
169 ms |
15280 KB |
Output is correct |
65 |
Correct |
170 ms |
15224 KB |
Output is correct |
66 |
Correct |
170 ms |
15200 KB |
Output is correct |
67 |
Correct |
164 ms |
15220 KB |
Output is correct |
68 |
Correct |
171 ms |
15200 KB |
Output is correct |
69 |
Correct |
165 ms |
15216 KB |
Output is correct |
70 |
Correct |
167 ms |
15232 KB |
Output is correct |
71 |
Correct |
169 ms |
15360 KB |
Output is correct |
72 |
Correct |
165 ms |
15216 KB |
Output is correct |
73 |
Correct |
191 ms |
27892 KB |
Output is correct |
74 |
Correct |
193 ms |
27892 KB |
Output is correct |
75 |
Correct |
168 ms |
15220 KB |
Output is correct |
76 |
Correct |
188 ms |
15104 KB |
Output is correct |
77 |
Correct |
174 ms |
15256 KB |
Output is correct |
78 |
Correct |
183 ms |
15092 KB |
Output is correct |
79 |
Correct |
176 ms |
15100 KB |
Output is correct |
80 |
Correct |
179 ms |
15096 KB |
Output is correct |
81 |
Correct |
139 ms |
15600 KB |
Output is correct |
82 |
Correct |
157 ms |
15284 KB |
Output is correct |
83 |
Correct |
186 ms |
20280 KB |
Output is correct |
84 |
Correct |
190 ms |
22532 KB |
Output is correct |
85 |
Correct |
200 ms |
22296 KB |
Output is correct |
86 |
Correct |
170 ms |
16628 KB |
Output is correct |
87 |
Correct |
174 ms |
15984 KB |
Output is correct |
88 |
Correct |
163 ms |
16432 KB |
Output is correct |
89 |
Correct |
166 ms |
16388 KB |
Output is correct |
90 |
Correct |
166 ms |
16308 KB |
Output is correct |
91 |
Correct |
159 ms |
16476 KB |
Output is correct |
92 |
Correct |
162 ms |
16740 KB |
Output is correct |
93 |
Correct |
164 ms |
19152 KB |
Output is correct |
94 |
Correct |
149 ms |
20928 KB |
Output is correct |
95 |
Correct |
158 ms |
19628 KB |
Output is correct |
96 |
Correct |
168 ms |
17760 KB |
Output is correct |
97 |
Correct |
158 ms |
16336 KB |
Output is correct |
98 |
Correct |
154 ms |
15780 KB |
Output is correct |
99 |
Correct |
166 ms |
15600 KB |
Output is correct |
100 |
Correct |
161 ms |
18784 KB |
Output is correct |
101 |
Correct |
160 ms |
16752 KB |
Output is correct |
102 |
Correct |
158 ms |
16064 KB |
Output is correct |
103 |
Correct |
162 ms |
15892 KB |
Output is correct |
104 |
Correct |
97 ms |
16108 KB |
Output is correct |
105 |
Correct |
196 ms |
24676 KB |
Output is correct |
106 |
Correct |
95 ms |
15976 KB |
Output is correct |
107 |
Correct |
102 ms |
15980 KB |
Output is correct |
108 |
Correct |
199 ms |
28656 KB |
Output is correct |
109 |
Correct |
102 ms |
16296 KB |
Output is correct |
110 |
Correct |
212 ms |
29044 KB |
Output is correct |
111 |
Correct |
156 ms |
15108 KB |
Output is correct |