#include<bits/stdc++.h>
using namespace std;
#define mp make_pair
#define INF 10000000
#define MOD 1000000007
#define MID ((l+r)/2)
#define HASHMOD 2305843009213693951
#define ll long long
#define ull unsigned long long
#define F first
#define S second
typedef pair<ll, ll> ii;
typedef pair<ii, int> iii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef map<int, int> mii;
#define EPS 1e-6
#define FOR(i,n) for(int i=0;i<((int)(n));i++)
#define FORi(i,a,b) for(int i=((int)(a));i<((int)(b));i++)
#define FOA(v, a) for(auto v : a)
int t, n;
vi a, b;
vector<vi> adj;
string x;
int cnt[300000];
int dfsc(int s, int p=-1){
cnt[s] = (x[s-1]=='1');
FOA(v,adj[s]) if(v!=p) cnt[s] += dfsc(v,s);
return cnt[s];
}
int dp[300000];
int dfs(int s, int p=-1){
if(dp[s]!=-1) return dp[s];
int ans2=0;
if(x[s-1]=='1') ans2 = 1;
int ans3 = 0;
if(x[s-1]=='1') ans3--;
FOA(v, adj[s]){
if(v==p) continue;
//cout<<s<<" going to "<<v<<endl;
//if(x[s-1]=='1') ans2 = max(ans2, 1+dfs(v,s));
ans3 += dfs(v,s);
}
ans2 = max(ans2, ans3);
dp[s] = ans2;
return ans2;
}
int anss[300000];
int ans(int s, int p=-1){
int ans2=1;
int ans3 = 0;
if(x[s-1]=='1') ans3--;
FOA(v, adj[s]){
if(v==p) continue;
if(x[s-1]=='1') ans2 = max(ans2, 1+dfs(v));
ans3 += dfs(v);
ans(v,s);
}
ans2 = max(ans2, ans3);
anss[s] = ans2;
return ans2;
}
int main(){
cin>>n;
adj.assign(n+1, vi());
FOR(i,n+1) dp[i] = -1;
FOR(i,n-1){
int a, b;
cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
cin>>x;
dfsc(1);
dfs(1);
int xx = ans(1);
FORi(i,1,n+1){
xx = max(xx, anss[i]);
//cout<<i<<" "<<dfs(i)<<endl;
}
cout<<xx<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
300 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
2 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
304 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
300 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
2 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
304 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
2 ms |
460 KB |
Output is correct |
23 |
Correct |
3 ms |
460 KB |
Output is correct |
24 |
Correct |
3 ms |
416 KB |
Output is correct |
25 |
Correct |
2 ms |
460 KB |
Output is correct |
26 |
Correct |
2 ms |
460 KB |
Output is correct |
27 |
Correct |
3 ms |
460 KB |
Output is correct |
28 |
Correct |
2 ms |
460 KB |
Output is correct |
29 |
Correct |
3 ms |
532 KB |
Output is correct |
30 |
Correct |
2 ms |
460 KB |
Output is correct |
31 |
Correct |
2 ms |
460 KB |
Output is correct |
32 |
Correct |
2 ms |
460 KB |
Output is correct |
33 |
Correct |
2 ms |
460 KB |
Output is correct |
34 |
Correct |
2 ms |
460 KB |
Output is correct |
35 |
Correct |
2 ms |
460 KB |
Output is correct |
36 |
Correct |
2 ms |
460 KB |
Output is correct |
37 |
Correct |
3 ms |
440 KB |
Output is correct |
38 |
Correct |
3 ms |
444 KB |
Output is correct |
39 |
Correct |
3 ms |
440 KB |
Output is correct |
40 |
Correct |
4 ms |
460 KB |
Output is correct |
41 |
Correct |
2 ms |
460 KB |
Output is correct |
42 |
Correct |
3 ms |
428 KB |
Output is correct |
43 |
Correct |
3 ms |
460 KB |
Output is correct |
44 |
Correct |
2 ms |
440 KB |
Output is correct |
45 |
Correct |
2 ms |
440 KB |
Output is correct |
46 |
Correct |
2 ms |
440 KB |
Output is correct |
47 |
Correct |
3 ms |
460 KB |
Output is correct |
48 |
Correct |
2 ms |
460 KB |
Output is correct |
49 |
Correct |
2 ms |
460 KB |
Output is correct |
50 |
Correct |
2 ms |
460 KB |
Output is correct |
51 |
Correct |
2 ms |
460 KB |
Output is correct |
52 |
Correct |
3 ms |
460 KB |
Output is correct |
53 |
Correct |
2 ms |
460 KB |
Output is correct |
54 |
Correct |
2 ms |
460 KB |
Output is correct |
55 |
Correct |
3 ms |
460 KB |
Output is correct |
56 |
Correct |
2 ms |
512 KB |
Output is correct |
57 |
Correct |
2 ms |
588 KB |
Output is correct |
58 |
Correct |
2 ms |
460 KB |
Output is correct |
59 |
Correct |
2 ms |
460 KB |
Output is correct |
60 |
Correct |
2 ms |
460 KB |
Output is correct |
61 |
Correct |
2 ms |
460 KB |
Output is correct |
62 |
Correct |
2 ms |
460 KB |
Output is correct |
63 |
Correct |
3 ms |
448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
296 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
300 KB |
Output is correct |
8 |
Correct |
1 ms |
464 KB |
Output is correct |
9 |
Correct |
2 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
304 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
1 ms |
204 KB |
Output is correct |
17 |
Correct |
1 ms |
204 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
2 ms |
460 KB |
Output is correct |
23 |
Correct |
3 ms |
460 KB |
Output is correct |
24 |
Correct |
3 ms |
416 KB |
Output is correct |
25 |
Correct |
2 ms |
460 KB |
Output is correct |
26 |
Correct |
2 ms |
460 KB |
Output is correct |
27 |
Correct |
3 ms |
460 KB |
Output is correct |
28 |
Correct |
2 ms |
460 KB |
Output is correct |
29 |
Correct |
3 ms |
532 KB |
Output is correct |
30 |
Correct |
2 ms |
460 KB |
Output is correct |
31 |
Correct |
2 ms |
460 KB |
Output is correct |
32 |
Correct |
2 ms |
460 KB |
Output is correct |
33 |
Correct |
2 ms |
460 KB |
Output is correct |
34 |
Correct |
2 ms |
460 KB |
Output is correct |
35 |
Correct |
2 ms |
460 KB |
Output is correct |
36 |
Correct |
2 ms |
460 KB |
Output is correct |
37 |
Correct |
3 ms |
440 KB |
Output is correct |
38 |
Correct |
3 ms |
444 KB |
Output is correct |
39 |
Correct |
3 ms |
440 KB |
Output is correct |
40 |
Correct |
4 ms |
460 KB |
Output is correct |
41 |
Correct |
2 ms |
460 KB |
Output is correct |
42 |
Correct |
3 ms |
428 KB |
Output is correct |
43 |
Correct |
3 ms |
460 KB |
Output is correct |
44 |
Correct |
2 ms |
440 KB |
Output is correct |
45 |
Correct |
2 ms |
440 KB |
Output is correct |
46 |
Correct |
2 ms |
440 KB |
Output is correct |
47 |
Correct |
3 ms |
460 KB |
Output is correct |
48 |
Correct |
2 ms |
460 KB |
Output is correct |
49 |
Correct |
2 ms |
460 KB |
Output is correct |
50 |
Correct |
2 ms |
460 KB |
Output is correct |
51 |
Correct |
2 ms |
460 KB |
Output is correct |
52 |
Correct |
3 ms |
460 KB |
Output is correct |
53 |
Correct |
2 ms |
460 KB |
Output is correct |
54 |
Correct |
2 ms |
460 KB |
Output is correct |
55 |
Correct |
3 ms |
460 KB |
Output is correct |
56 |
Correct |
2 ms |
512 KB |
Output is correct |
57 |
Correct |
2 ms |
588 KB |
Output is correct |
58 |
Correct |
2 ms |
460 KB |
Output is correct |
59 |
Correct |
2 ms |
460 KB |
Output is correct |
60 |
Correct |
2 ms |
460 KB |
Output is correct |
61 |
Correct |
2 ms |
460 KB |
Output is correct |
62 |
Correct |
2 ms |
460 KB |
Output is correct |
63 |
Correct |
3 ms |
448 KB |
Output is correct |
64 |
Correct |
363 ms |
16860 KB |
Output is correct |
65 |
Correct |
377 ms |
16852 KB |
Output is correct |
66 |
Correct |
320 ms |
16724 KB |
Output is correct |
67 |
Correct |
411 ms |
16764 KB |
Output is correct |
68 |
Correct |
357 ms |
16800 KB |
Output is correct |
69 |
Correct |
369 ms |
16664 KB |
Output is correct |
70 |
Correct |
403 ms |
16752 KB |
Output is correct |
71 |
Correct |
344 ms |
16748 KB |
Output is correct |
72 |
Correct |
398 ms |
16820 KB |
Output is correct |
73 |
Correct |
373 ms |
29328 KB |
Output is correct |
74 |
Correct |
435 ms |
29280 KB |
Output is correct |
75 |
Correct |
353 ms |
16636 KB |
Output is correct |
76 |
Correct |
368 ms |
16724 KB |
Output is correct |
77 |
Correct |
380 ms |
16556 KB |
Output is correct |
78 |
Correct |
378 ms |
16648 KB |
Output is correct |
79 |
Correct |
393 ms |
16656 KB |
Output is correct |
80 |
Correct |
371 ms |
16648 KB |
Output is correct |
81 |
Correct |
290 ms |
17116 KB |
Output is correct |
82 |
Correct |
316 ms |
16956 KB |
Output is correct |
83 |
Correct |
360 ms |
21768 KB |
Output is correct |
84 |
Correct |
366 ms |
23936 KB |
Output is correct |
85 |
Correct |
429 ms |
23700 KB |
Output is correct |
86 |
Correct |
356 ms |
18140 KB |
Output is correct |
87 |
Correct |
385 ms |
17580 KB |
Output is correct |
88 |
Correct |
303 ms |
17884 KB |
Output is correct |
89 |
Correct |
321 ms |
18112 KB |
Output is correct |
90 |
Correct |
362 ms |
17700 KB |
Output is correct |
91 |
Correct |
332 ms |
18024 KB |
Output is correct |
92 |
Correct |
383 ms |
18336 KB |
Output is correct |
93 |
Correct |
346 ms |
20672 KB |
Output is correct |
94 |
Correct |
405 ms |
22484 KB |
Output is correct |
95 |
Correct |
386 ms |
21092 KB |
Output is correct |
96 |
Correct |
397 ms |
19300 KB |
Output is correct |
97 |
Correct |
391 ms |
17824 KB |
Output is correct |
98 |
Correct |
369 ms |
17296 KB |
Output is correct |
99 |
Correct |
361 ms |
17040 KB |
Output is correct |
100 |
Correct |
400 ms |
20308 KB |
Output is correct |
101 |
Correct |
386 ms |
18252 KB |
Output is correct |
102 |
Correct |
414 ms |
17436 KB |
Output is correct |
103 |
Correct |
388 ms |
17320 KB |
Output is correct |
104 |
Correct |
282 ms |
17576 KB |
Output is correct |
105 |
Correct |
456 ms |
26196 KB |
Output is correct |
106 |
Correct |
236 ms |
17436 KB |
Output is correct |
107 |
Correct |
241 ms |
17532 KB |
Output is correct |
108 |
Correct |
444 ms |
30120 KB |
Output is correct |
109 |
Correct |
259 ms |
17724 KB |
Output is correct |
110 |
Correct |
446 ms |
30456 KB |
Output is correct |
111 |
Correct |
334 ms |
16636 KB |
Output is correct |