import sys
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)
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
18220 KB |
Output is correct |
2 |
Correct |
33 ms |
18164 KB |
Output is correct |
3 |
Correct |
36 ms |
18128 KB |
Output is correct |
4 |
Correct |
37 ms |
18144 KB |
Output is correct |
5 |
Correct |
36 ms |
18164 KB |
Output is correct |
6 |
Correct |
39 ms |
18220 KB |
Output is correct |
7 |
Correct |
35 ms |
18196 KB |
Output is correct |
8 |
Correct |
35 ms |
18244 KB |
Output is correct |
9 |
Correct |
38 ms |
18228 KB |
Output is correct |
10 |
Correct |
39 ms |
18220 KB |
Output is correct |
11 |
Correct |
32 ms |
18136 KB |
Output is correct |
12 |
Correct |
34 ms |
18184 KB |
Output is correct |
13 |
Correct |
35 ms |
18200 KB |
Output is correct |
14 |
Correct |
35 ms |
18212 KB |
Output is correct |
15 |
Correct |
34 ms |
18184 KB |
Output is correct |
16 |
Correct |
34 ms |
18136 KB |
Output is correct |
17 |
Correct |
38 ms |
18264 KB |
Output is correct |
18 |
Correct |
35 ms |
18220 KB |
Output is correct |
19 |
Correct |
33 ms |
18160 KB |
Output is correct |
20 |
Correct |
38 ms |
18156 KB |
Output is correct |
21 |
Correct |
40 ms |
18240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
18220 KB |
Output is correct |
2 |
Correct |
33 ms |
18164 KB |
Output is correct |
3 |
Correct |
36 ms |
18128 KB |
Output is correct |
4 |
Correct |
37 ms |
18144 KB |
Output is correct |
5 |
Correct |
36 ms |
18164 KB |
Output is correct |
6 |
Correct |
39 ms |
18220 KB |
Output is correct |
7 |
Correct |
35 ms |
18196 KB |
Output is correct |
8 |
Correct |
35 ms |
18244 KB |
Output is correct |
9 |
Correct |
38 ms |
18228 KB |
Output is correct |
10 |
Correct |
39 ms |
18220 KB |
Output is correct |
11 |
Correct |
32 ms |
18136 KB |
Output is correct |
12 |
Correct |
34 ms |
18184 KB |
Output is correct |
13 |
Correct |
35 ms |
18200 KB |
Output is correct |
14 |
Correct |
35 ms |
18212 KB |
Output is correct |
15 |
Correct |
34 ms |
18184 KB |
Output is correct |
16 |
Correct |
34 ms |
18136 KB |
Output is correct |
17 |
Correct |
38 ms |
18264 KB |
Output is correct |
18 |
Correct |
35 ms |
18220 KB |
Output is correct |
19 |
Correct |
33 ms |
18160 KB |
Output is correct |
20 |
Correct |
38 ms |
18156 KB |
Output is correct |
21 |
Correct |
40 ms |
18240 KB |
Output is correct |
22 |
Correct |
123 ms |
25848 KB |
Output is correct |
23 |
Correct |
125 ms |
25280 KB |
Output is correct |
24 |
Correct |
136 ms |
26388 KB |
Output is correct |
25 |
Correct |
122 ms |
26000 KB |
Output is correct |
26 |
Correct |
134 ms |
25888 KB |
Output is correct |
27 |
Correct |
165 ms |
26300 KB |
Output is correct |
28 |
Correct |
117 ms |
25380 KB |
Output is correct |
29 |
Correct |
121 ms |
26272 KB |
Output is correct |
30 |
Correct |
146 ms |
27580 KB |
Output is correct |
31 |
Correct |
134 ms |
25500 KB |
Output is correct |
32 |
Correct |
125 ms |
25760 KB |
Output is correct |
33 |
Correct |
136 ms |
26360 KB |
Output is correct |
34 |
Correct |
128 ms |
26652 KB |
Output is correct |
35 |
Correct |
131 ms |
25212 KB |
Output is correct |
36 |
Correct |
134 ms |
25432 KB |
Output is correct |
37 |
Correct |
118 ms |
24860 KB |
Output is correct |
38 |
Correct |
142 ms |
25972 KB |
Output is correct |
39 |
Correct |
129 ms |
26944 KB |
Output is correct |
40 |
Correct |
186 ms |
37468 KB |
Output is correct |
41 |
Correct |
142 ms |
28160 KB |
Output is correct |
42 |
Correct |
125 ms |
26952 KB |
Output is correct |
43 |
Correct |
126 ms |
28012 KB |
Output is correct |
44 |
Correct |
126 ms |
28272 KB |
Output is correct |
45 |
Correct |
147 ms |
26728 KB |
Output is correct |
46 |
Correct |
182 ms |
28604 KB |
Output is correct |
47 |
Correct |
116 ms |
26568 KB |
Output is correct |
48 |
Correct |
110 ms |
25500 KB |
Output is correct |
49 |
Correct |
116 ms |
25688 KB |
Output is correct |
50 |
Correct |
165 ms |
33276 KB |
Output is correct |
51 |
Correct |
133 ms |
29724 KB |
Output is correct |
52 |
Correct |
138 ms |
26396 KB |
Output is correct |
53 |
Correct |
124 ms |
29324 KB |
Output is correct |
54 |
Correct |
153 ms |
27164 KB |
Output is correct |
55 |
Correct |
121 ms |
26264 KB |
Output is correct |
56 |
Correct |
97 ms |
23464 KB |
Output is correct |
57 |
Correct |
127 ms |
27676 KB |
Output is correct |
58 |
Correct |
102 ms |
23956 KB |
Output is correct |
59 |
Correct |
114 ms |
23476 KB |
Output is correct |
60 |
Correct |
158 ms |
27568 KB |
Output is correct |
61 |
Correct |
101 ms |
23948 KB |
Output is correct |
62 |
Correct |
116 ms |
28752 KB |
Output is correct |
63 |
Correct |
119 ms |
25276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
18220 KB |
Output is correct |
2 |
Correct |
33 ms |
18164 KB |
Output is correct |
3 |
Correct |
36 ms |
18128 KB |
Output is correct |
4 |
Correct |
37 ms |
18144 KB |
Output is correct |
5 |
Correct |
36 ms |
18164 KB |
Output is correct |
6 |
Correct |
39 ms |
18220 KB |
Output is correct |
7 |
Correct |
35 ms |
18196 KB |
Output is correct |
8 |
Correct |
35 ms |
18244 KB |
Output is correct |
9 |
Correct |
38 ms |
18228 KB |
Output is correct |
10 |
Correct |
39 ms |
18220 KB |
Output is correct |
11 |
Correct |
32 ms |
18136 KB |
Output is correct |
12 |
Correct |
34 ms |
18184 KB |
Output is correct |
13 |
Correct |
35 ms |
18200 KB |
Output is correct |
14 |
Correct |
35 ms |
18212 KB |
Output is correct |
15 |
Correct |
34 ms |
18184 KB |
Output is correct |
16 |
Correct |
34 ms |
18136 KB |
Output is correct |
17 |
Correct |
38 ms |
18264 KB |
Output is correct |
18 |
Correct |
35 ms |
18220 KB |
Output is correct |
19 |
Correct |
33 ms |
18160 KB |
Output is correct |
20 |
Correct |
38 ms |
18156 KB |
Output is correct |
21 |
Correct |
40 ms |
18240 KB |
Output is correct |
22 |
Correct |
123 ms |
25848 KB |
Output is correct |
23 |
Correct |
125 ms |
25280 KB |
Output is correct |
24 |
Correct |
136 ms |
26388 KB |
Output is correct |
25 |
Correct |
122 ms |
26000 KB |
Output is correct |
26 |
Correct |
134 ms |
25888 KB |
Output is correct |
27 |
Correct |
165 ms |
26300 KB |
Output is correct |
28 |
Correct |
117 ms |
25380 KB |
Output is correct |
29 |
Correct |
121 ms |
26272 KB |
Output is correct |
30 |
Correct |
146 ms |
27580 KB |
Output is correct |
31 |
Correct |
134 ms |
25500 KB |
Output is correct |
32 |
Correct |
125 ms |
25760 KB |
Output is correct |
33 |
Correct |
136 ms |
26360 KB |
Output is correct |
34 |
Correct |
128 ms |
26652 KB |
Output is correct |
35 |
Correct |
131 ms |
25212 KB |
Output is correct |
36 |
Correct |
134 ms |
25432 KB |
Output is correct |
37 |
Correct |
118 ms |
24860 KB |
Output is correct |
38 |
Correct |
142 ms |
25972 KB |
Output is correct |
39 |
Correct |
129 ms |
26944 KB |
Output is correct |
40 |
Correct |
186 ms |
37468 KB |
Output is correct |
41 |
Correct |
142 ms |
28160 KB |
Output is correct |
42 |
Correct |
125 ms |
26952 KB |
Output is correct |
43 |
Correct |
126 ms |
28012 KB |
Output is correct |
44 |
Correct |
126 ms |
28272 KB |
Output is correct |
45 |
Correct |
147 ms |
26728 KB |
Output is correct |
46 |
Correct |
182 ms |
28604 KB |
Output is correct |
47 |
Correct |
116 ms |
26568 KB |
Output is correct |
48 |
Correct |
110 ms |
25500 KB |
Output is correct |
49 |
Correct |
116 ms |
25688 KB |
Output is correct |
50 |
Correct |
165 ms |
33276 KB |
Output is correct |
51 |
Correct |
133 ms |
29724 KB |
Output is correct |
52 |
Correct |
138 ms |
26396 KB |
Output is correct |
53 |
Correct |
124 ms |
29324 KB |
Output is correct |
54 |
Correct |
153 ms |
27164 KB |
Output is correct |
55 |
Correct |
121 ms |
26264 KB |
Output is correct |
56 |
Correct |
97 ms |
23464 KB |
Output is correct |
57 |
Correct |
127 ms |
27676 KB |
Output is correct |
58 |
Correct |
102 ms |
23956 KB |
Output is correct |
59 |
Correct |
114 ms |
23476 KB |
Output is correct |
60 |
Correct |
158 ms |
27568 KB |
Output is correct |
61 |
Correct |
101 ms |
23948 KB |
Output is correct |
62 |
Correct |
116 ms |
28752 KB |
Output is correct |
63 |
Correct |
119 ms |
25276 KB |
Output is correct |
64 |
Correct |
1056 ms |
68384 KB |
Output is correct |
65 |
Correct |
1040 ms |
66704 KB |
Output is correct |
66 |
Correct |
987 ms |
65312 KB |
Output is correct |
67 |
Correct |
999 ms |
68296 KB |
Output is correct |
68 |
Correct |
1002 ms |
68700 KB |
Output is correct |
69 |
Correct |
1022 ms |
68260 KB |
Output is correct |
70 |
Correct |
1084 ms |
67324 KB |
Output is correct |
71 |
Correct |
963 ms |
65268 KB |
Output is correct |
72 |
Correct |
934 ms |
66196 KB |
Output is correct |
73 |
Execution timed out |
1612 ms |
462556 KB |
Time limit exceeded |
74 |
Halted |
0 ms |
0 KB |
- |