import sys
input = lambda: sys.stdin.readline().strip()
sys.setrecursionlimit(1000000000)
n = int(input())
if n==1:
print(1)
quit()
edges = [[] for i in range(n+1)]
par = [-1 for i in range(n+1)]
visited = [False for i in range(n+1)]
for i in range(n-1):
a,b = map(int, input().split())
edges[a].append(b)
edges[b].append(a)
sus = input()
root = 1
flag = False
def dfs(a):
for i in edges[a]:
if not visited[i]:
par[i] = a
visited[i] = True
dfs(i)
visited[root] = True
dfs(root)
dps = [-1 for i in range(n+1)]
used = [0 for i in range(n+1)]
ans = -1
def dp(a):
global ans
if dps[a] != -1:
return dps[a]
sum = 0
maxi = 0
for i in edges[a]:
if i == par[a]: continue
sum += dp(i)
maxi = max(maxi, dp(i))
if sus[a-1]=="1":
ans = max(ans, maxi+1, sum-1)
else:
ans = max(ans,sum)
dps[a] = max(sum-int(sus[a-1]), int(sus[a-1]))
return dps[a]
dp(root)
print(ans)
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
18192 KB |
Output is correct |
2 |
Correct |
33 ms |
18228 KB |
Output is correct |
3 |
Correct |
39 ms |
18200 KB |
Output is correct |
4 |
Correct |
35 ms |
18144 KB |
Output is correct |
5 |
Correct |
36 ms |
18160 KB |
Output is correct |
6 |
Correct |
44 ms |
18160 KB |
Output is correct |
7 |
Correct |
39 ms |
18220 KB |
Output is correct |
8 |
Correct |
39 ms |
18168 KB |
Output is correct |
9 |
Correct |
37 ms |
18176 KB |
Output is correct |
10 |
Correct |
35 ms |
18220 KB |
Output is correct |
11 |
Correct |
34 ms |
18152 KB |
Output is correct |
12 |
Correct |
41 ms |
18160 KB |
Output is correct |
13 |
Correct |
34 ms |
18228 KB |
Output is correct |
14 |
Correct |
36 ms |
18188 KB |
Output is correct |
15 |
Correct |
44 ms |
18232 KB |
Output is correct |
16 |
Correct |
35 ms |
18200 KB |
Output is correct |
17 |
Correct |
34 ms |
18220 KB |
Output is correct |
18 |
Correct |
37 ms |
18208 KB |
Output is correct |
19 |
Correct |
39 ms |
18220 KB |
Output is correct |
20 |
Correct |
38 ms |
18112 KB |
Output is correct |
21 |
Correct |
36 ms |
18220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
18192 KB |
Output is correct |
2 |
Correct |
33 ms |
18228 KB |
Output is correct |
3 |
Correct |
39 ms |
18200 KB |
Output is correct |
4 |
Correct |
35 ms |
18144 KB |
Output is correct |
5 |
Correct |
36 ms |
18160 KB |
Output is correct |
6 |
Correct |
44 ms |
18160 KB |
Output is correct |
7 |
Correct |
39 ms |
18220 KB |
Output is correct |
8 |
Correct |
39 ms |
18168 KB |
Output is correct |
9 |
Correct |
37 ms |
18176 KB |
Output is correct |
10 |
Correct |
35 ms |
18220 KB |
Output is correct |
11 |
Correct |
34 ms |
18152 KB |
Output is correct |
12 |
Correct |
41 ms |
18160 KB |
Output is correct |
13 |
Correct |
34 ms |
18228 KB |
Output is correct |
14 |
Correct |
36 ms |
18188 KB |
Output is correct |
15 |
Correct |
44 ms |
18232 KB |
Output is correct |
16 |
Correct |
35 ms |
18200 KB |
Output is correct |
17 |
Correct |
34 ms |
18220 KB |
Output is correct |
18 |
Correct |
37 ms |
18208 KB |
Output is correct |
19 |
Correct |
39 ms |
18220 KB |
Output is correct |
20 |
Correct |
38 ms |
18112 KB |
Output is correct |
21 |
Correct |
36 ms |
18220 KB |
Output is correct |
22 |
Correct |
85 ms |
23176 KB |
Output is correct |
23 |
Correct |
83 ms |
23196 KB |
Output is correct |
24 |
Correct |
92 ms |
25180 KB |
Output is correct |
25 |
Correct |
95 ms |
23948 KB |
Output is correct |
26 |
Correct |
105 ms |
23468 KB |
Output is correct |
27 |
Correct |
82 ms |
23552 KB |
Output is correct |
28 |
Correct |
86 ms |
24220 KB |
Output is correct |
29 |
Correct |
90 ms |
24452 KB |
Output is correct |
30 |
Correct |
96 ms |
24784 KB |
Output is correct |
31 |
Correct |
96 ms |
23256 KB |
Output is correct |
32 |
Correct |
94 ms |
22968 KB |
Output is correct |
33 |
Correct |
99 ms |
24428 KB |
Output is correct |
34 |
Correct |
95 ms |
24196 KB |
Output is correct |
35 |
Correct |
89 ms |
22936 KB |
Output is correct |
36 |
Correct |
85 ms |
22868 KB |
Output is correct |
37 |
Correct |
106 ms |
21984 KB |
Output is correct |
38 |
Correct |
105 ms |
23528 KB |
Output is correct |
39 |
Correct |
89 ms |
25788 KB |
Output is correct |
40 |
Correct |
121 ms |
37140 KB |
Output is correct |
41 |
Correct |
101 ms |
26948 KB |
Output is correct |
42 |
Correct |
93 ms |
25872 KB |
Output is correct |
43 |
Correct |
93 ms |
29424 KB |
Output is correct |
44 |
Correct |
112 ms |
28280 KB |
Output is correct |
45 |
Correct |
82 ms |
25044 KB |
Output is correct |
46 |
Correct |
103 ms |
27288 KB |
Output is correct |
47 |
Correct |
104 ms |
25720 KB |
Output is correct |
48 |
Correct |
80 ms |
23612 KB |
Output is correct |
49 |
Correct |
83 ms |
23792 KB |
Output is correct |
50 |
Correct |
90 ms |
29556 KB |
Output is correct |
51 |
Correct |
99 ms |
28476 KB |
Output is correct |
52 |
Correct |
96 ms |
24092 KB |
Output is correct |
53 |
Correct |
90 ms |
28224 KB |
Output is correct |
54 |
Correct |
87 ms |
25832 KB |
Output is correct |
55 |
Correct |
94 ms |
24212 KB |
Output is correct |
56 |
Correct |
69 ms |
20968 KB |
Output is correct |
57 |
Correct |
90 ms |
25372 KB |
Output is correct |
58 |
Correct |
78 ms |
21308 KB |
Output is correct |
59 |
Correct |
69 ms |
20948 KB |
Output is correct |
60 |
Correct |
81 ms |
24220 KB |
Output is correct |
61 |
Correct |
71 ms |
21496 KB |
Output is correct |
62 |
Correct |
82 ms |
25880 KB |
Output is correct |
63 |
Correct |
96 ms |
22768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
34 ms |
18192 KB |
Output is correct |
2 |
Correct |
33 ms |
18228 KB |
Output is correct |
3 |
Correct |
39 ms |
18200 KB |
Output is correct |
4 |
Correct |
35 ms |
18144 KB |
Output is correct |
5 |
Correct |
36 ms |
18160 KB |
Output is correct |
6 |
Correct |
44 ms |
18160 KB |
Output is correct |
7 |
Correct |
39 ms |
18220 KB |
Output is correct |
8 |
Correct |
39 ms |
18168 KB |
Output is correct |
9 |
Correct |
37 ms |
18176 KB |
Output is correct |
10 |
Correct |
35 ms |
18220 KB |
Output is correct |
11 |
Correct |
34 ms |
18152 KB |
Output is correct |
12 |
Correct |
41 ms |
18160 KB |
Output is correct |
13 |
Correct |
34 ms |
18228 KB |
Output is correct |
14 |
Correct |
36 ms |
18188 KB |
Output is correct |
15 |
Correct |
44 ms |
18232 KB |
Output is correct |
16 |
Correct |
35 ms |
18200 KB |
Output is correct |
17 |
Correct |
34 ms |
18220 KB |
Output is correct |
18 |
Correct |
37 ms |
18208 KB |
Output is correct |
19 |
Correct |
39 ms |
18220 KB |
Output is correct |
20 |
Correct |
38 ms |
18112 KB |
Output is correct |
21 |
Correct |
36 ms |
18220 KB |
Output is correct |
22 |
Correct |
85 ms |
23176 KB |
Output is correct |
23 |
Correct |
83 ms |
23196 KB |
Output is correct |
24 |
Correct |
92 ms |
25180 KB |
Output is correct |
25 |
Correct |
95 ms |
23948 KB |
Output is correct |
26 |
Correct |
105 ms |
23468 KB |
Output is correct |
27 |
Correct |
82 ms |
23552 KB |
Output is correct |
28 |
Correct |
86 ms |
24220 KB |
Output is correct |
29 |
Correct |
90 ms |
24452 KB |
Output is correct |
30 |
Correct |
96 ms |
24784 KB |
Output is correct |
31 |
Correct |
96 ms |
23256 KB |
Output is correct |
32 |
Correct |
94 ms |
22968 KB |
Output is correct |
33 |
Correct |
99 ms |
24428 KB |
Output is correct |
34 |
Correct |
95 ms |
24196 KB |
Output is correct |
35 |
Correct |
89 ms |
22936 KB |
Output is correct |
36 |
Correct |
85 ms |
22868 KB |
Output is correct |
37 |
Correct |
106 ms |
21984 KB |
Output is correct |
38 |
Correct |
105 ms |
23528 KB |
Output is correct |
39 |
Correct |
89 ms |
25788 KB |
Output is correct |
40 |
Correct |
121 ms |
37140 KB |
Output is correct |
41 |
Correct |
101 ms |
26948 KB |
Output is correct |
42 |
Correct |
93 ms |
25872 KB |
Output is correct |
43 |
Correct |
93 ms |
29424 KB |
Output is correct |
44 |
Correct |
112 ms |
28280 KB |
Output is correct |
45 |
Correct |
82 ms |
25044 KB |
Output is correct |
46 |
Correct |
103 ms |
27288 KB |
Output is correct |
47 |
Correct |
104 ms |
25720 KB |
Output is correct |
48 |
Correct |
80 ms |
23612 KB |
Output is correct |
49 |
Correct |
83 ms |
23792 KB |
Output is correct |
50 |
Correct |
90 ms |
29556 KB |
Output is correct |
51 |
Correct |
99 ms |
28476 KB |
Output is correct |
52 |
Correct |
96 ms |
24092 KB |
Output is correct |
53 |
Correct |
90 ms |
28224 KB |
Output is correct |
54 |
Correct |
87 ms |
25832 KB |
Output is correct |
55 |
Correct |
94 ms |
24212 KB |
Output is correct |
56 |
Correct |
69 ms |
20968 KB |
Output is correct |
57 |
Correct |
90 ms |
25372 KB |
Output is correct |
58 |
Correct |
78 ms |
21308 KB |
Output is correct |
59 |
Correct |
69 ms |
20948 KB |
Output is correct |
60 |
Correct |
81 ms |
24220 KB |
Output is correct |
61 |
Correct |
71 ms |
21496 KB |
Output is correct |
62 |
Correct |
82 ms |
25880 KB |
Output is correct |
63 |
Correct |
96 ms |
22768 KB |
Output is correct |
64 |
Correct |
807 ms |
65076 KB |
Output is correct |
65 |
Correct |
774 ms |
63760 KB |
Output is correct |
66 |
Correct |
716 ms |
63388 KB |
Output is correct |
67 |
Correct |
727 ms |
63796 KB |
Output is correct |
68 |
Correct |
865 ms |
65316 KB |
Output is correct |
69 |
Correct |
806 ms |
64468 KB |
Output is correct |
70 |
Correct |
850 ms |
64404 KB |
Output is correct |
71 |
Correct |
721 ms |
62552 KB |
Output is correct |
72 |
Correct |
697 ms |
62000 KB |
Output is correct |
73 |
Execution timed out |
1604 ms |
500724 KB |
Time limit exceeded |
74 |
Halted |
0 ms |
0 KB |
- |