#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
using namespace std;
const int mxn=2e5+5;
const int inf=1e9;
vector<int> adj[mxn];
int dp[mxn][2]={};
int ans[mxn]={};
int tp[mxn]={};
int sz[mxn]={};
int fa[mxn]={};
void dfs1(int cur,int prev) {
sz[cur]=(tp[cur]==1);
fa[cur]=(tp[cur]==1);
for(int u: adj[cur]) {
if(u==prev) continue;
dfs1(u,cur);
sz[cur]+=sz[u];
if(tp[cur]==0) {
fa[cur]+=fa[u];
}
}
}
void dfs(int cur,int prev) {
int mx1=0;
int sm=0;
int mx2=0;
int sm2=0;
int mx3=0;
for(int u : adj[cur]) {
if(u==prev)continue;
dfs(u,cur);
mx1=max(mx1,dp[u][0]);
mx2=max(mx2,fa[u]);
mx3=max(mx3,dp[u][1]);
sm+=fa[u];
sm2+=max(fa[u],dp[u][1]);
}
if(tp[cur]==0) {
dp[cur][0]=max({mx1,mx2,sm});
dp[cur][1]=sm2;
ans[cur]=max(dp[cur][0],dp[cur][1]);
} else {
dp[cur][0]=max({mx2+1,mx3+1});
dp[cur][1]=max({mx3-1,mx2-1,sm2-1,sm-1});
ans[cur]=max({dp[cur][0],mx1,mx3+1,mx2+1,sm2-1,sm-1});
}
}
int main() {
int n;
cin>>n;
for(int i=1;i<n;i++) {
int u,v;
scanf("%d%d",&u,&v);
adj[u].push_back(v);
adj[v].push_back(u);
}
string s;
cin>>s;
for(int i=0;i<n;i++) tp[i+1]=s[i]-'0';
dfs1(1,-1);
dfs(1,-1);
int res=0;
for(int i=1;i<=n;i++) res=max(res,ans[i]);
cout<<res<<endl;
}
Compilation message
power.cpp: In function 'int main()':
power.cpp:87:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
87 | scanf("%d%d",&u,&v);
| ~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 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 |
3 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 |
3 ms |
4992 KB |
Output is correct |
12 |
Correct |
3 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 |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
4992 KB |
Output is correct |
18 |
Correct |
4 ms |
4992 KB |
Output is correct |
19 |
Correct |
3 ms |
4992 KB |
Output is correct |
20 |
Correct |
3 ms |
4992 KB |
Output is correct |
21 |
Correct |
3 ms |
4992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 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 |
3 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 |
3 ms |
4992 KB |
Output is correct |
12 |
Correct |
3 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 |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
4992 KB |
Output is correct |
18 |
Correct |
4 ms |
4992 KB |
Output is correct |
19 |
Correct |
3 ms |
4992 KB |
Output is correct |
20 |
Correct |
3 ms |
4992 KB |
Output is correct |
21 |
Correct |
3 ms |
4992 KB |
Output is correct |
22 |
Correct |
5 ms |
5120 KB |
Output is correct |
23 |
Correct |
4 ms |
5120 KB |
Output is correct |
24 |
Correct |
5 ms |
5120 KB |
Output is correct |
25 |
Correct |
5 ms |
5120 KB |
Output is correct |
26 |
Correct |
6 ms |
5120 KB |
Output is correct |
27 |
Correct |
4 ms |
5120 KB |
Output is correct |
28 |
Correct |
5 ms |
5120 KB |
Output is correct |
29 |
Correct |
4 ms |
5376 KB |
Output is correct |
30 |
Correct |
5 ms |
5376 KB |
Output is correct |
31 |
Correct |
5 ms |
5120 KB |
Output is correct |
32 |
Correct |
5 ms |
5208 KB |
Output is correct |
33 |
Correct |
5 ms |
5120 KB |
Output is correct |
34 |
Correct |
5 ms |
5120 KB |
Output is correct |
35 |
Correct |
6 ms |
5120 KB |
Output is correct |
36 |
Correct |
5 ms |
5120 KB |
Output is correct |
37 |
Correct |
5 ms |
5120 KB |
Output is correct |
38 |
Correct |
5 ms |
5120 KB |
Output is correct |
39 |
Correct |
5 ms |
5248 KB |
Output is correct |
40 |
Correct |
5 ms |
5248 KB |
Output is correct |
41 |
Correct |
5 ms |
5248 KB |
Output is correct |
42 |
Correct |
4 ms |
5248 KB |
Output is correct |
43 |
Correct |
5 ms |
5248 KB |
Output is correct |
44 |
Correct |
4 ms |
5248 KB |
Output is correct |
45 |
Correct |
5 ms |
5248 KB |
Output is correct |
46 |
Correct |
5 ms |
5248 KB |
Output is correct |
47 |
Correct |
6 ms |
5248 KB |
Output is correct |
48 |
Correct |
5 ms |
5248 KB |
Output is correct |
49 |
Correct |
5 ms |
5248 KB |
Output is correct |
50 |
Correct |
5 ms |
5248 KB |
Output is correct |
51 |
Correct |
5 ms |
5248 KB |
Output is correct |
52 |
Correct |
5 ms |
5120 KB |
Output is correct |
53 |
Correct |
5 ms |
5248 KB |
Output is correct |
54 |
Correct |
5 ms |
5248 KB |
Output is correct |
55 |
Correct |
5 ms |
5248 KB |
Output is correct |
56 |
Correct |
5 ms |
5120 KB |
Output is correct |
57 |
Correct |
5 ms |
5376 KB |
Output is correct |
58 |
Correct |
4 ms |
5248 KB |
Output is correct |
59 |
Correct |
4 ms |
5248 KB |
Output is correct |
60 |
Correct |
5 ms |
5248 KB |
Output is correct |
61 |
Correct |
4 ms |
5120 KB |
Output is correct |
62 |
Correct |
5 ms |
5376 KB |
Output is correct |
63 |
Correct |
5 ms |
5120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
4992 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 |
3 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 |
3 ms |
4992 KB |
Output is correct |
12 |
Correct |
3 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 |
4 ms |
4992 KB |
Output is correct |
17 |
Correct |
4 ms |
4992 KB |
Output is correct |
18 |
Correct |
4 ms |
4992 KB |
Output is correct |
19 |
Correct |
3 ms |
4992 KB |
Output is correct |
20 |
Correct |
3 ms |
4992 KB |
Output is correct |
21 |
Correct |
3 ms |
4992 KB |
Output is correct |
22 |
Correct |
5 ms |
5120 KB |
Output is correct |
23 |
Correct |
4 ms |
5120 KB |
Output is correct |
24 |
Correct |
5 ms |
5120 KB |
Output is correct |
25 |
Correct |
5 ms |
5120 KB |
Output is correct |
26 |
Correct |
6 ms |
5120 KB |
Output is correct |
27 |
Correct |
4 ms |
5120 KB |
Output is correct |
28 |
Correct |
5 ms |
5120 KB |
Output is correct |
29 |
Correct |
4 ms |
5376 KB |
Output is correct |
30 |
Correct |
5 ms |
5376 KB |
Output is correct |
31 |
Correct |
5 ms |
5120 KB |
Output is correct |
32 |
Correct |
5 ms |
5208 KB |
Output is correct |
33 |
Correct |
5 ms |
5120 KB |
Output is correct |
34 |
Correct |
5 ms |
5120 KB |
Output is correct |
35 |
Correct |
6 ms |
5120 KB |
Output is correct |
36 |
Correct |
5 ms |
5120 KB |
Output is correct |
37 |
Correct |
5 ms |
5120 KB |
Output is correct |
38 |
Correct |
5 ms |
5120 KB |
Output is correct |
39 |
Correct |
5 ms |
5248 KB |
Output is correct |
40 |
Correct |
5 ms |
5248 KB |
Output is correct |
41 |
Correct |
5 ms |
5248 KB |
Output is correct |
42 |
Correct |
4 ms |
5248 KB |
Output is correct |
43 |
Correct |
5 ms |
5248 KB |
Output is correct |
44 |
Correct |
4 ms |
5248 KB |
Output is correct |
45 |
Correct |
5 ms |
5248 KB |
Output is correct |
46 |
Correct |
5 ms |
5248 KB |
Output is correct |
47 |
Correct |
6 ms |
5248 KB |
Output is correct |
48 |
Correct |
5 ms |
5248 KB |
Output is correct |
49 |
Correct |
5 ms |
5248 KB |
Output is correct |
50 |
Correct |
5 ms |
5248 KB |
Output is correct |
51 |
Correct |
5 ms |
5248 KB |
Output is correct |
52 |
Correct |
5 ms |
5120 KB |
Output is correct |
53 |
Correct |
5 ms |
5248 KB |
Output is correct |
54 |
Correct |
5 ms |
5248 KB |
Output is correct |
55 |
Correct |
5 ms |
5248 KB |
Output is correct |
56 |
Correct |
5 ms |
5120 KB |
Output is correct |
57 |
Correct |
5 ms |
5376 KB |
Output is correct |
58 |
Correct |
4 ms |
5248 KB |
Output is correct |
59 |
Correct |
4 ms |
5248 KB |
Output is correct |
60 |
Correct |
5 ms |
5248 KB |
Output is correct |
61 |
Correct |
4 ms |
5120 KB |
Output is correct |
62 |
Correct |
5 ms |
5376 KB |
Output is correct |
63 |
Correct |
5 ms |
5120 KB |
Output is correct |
64 |
Correct |
273 ms |
19076 KB |
Output is correct |
65 |
Correct |
277 ms |
19344 KB |
Output is correct |
66 |
Correct |
259 ms |
19200 KB |
Output is correct |
67 |
Correct |
249 ms |
19080 KB |
Output is correct |
68 |
Correct |
248 ms |
19204 KB |
Output is correct |
69 |
Correct |
244 ms |
19208 KB |
Output is correct |
70 |
Correct |
244 ms |
19080 KB |
Output is correct |
71 |
Correct |
246 ms |
19324 KB |
Output is correct |
72 |
Correct |
247 ms |
19080 KB |
Output is correct |
73 |
Correct |
291 ms |
39560 KB |
Output is correct |
74 |
Correct |
279 ms |
39304 KB |
Output is correct |
75 |
Correct |
248 ms |
18952 KB |
Output is correct |
76 |
Correct |
241 ms |
19080 KB |
Output is correct |
77 |
Correct |
279 ms |
18952 KB |
Output is correct |
78 |
Correct |
254 ms |
19208 KB |
Output is correct |
79 |
Correct |
251 ms |
19080 KB |
Output is correct |
80 |
Correct |
253 ms |
18952 KB |
Output is correct |
81 |
Correct |
212 ms |
19440 KB |
Output is correct |
82 |
Correct |
242 ms |
19316 KB |
Output is correct |
83 |
Correct |
271 ms |
27400 KB |
Output is correct |
84 |
Correct |
267 ms |
30856 KB |
Output is correct |
85 |
Correct |
261 ms |
30216 KB |
Output is correct |
86 |
Correct |
254 ms |
21396 KB |
Output is correct |
87 |
Correct |
247 ms |
20360 KB |
Output is correct |
88 |
Correct |
257 ms |
20744 KB |
Output is correct |
89 |
Correct |
273 ms |
20744 KB |
Output is correct |
90 |
Correct |
249 ms |
20420 KB |
Output is correct |
91 |
Correct |
243 ms |
20728 KB |
Output is correct |
92 |
Correct |
247 ms |
21256 KB |
Output is correct |
93 |
Correct |
239 ms |
25348 KB |
Output is correct |
94 |
Correct |
244 ms |
28092 KB |
Output is correct |
95 |
Correct |
242 ms |
26052 KB |
Output is correct |
96 |
Correct |
247 ms |
23048 KB |
Output is correct |
97 |
Correct |
247 ms |
20616 KB |
Output is correct |
98 |
Correct |
258 ms |
19840 KB |
Output is correct |
99 |
Correct |
254 ms |
19500 KB |
Output is correct |
100 |
Correct |
250 ms |
24588 KB |
Output is correct |
101 |
Correct |
264 ms |
21344 KB |
Output is correct |
102 |
Correct |
258 ms |
20144 KB |
Output is correct |
103 |
Correct |
247 ms |
19976 KB |
Output is correct |
104 |
Correct |
147 ms |
20076 KB |
Output is correct |
105 |
Correct |
283 ms |
34312 KB |
Output is correct |
106 |
Correct |
157 ms |
19820 KB |
Output is correct |
107 |
Correct |
155 ms |
19948 KB |
Output is correct |
108 |
Correct |
294 ms |
40668 KB |
Output is correct |
109 |
Correct |
143 ms |
20032 KB |
Output is correct |
110 |
Correct |
286 ms |
41140 KB |
Output is correct |
111 |
Correct |
234 ms |
18952 KB |
Output is correct |