import sys
from collections import defaultdict
from sys import setrecursionlimit, stdin, stdout
setrecursionlimit(1000000)
class DSU:
def __init__(self, n):
self.parent = list(range(n))
def find(self, a):
acopy = a
while a != self.parent[a]:
a = self.parent[a]
while acopy != a:
self.parent[acopy], acopy = a, self.parent[acopy]
return a
def merge(self, a, b):
self.parent[self.find(b)] = self.find(a)
def setup(v, parent):
for p in graf[v]:
if p == parent:
continue
depth[p] = depth[v] + 1
up[p] = v
setup(p, v)
def merge(a, b):
a = dsu.find(a)
b = dsu.find(b)
while a != b:
if depth[a] < depth[b]:
a, b = b, a
dsu.merge(up[a], a)
a = dsu.find(up[a])
n, k = map(int, input().split())
graf = [[] for i in range(n+1)]
dsu = DSU(n)
up = [0] * n
depth = [0] * n
for _ in range(n - 1):
a, b = map(int, input().split())
a-=1
b-=1
graf[a].append(b)
graf[b].append(a)
setup(0, -1)
groups = [[] for i in range(n+1)]
for i in range(n):
a = int(input()) - 1
groups[a].append(i)
for group in groups:
for j in range(1, len(group)):
merge(group[0], group[j])
sol = 0
deg = [0] * n
for i in range(n):
for p in graf[i]:
if dsu.find(i) != dsu.find(p):
if deg[dsu.find(i)] == 0:
sol += 1
if deg[dsu.find(i)] == 1:
sol -= 1
deg[dsu.find(i)] += 1
print((sol + 1) // 2)
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
18740 KB |
Output is correct |
2 |
Correct |
31 ms |
18740 KB |
Output is correct |
3 |
Correct |
30 ms |
18748 KB |
Output is correct |
4 |
Correct |
28 ms |
18748 KB |
Output is correct |
5 |
Correct |
29 ms |
18808 KB |
Output is correct |
6 |
Correct |
31 ms |
18740 KB |
Output is correct |
7 |
Correct |
32 ms |
18740 KB |
Output is correct |
8 |
Correct |
32 ms |
18740 KB |
Output is correct |
9 |
Correct |
34 ms |
18640 KB |
Output is correct |
10 |
Correct |
28 ms |
18716 KB |
Output is correct |
11 |
Correct |
32 ms |
18740 KB |
Output is correct |
12 |
Correct |
29 ms |
18740 KB |
Output is correct |
13 |
Correct |
27 ms |
18740 KB |
Output is correct |
14 |
Correct |
34 ms |
18836 KB |
Output is correct |
15 |
Correct |
30 ms |
18744 KB |
Output is correct |
16 |
Correct |
29 ms |
18748 KB |
Output is correct |
17 |
Correct |
32 ms |
18748 KB |
Output is correct |
18 |
Correct |
31 ms |
18784 KB |
Output is correct |
19 |
Correct |
28 ms |
18748 KB |
Output is correct |
20 |
Correct |
28 ms |
18740 KB |
Output is correct |
21 |
Correct |
31 ms |
18740 KB |
Output is correct |
22 |
Correct |
31 ms |
18780 KB |
Output is correct |
23 |
Correct |
28 ms |
18748 KB |
Output is correct |
24 |
Correct |
34 ms |
18808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
18740 KB |
Output is correct |
2 |
Correct |
31 ms |
18740 KB |
Output is correct |
3 |
Correct |
30 ms |
18748 KB |
Output is correct |
4 |
Correct |
28 ms |
18748 KB |
Output is correct |
5 |
Correct |
29 ms |
18808 KB |
Output is correct |
6 |
Correct |
31 ms |
18740 KB |
Output is correct |
7 |
Correct |
32 ms |
18740 KB |
Output is correct |
8 |
Correct |
32 ms |
18740 KB |
Output is correct |
9 |
Correct |
34 ms |
18640 KB |
Output is correct |
10 |
Correct |
28 ms |
18716 KB |
Output is correct |
11 |
Correct |
32 ms |
18740 KB |
Output is correct |
12 |
Correct |
29 ms |
18740 KB |
Output is correct |
13 |
Correct |
27 ms |
18740 KB |
Output is correct |
14 |
Correct |
34 ms |
18836 KB |
Output is correct |
15 |
Correct |
30 ms |
18744 KB |
Output is correct |
16 |
Correct |
29 ms |
18748 KB |
Output is correct |
17 |
Correct |
32 ms |
18748 KB |
Output is correct |
18 |
Correct |
31 ms |
18784 KB |
Output is correct |
19 |
Correct |
28 ms |
18748 KB |
Output is correct |
20 |
Correct |
28 ms |
18740 KB |
Output is correct |
21 |
Correct |
31 ms |
18740 KB |
Output is correct |
22 |
Correct |
31 ms |
18780 KB |
Output is correct |
23 |
Correct |
28 ms |
18748 KB |
Output is correct |
24 |
Correct |
34 ms |
18808 KB |
Output is correct |
25 |
Correct |
32 ms |
18768 KB |
Output is correct |
26 |
Correct |
148 ms |
27208 KB |
Output is correct |
27 |
Correct |
109 ms |
29476 KB |
Output is correct |
28 |
Correct |
112 ms |
28652 KB |
Output is correct |
29 |
Correct |
102 ms |
26272 KB |
Output is correct |
30 |
Correct |
122 ms |
27428 KB |
Output is correct |
31 |
Correct |
32 ms |
18740 KB |
Output is correct |
32 |
Correct |
153 ms |
47908 KB |
Output is correct |
33 |
Correct |
29 ms |
18748 KB |
Output is correct |
34 |
Correct |
94 ms |
26856 KB |
Output is correct |
35 |
Correct |
100 ms |
27068 KB |
Output is correct |
36 |
Correct |
123 ms |
27172 KB |
Output is correct |
37 |
Correct |
129 ms |
27060 KB |
Output is correct |
38 |
Correct |
31 ms |
18740 KB |
Output is correct |
39 |
Correct |
139 ms |
27424 KB |
Output is correct |
40 |
Correct |
116 ms |
27496 KB |
Output is correct |
41 |
Correct |
122 ms |
27172 KB |
Output is correct |
42 |
Correct |
112 ms |
27168 KB |
Output is correct |
43 |
Correct |
101 ms |
29732 KB |
Output is correct |
44 |
Correct |
31 ms |
18740 KB |
Output is correct |
45 |
Correct |
112 ms |
34588 KB |
Output is correct |
46 |
Correct |
162 ms |
27172 KB |
Output is correct |
47 |
Correct |
30 ms |
18740 KB |
Output is correct |
48 |
Correct |
123 ms |
27008 KB |
Output is correct |
49 |
Correct |
94 ms |
26660 KB |
Output is correct |
50 |
Correct |
117 ms |
49696 KB |
Output is correct |
51 |
Correct |
120 ms |
26916 KB |
Output is correct |
52 |
Correct |
108 ms |
27152 KB |
Output is correct |
53 |
Correct |
113 ms |
26916 KB |
Output is correct |
54 |
Correct |
119 ms |
27176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
18740 KB |
Output is correct |
2 |
Correct |
31 ms |
18740 KB |
Output is correct |
3 |
Correct |
30 ms |
18748 KB |
Output is correct |
4 |
Correct |
28 ms |
18748 KB |
Output is correct |
5 |
Correct |
29 ms |
18808 KB |
Output is correct |
6 |
Correct |
31 ms |
18740 KB |
Output is correct |
7 |
Correct |
32 ms |
18740 KB |
Output is correct |
8 |
Correct |
32 ms |
18740 KB |
Output is correct |
9 |
Correct |
34 ms |
18640 KB |
Output is correct |
10 |
Correct |
28 ms |
18716 KB |
Output is correct |
11 |
Correct |
32 ms |
18740 KB |
Output is correct |
12 |
Correct |
29 ms |
18740 KB |
Output is correct |
13 |
Correct |
27 ms |
18740 KB |
Output is correct |
14 |
Correct |
34 ms |
18836 KB |
Output is correct |
15 |
Correct |
30 ms |
18744 KB |
Output is correct |
16 |
Correct |
29 ms |
18748 KB |
Output is correct |
17 |
Correct |
32 ms |
18748 KB |
Output is correct |
18 |
Correct |
31 ms |
18784 KB |
Output is correct |
19 |
Correct |
28 ms |
18748 KB |
Output is correct |
20 |
Correct |
28 ms |
18740 KB |
Output is correct |
21 |
Correct |
31 ms |
18740 KB |
Output is correct |
22 |
Correct |
31 ms |
18780 KB |
Output is correct |
23 |
Correct |
28 ms |
18748 KB |
Output is correct |
24 |
Correct |
34 ms |
18808 KB |
Output is correct |
25 |
Correct |
31 ms |
18776 KB |
Output is correct |
26 |
Correct |
358 ms |
50720 KB |
Output is correct |
27 |
Correct |
414 ms |
53032 KB |
Output is correct |
28 |
Correct |
109 ms |
29476 KB |
Output is correct |
29 |
Correct |
30 ms |
18740 KB |
Output is correct |
30 |
Correct |
31 ms |
18748 KB |
Output is correct |
31 |
Correct |
430 ms |
52356 KB |
Output is correct |
32 |
Correct |
96 ms |
26660 KB |
Output is correct |
33 |
Runtime error |
377 ms |
262144 KB |
Execution killed with signal 9 |
34 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
363 ms |
49712 KB |
Output is correct |
2 |
Correct |
319 ms |
50944 KB |
Output is correct |
3 |
Correct |
132 ms |
26832 KB |
Output is correct |
4 |
Correct |
111 ms |
29732 KB |
Output is correct |
5 |
Correct |
31 ms |
18748 KB |
Output is correct |
6 |
Correct |
29 ms |
18740 KB |
Output is correct |
7 |
Correct |
98 ms |
26876 KB |
Output is correct |
8 |
Correct |
525 ms |
62452 KB |
Output is correct |
9 |
Correct |
118 ms |
27172 KB |
Output is correct |
10 |
Correct |
466 ms |
52796 KB |
Output is correct |
11 |
Correct |
30 ms |
18748 KB |
Output is correct |
12 |
Correct |
430 ms |
59264 KB |
Output is correct |
13 |
Correct |
543 ms |
63688 KB |
Output is correct |
14 |
Correct |
336 ms |
50720 KB |
Output is correct |
15 |
Correct |
346 ms |
50720 KB |
Output is correct |
16 |
Correct |
111 ms |
26800 KB |
Output is correct |
17 |
Correct |
31 ms |
18740 KB |
Output is correct |
18 |
Correct |
370 ms |
52736 KB |
Output is correct |
19 |
Runtime error |
446 ms |
262144 KB |
Execution killed with signal 9 |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
18740 KB |
Output is correct |
2 |
Correct |
31 ms |
18740 KB |
Output is correct |
3 |
Correct |
30 ms |
18748 KB |
Output is correct |
4 |
Correct |
28 ms |
18748 KB |
Output is correct |
5 |
Correct |
29 ms |
18808 KB |
Output is correct |
6 |
Correct |
31 ms |
18740 KB |
Output is correct |
7 |
Correct |
32 ms |
18740 KB |
Output is correct |
8 |
Correct |
32 ms |
18740 KB |
Output is correct |
9 |
Correct |
34 ms |
18640 KB |
Output is correct |
10 |
Correct |
28 ms |
18716 KB |
Output is correct |
11 |
Correct |
32 ms |
18740 KB |
Output is correct |
12 |
Correct |
29 ms |
18740 KB |
Output is correct |
13 |
Correct |
27 ms |
18740 KB |
Output is correct |
14 |
Correct |
34 ms |
18836 KB |
Output is correct |
15 |
Correct |
30 ms |
18744 KB |
Output is correct |
16 |
Correct |
29 ms |
18748 KB |
Output is correct |
17 |
Correct |
32 ms |
18748 KB |
Output is correct |
18 |
Correct |
31 ms |
18784 KB |
Output is correct |
19 |
Correct |
28 ms |
18748 KB |
Output is correct |
20 |
Correct |
28 ms |
18740 KB |
Output is correct |
21 |
Correct |
31 ms |
18740 KB |
Output is correct |
22 |
Correct |
31 ms |
18780 KB |
Output is correct |
23 |
Correct |
28 ms |
18748 KB |
Output is correct |
24 |
Correct |
34 ms |
18808 KB |
Output is correct |
25 |
Correct |
32 ms |
18768 KB |
Output is correct |
26 |
Correct |
148 ms |
27208 KB |
Output is correct |
27 |
Correct |
109 ms |
29476 KB |
Output is correct |
28 |
Correct |
112 ms |
28652 KB |
Output is correct |
29 |
Correct |
102 ms |
26272 KB |
Output is correct |
30 |
Correct |
122 ms |
27428 KB |
Output is correct |
31 |
Correct |
32 ms |
18740 KB |
Output is correct |
32 |
Correct |
153 ms |
47908 KB |
Output is correct |
33 |
Correct |
29 ms |
18748 KB |
Output is correct |
34 |
Correct |
94 ms |
26856 KB |
Output is correct |
35 |
Correct |
100 ms |
27068 KB |
Output is correct |
36 |
Correct |
123 ms |
27172 KB |
Output is correct |
37 |
Correct |
129 ms |
27060 KB |
Output is correct |
38 |
Correct |
31 ms |
18740 KB |
Output is correct |
39 |
Correct |
139 ms |
27424 KB |
Output is correct |
40 |
Correct |
116 ms |
27496 KB |
Output is correct |
41 |
Correct |
122 ms |
27172 KB |
Output is correct |
42 |
Correct |
112 ms |
27168 KB |
Output is correct |
43 |
Correct |
101 ms |
29732 KB |
Output is correct |
44 |
Correct |
31 ms |
18740 KB |
Output is correct |
45 |
Correct |
112 ms |
34588 KB |
Output is correct |
46 |
Correct |
162 ms |
27172 KB |
Output is correct |
47 |
Correct |
30 ms |
18740 KB |
Output is correct |
48 |
Correct |
123 ms |
27008 KB |
Output is correct |
49 |
Correct |
94 ms |
26660 KB |
Output is correct |
50 |
Correct |
117 ms |
49696 KB |
Output is correct |
51 |
Correct |
120 ms |
26916 KB |
Output is correct |
52 |
Correct |
108 ms |
27152 KB |
Output is correct |
53 |
Correct |
113 ms |
26916 KB |
Output is correct |
54 |
Correct |
119 ms |
27176 KB |
Output is correct |
55 |
Correct |
31 ms |
18776 KB |
Output is correct |
56 |
Correct |
358 ms |
50720 KB |
Output is correct |
57 |
Correct |
414 ms |
53032 KB |
Output is correct |
58 |
Correct |
109 ms |
29476 KB |
Output is correct |
59 |
Correct |
30 ms |
18740 KB |
Output is correct |
60 |
Correct |
31 ms |
18748 KB |
Output is correct |
61 |
Correct |
430 ms |
52356 KB |
Output is correct |
62 |
Correct |
96 ms |
26660 KB |
Output is correct |
63 |
Runtime error |
377 ms |
262144 KB |
Execution killed with signal 9 |
64 |
Halted |
0 ms |
0 KB |
- |