답안 #1045500

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1045500 2024-08-06T03:31:15 Z RauPro Mergers (JOI19_mergers) PyPy 3
100 / 100
2003 ms 164084 KB
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):
    stack = [(v, parent)]

    while stack:
        v, parent = stack.pop()

        for p in graf[v]:
            if p == parent:
                continue
            depth[p] = depth[v] + 1
            up[p] = v
            stack.append((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 33 ms 18744 KB Output is correct
2 Correct 33 ms 18808 KB Output is correct
3 Correct 30 ms 18740 KB Output is correct
4 Correct 28 ms 18888 KB Output is correct
5 Correct 28 ms 18796 KB Output is correct
6 Correct 35 ms 18748 KB Output is correct
7 Correct 26 ms 18740 KB Output is correct
8 Correct 32 ms 18704 KB Output is correct
9 Correct 30 ms 18748 KB Output is correct
10 Correct 26 ms 18736 KB Output is correct
11 Correct 29 ms 18748 KB Output is correct
12 Correct 29 ms 18748 KB Output is correct
13 Correct 28 ms 18748 KB Output is correct
14 Correct 30 ms 18720 KB Output is correct
15 Correct 30 ms 18748 KB Output is correct
16 Correct 25 ms 18740 KB Output is correct
17 Correct 32 ms 18748 KB Output is correct
18 Correct 37 ms 18680 KB Output is correct
19 Correct 31 ms 18740 KB Output is correct
20 Correct 29 ms 18748 KB Output is correct
21 Correct 31 ms 18740 KB Output is correct
22 Correct 30 ms 18748 KB Output is correct
23 Correct 27 ms 18740 KB Output is correct
24 Correct 27 ms 18748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 18744 KB Output is correct
2 Correct 33 ms 18808 KB Output is correct
3 Correct 30 ms 18740 KB Output is correct
4 Correct 28 ms 18888 KB Output is correct
5 Correct 28 ms 18796 KB Output is correct
6 Correct 35 ms 18748 KB Output is correct
7 Correct 26 ms 18740 KB Output is correct
8 Correct 32 ms 18704 KB Output is correct
9 Correct 30 ms 18748 KB Output is correct
10 Correct 26 ms 18736 KB Output is correct
11 Correct 29 ms 18748 KB Output is correct
12 Correct 29 ms 18748 KB Output is correct
13 Correct 28 ms 18748 KB Output is correct
14 Correct 30 ms 18720 KB Output is correct
15 Correct 30 ms 18748 KB Output is correct
16 Correct 25 ms 18740 KB Output is correct
17 Correct 32 ms 18748 KB Output is correct
18 Correct 37 ms 18680 KB Output is correct
19 Correct 31 ms 18740 KB Output is correct
20 Correct 29 ms 18748 KB Output is correct
21 Correct 31 ms 18740 KB Output is correct
22 Correct 30 ms 18748 KB Output is correct
23 Correct 27 ms 18740 KB Output is correct
24 Correct 27 ms 18748 KB Output is correct
25 Correct 31 ms 18736 KB Output is correct
26 Correct 117 ms 26680 KB Output is correct
27 Correct 107 ms 26972 KB Output is correct
28 Correct 108 ms 26916 KB Output is correct
29 Correct 93 ms 26652 KB Output is correct
30 Correct 142 ms 27684 KB Output is correct
31 Correct 28 ms 18708 KB Output is correct
32 Correct 112 ms 26660 KB Output is correct
33 Correct 29 ms 18748 KB Output is correct
34 Correct 97 ms 26700 KB Output is correct
35 Correct 93 ms 26660 KB Output is correct
36 Correct 116 ms 26916 KB Output is correct
37 Correct 117 ms 27156 KB Output is correct
38 Correct 30 ms 18732 KB Output is correct
39 Correct 120 ms 26916 KB Output is correct
40 Correct 117 ms 27120 KB Output is correct
41 Correct 112 ms 27376 KB Output is correct
42 Correct 113 ms 26664 KB Output is correct
43 Correct 128 ms 26616 KB Output is correct
44 Correct 31 ms 18748 KB Output is correct
45 Correct 101 ms 26888 KB Output is correct
46 Correct 121 ms 27048 KB Output is correct
47 Correct 29 ms 18704 KB Output is correct
48 Correct 121 ms 27684 KB Output is correct
49 Correct 107 ms 26568 KB Output is correct
50 Correct 97 ms 26660 KB Output is correct
51 Correct 118 ms 26900 KB Output is correct
52 Correct 104 ms 26916 KB Output is correct
53 Correct 113 ms 26660 KB Output is correct
54 Correct 106 ms 26660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 18744 KB Output is correct
2 Correct 33 ms 18808 KB Output is correct
3 Correct 30 ms 18740 KB Output is correct
4 Correct 28 ms 18888 KB Output is correct
5 Correct 28 ms 18796 KB Output is correct
6 Correct 35 ms 18748 KB Output is correct
7 Correct 26 ms 18740 KB Output is correct
8 Correct 32 ms 18704 KB Output is correct
9 Correct 30 ms 18748 KB Output is correct
10 Correct 26 ms 18736 KB Output is correct
11 Correct 29 ms 18748 KB Output is correct
12 Correct 29 ms 18748 KB Output is correct
13 Correct 28 ms 18748 KB Output is correct
14 Correct 30 ms 18720 KB Output is correct
15 Correct 30 ms 18748 KB Output is correct
16 Correct 25 ms 18740 KB Output is correct
17 Correct 32 ms 18748 KB Output is correct
18 Correct 37 ms 18680 KB Output is correct
19 Correct 31 ms 18740 KB Output is correct
20 Correct 29 ms 18748 KB Output is correct
21 Correct 31 ms 18740 KB Output is correct
22 Correct 30 ms 18748 KB Output is correct
23 Correct 27 ms 18740 KB Output is correct
24 Correct 27 ms 18748 KB Output is correct
25 Correct 29 ms 18720 KB Output is correct
26 Correct 396 ms 50908 KB Output is correct
27 Correct 398 ms 51748 KB Output is correct
28 Correct 114 ms 26916 KB Output is correct
29 Correct 32 ms 18748 KB Output is correct
30 Correct 35 ms 18840 KB Output is correct
31 Correct 419 ms 50508 KB Output is correct
32 Correct 101 ms 26660 KB Output is correct
33 Correct 364 ms 49204 KB Output is correct
34 Correct 410 ms 52288 KB Output is correct
35 Correct 126 ms 26912 KB Output is correct
36 Correct 363 ms 50432 KB Output is correct
37 Correct 116 ms 27172 KB Output is correct
38 Correct 112 ms 27168 KB Output is correct
39 Correct 366 ms 50316 KB Output is correct
40 Correct 121 ms 26916 KB Output is correct
41 Correct 343 ms 49444 KB Output is correct
42 Correct 363 ms 50696 KB Output is correct
43 Correct 31 ms 18812 KB Output is correct
44 Correct 332 ms 47984 KB Output is correct
45 Correct 386 ms 53220 KB Output is correct
46 Correct 106 ms 26916 KB Output is correct
47 Correct 107 ms 26788 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 358 ms 50984 KB Output is correct
2 Correct 311 ms 50468 KB Output is correct
3 Correct 116 ms 26660 KB Output is correct
4 Correct 118 ms 26820 KB Output is correct
5 Correct 33 ms 18884 KB Output is correct
6 Correct 28 ms 18748 KB Output is correct
7 Correct 96 ms 26660 KB Output is correct
8 Correct 524 ms 56676 KB Output is correct
9 Correct 120 ms 26916 KB Output is correct
10 Correct 419 ms 51364 KB Output is correct
11 Correct 32 ms 18736 KB Output is correct
12 Correct 409 ms 51232 KB Output is correct
13 Correct 517 ms 57916 KB Output is correct
14 Correct 303 ms 47036 KB Output is correct
15 Correct 352 ms 49928 KB Output is correct
16 Correct 125 ms 26656 KB Output is correct
17 Correct 30 ms 18748 KB Output is correct
18 Correct 372 ms 51756 KB Output is correct
19 Correct 341 ms 47760 KB Output is correct
20 Correct 112 ms 27172 KB Output is correct
21 Correct 31 ms 18748 KB Output is correct
22 Correct 408 ms 51752 KB Output is correct
23 Correct 121 ms 27684 KB Output is correct
24 Correct 470 ms 52156 KB Output is correct
25 Correct 313 ms 48676 KB Output is correct
26 Correct 122 ms 26636 KB Output is correct
27 Correct 97 ms 26656 KB Output is correct
28 Correct 124 ms 26916 KB Output is correct
29 Correct 131 ms 26916 KB Output is correct
30 Correct 116 ms 26908 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 33 ms 18744 KB Output is correct
2 Correct 33 ms 18808 KB Output is correct
3 Correct 30 ms 18740 KB Output is correct
4 Correct 28 ms 18888 KB Output is correct
5 Correct 28 ms 18796 KB Output is correct
6 Correct 35 ms 18748 KB Output is correct
7 Correct 26 ms 18740 KB Output is correct
8 Correct 32 ms 18704 KB Output is correct
9 Correct 30 ms 18748 KB Output is correct
10 Correct 26 ms 18736 KB Output is correct
11 Correct 29 ms 18748 KB Output is correct
12 Correct 29 ms 18748 KB Output is correct
13 Correct 28 ms 18748 KB Output is correct
14 Correct 30 ms 18720 KB Output is correct
15 Correct 30 ms 18748 KB Output is correct
16 Correct 25 ms 18740 KB Output is correct
17 Correct 32 ms 18748 KB Output is correct
18 Correct 37 ms 18680 KB Output is correct
19 Correct 31 ms 18740 KB Output is correct
20 Correct 29 ms 18748 KB Output is correct
21 Correct 31 ms 18740 KB Output is correct
22 Correct 30 ms 18748 KB Output is correct
23 Correct 27 ms 18740 KB Output is correct
24 Correct 27 ms 18748 KB Output is correct
25 Correct 31 ms 18736 KB Output is correct
26 Correct 117 ms 26680 KB Output is correct
27 Correct 107 ms 26972 KB Output is correct
28 Correct 108 ms 26916 KB Output is correct
29 Correct 93 ms 26652 KB Output is correct
30 Correct 142 ms 27684 KB Output is correct
31 Correct 28 ms 18708 KB Output is correct
32 Correct 112 ms 26660 KB Output is correct
33 Correct 29 ms 18748 KB Output is correct
34 Correct 97 ms 26700 KB Output is correct
35 Correct 93 ms 26660 KB Output is correct
36 Correct 116 ms 26916 KB Output is correct
37 Correct 117 ms 27156 KB Output is correct
38 Correct 30 ms 18732 KB Output is correct
39 Correct 120 ms 26916 KB Output is correct
40 Correct 117 ms 27120 KB Output is correct
41 Correct 112 ms 27376 KB Output is correct
42 Correct 113 ms 26664 KB Output is correct
43 Correct 128 ms 26616 KB Output is correct
44 Correct 31 ms 18748 KB Output is correct
45 Correct 101 ms 26888 KB Output is correct
46 Correct 121 ms 27048 KB Output is correct
47 Correct 29 ms 18704 KB Output is correct
48 Correct 121 ms 27684 KB Output is correct
49 Correct 107 ms 26568 KB Output is correct
50 Correct 97 ms 26660 KB Output is correct
51 Correct 118 ms 26900 KB Output is correct
52 Correct 104 ms 26916 KB Output is correct
53 Correct 113 ms 26660 KB Output is correct
54 Correct 106 ms 26660 KB Output is correct
55 Correct 29 ms 18720 KB Output is correct
56 Correct 396 ms 50908 KB Output is correct
57 Correct 398 ms 51748 KB Output is correct
58 Correct 114 ms 26916 KB Output is correct
59 Correct 32 ms 18748 KB Output is correct
60 Correct 35 ms 18840 KB Output is correct
61 Correct 419 ms 50508 KB Output is correct
62 Correct 101 ms 26660 KB Output is correct
63 Correct 364 ms 49204 KB Output is correct
64 Correct 410 ms 52288 KB Output is correct
65 Correct 126 ms 26912 KB Output is correct
66 Correct 363 ms 50432 KB Output is correct
67 Correct 116 ms 27172 KB Output is correct
68 Correct 112 ms 27168 KB Output is correct
69 Correct 366 ms 50316 KB Output is correct
70 Correct 121 ms 26916 KB Output is correct
71 Correct 343 ms 49444 KB Output is correct
72 Correct 363 ms 50696 KB Output is correct
73 Correct 31 ms 18812 KB Output is correct
74 Correct 332 ms 47984 KB Output is correct
75 Correct 386 ms 53220 KB Output is correct
76 Correct 106 ms 26916 KB Output is correct
77 Correct 107 ms 26788 KB Output is correct
78 Correct 358 ms 50984 KB Output is correct
79 Correct 311 ms 50468 KB Output is correct
80 Correct 116 ms 26660 KB Output is correct
81 Correct 118 ms 26820 KB Output is correct
82 Correct 33 ms 18884 KB Output is correct
83 Correct 28 ms 18748 KB Output is correct
84 Correct 96 ms 26660 KB Output is correct
85 Correct 524 ms 56676 KB Output is correct
86 Correct 120 ms 26916 KB Output is correct
87 Correct 419 ms 51364 KB Output is correct
88 Correct 32 ms 18736 KB Output is correct
89 Correct 409 ms 51232 KB Output is correct
90 Correct 517 ms 57916 KB Output is correct
91 Correct 303 ms 47036 KB Output is correct
92 Correct 352 ms 49928 KB Output is correct
93 Correct 125 ms 26656 KB Output is correct
94 Correct 30 ms 18748 KB Output is correct
95 Correct 372 ms 51756 KB Output is correct
96 Correct 341 ms 47760 KB Output is correct
97 Correct 112 ms 27172 KB Output is correct
98 Correct 31 ms 18748 KB Output is correct
99 Correct 408 ms 51752 KB Output is correct
100 Correct 121 ms 27684 KB Output is correct
101 Correct 470 ms 52156 KB Output is correct
102 Correct 313 ms 48676 KB Output is correct
103 Correct 122 ms 26636 KB Output is correct
104 Correct 97 ms 26656 KB Output is correct
105 Correct 124 ms 26916 KB Output is correct
106 Correct 131 ms 26916 KB Output is correct
107 Correct 116 ms 26908 KB Output is correct
108 Correct 1760 ms 163188 KB Output is correct
109 Correct 1656 ms 151932 KB Output is correct
110 Correct 1731 ms 151564 KB Output is correct
111 Correct 1758 ms 156196 KB Output is correct
112 Correct 1850 ms 158756 KB Output is correct
113 Correct 1566 ms 162528 KB Output is correct
114 Correct 1692 ms 157100 KB Output is correct
115 Correct 1806 ms 155640 KB Output is correct
116 Correct 1937 ms 144980 KB Output is correct
117 Correct 1720 ms 156700 KB Output is correct
118 Correct 1698 ms 153520 KB Output is correct
119 Correct 1767 ms 156572 KB Output is correct
120 Correct 1777 ms 157220 KB Output is correct
121 Correct 1725 ms 158640 KB Output is correct
122 Correct 2003 ms 155936 KB Output is correct
123 Correct 1468 ms 164084 KB Output is correct
124 Correct 1838 ms 155168 KB Output is correct