Submission #1045446

# Submission time Handle Problem Language Result Execution time Memory
1045446 2024-08-06T01:09:01 Z RauPro Mergers (JOI19_mergers) PyPy 3
34 / 100
645 ms 262144 KB
import sys
from collections import defaultdict
from sys import setrecursionlimit, stdin, stdout

setrecursionlimit(300000)
class DSU:
    def __init__(self, n):
        self.par = list(range(n))

    def find(self, x):
        if self.par[x] == x:
            return x
        self.par[x] = self.find(self.par[x])
        return self.par[x]

    def merge(self, a, b):
        a = self.find(a)
        b = self.find(b)
        if a != b:
            self.par[b] = 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)
# Verdict Execution time Memory Grader output
1 Correct 29 ms 18740 KB Output is correct
2 Correct 33 ms 18716 KB Output is correct
3 Correct 31 ms 18736 KB Output is correct
4 Correct 30 ms 18748 KB Output is correct
5 Correct 26 ms 18684 KB Output is correct
6 Correct 32 ms 18740 KB Output is correct
7 Correct 25 ms 18748 KB Output is correct
8 Correct 30 ms 19244 KB Output is correct
9 Correct 26 ms 18748 KB Output is correct
10 Correct 30 ms 18704 KB Output is correct
11 Correct 29 ms 18740 KB Output is correct
12 Correct 32 ms 18744 KB Output is correct
13 Correct 32 ms 18740 KB Output is correct
14 Correct 32 ms 18824 KB Output is correct
15 Correct 29 ms 18748 KB Output is correct
16 Correct 29 ms 18716 KB Output is correct
17 Correct 28 ms 18740 KB Output is correct
18 Correct 31 ms 18740 KB Output is correct
19 Correct 28 ms 18748 KB Output is correct
20 Correct 28 ms 18748 KB Output is correct
21 Correct 31 ms 18748 KB Output is correct
22 Correct 30 ms 18748 KB Output is correct
23 Correct 28 ms 18780 KB Output is correct
24 Correct 28 ms 18724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 18740 KB Output is correct
2 Correct 33 ms 18716 KB Output is correct
3 Correct 31 ms 18736 KB Output is correct
4 Correct 30 ms 18748 KB Output is correct
5 Correct 26 ms 18684 KB Output is correct
6 Correct 32 ms 18740 KB Output is correct
7 Correct 25 ms 18748 KB Output is correct
8 Correct 30 ms 19244 KB Output is correct
9 Correct 26 ms 18748 KB Output is correct
10 Correct 30 ms 18704 KB Output is correct
11 Correct 29 ms 18740 KB Output is correct
12 Correct 32 ms 18744 KB Output is correct
13 Correct 32 ms 18740 KB Output is correct
14 Correct 32 ms 18824 KB Output is correct
15 Correct 29 ms 18748 KB Output is correct
16 Correct 29 ms 18716 KB Output is correct
17 Correct 28 ms 18740 KB Output is correct
18 Correct 31 ms 18740 KB Output is correct
19 Correct 28 ms 18748 KB Output is correct
20 Correct 28 ms 18748 KB Output is correct
21 Correct 31 ms 18748 KB Output is correct
22 Correct 30 ms 18748 KB Output is correct
23 Correct 28 ms 18780 KB Output is correct
24 Correct 28 ms 18724 KB Output is correct
25 Correct 27 ms 18740 KB Output is correct
26 Correct 158 ms 26824 KB Output is correct
27 Correct 122 ms 31424 KB Output is correct
28 Correct 122 ms 31268 KB Output is correct
29 Correct 102 ms 26224 KB Output is correct
30 Correct 130 ms 27168 KB Output is correct
31 Correct 30 ms 18748 KB Output is correct
32 Correct 133 ms 49328 KB Output is correct
33 Correct 37 ms 18748 KB Output is correct
34 Correct 104 ms 26688 KB Output is correct
35 Correct 101 ms 27172 KB Output is correct
36 Correct 122 ms 27168 KB Output is correct
37 Correct 124 ms 26912 KB Output is correct
38 Correct 33 ms 18772 KB Output is correct
39 Correct 129 ms 27624 KB Output is correct
40 Correct 113 ms 27688 KB Output is correct
41 Correct 125 ms 26660 KB Output is correct
42 Correct 126 ms 27532 KB Output is correct
43 Correct 107 ms 30252 KB Output is correct
44 Correct 29 ms 18740 KB Output is correct
45 Correct 121 ms 34340 KB Output is correct
46 Correct 120 ms 26916 KB Output is correct
47 Correct 36 ms 18892 KB Output is correct
48 Correct 136 ms 26916 KB Output is correct
49 Correct 95 ms 26404 KB Output is correct
50 Correct 126 ms 49700 KB Output is correct
51 Correct 128 ms 26916 KB Output is correct
52 Correct 110 ms 26736 KB Output is correct
53 Correct 112 ms 27172 KB Output is correct
54 Correct 122 ms 27640 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 18740 KB Output is correct
2 Correct 33 ms 18716 KB Output is correct
3 Correct 31 ms 18736 KB Output is correct
4 Correct 30 ms 18748 KB Output is correct
5 Correct 26 ms 18684 KB Output is correct
6 Correct 32 ms 18740 KB Output is correct
7 Correct 25 ms 18748 KB Output is correct
8 Correct 30 ms 19244 KB Output is correct
9 Correct 26 ms 18748 KB Output is correct
10 Correct 30 ms 18704 KB Output is correct
11 Correct 29 ms 18740 KB Output is correct
12 Correct 32 ms 18744 KB Output is correct
13 Correct 32 ms 18740 KB Output is correct
14 Correct 32 ms 18824 KB Output is correct
15 Correct 29 ms 18748 KB Output is correct
16 Correct 29 ms 18716 KB Output is correct
17 Correct 28 ms 18740 KB Output is correct
18 Correct 31 ms 18740 KB Output is correct
19 Correct 28 ms 18748 KB Output is correct
20 Correct 28 ms 18748 KB Output is correct
21 Correct 31 ms 18748 KB Output is correct
22 Correct 30 ms 18748 KB Output is correct
23 Correct 28 ms 18780 KB Output is correct
24 Correct 28 ms 18724 KB Output is correct
25 Correct 31 ms 18748 KB Output is correct
26 Correct 331 ms 48736 KB Output is correct
27 Correct 454 ms 59404 KB Output is correct
28 Correct 122 ms 31524 KB Output is correct
29 Correct 34 ms 18740 KB Output is correct
30 Correct 29 ms 18768 KB Output is correct
31 Correct 450 ms 59428 KB Output is correct
32 Correct 96 ms 26640 KB Output is correct
33 Runtime error 384 ms 262144 KB Execution killed with signal 9
34 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 346 ms 49124 KB Output is correct
2 Correct 308 ms 50884 KB Output is correct
3 Correct 130 ms 26952 KB Output is correct
4 Correct 122 ms 31396 KB Output is correct
5 Correct 32 ms 18784 KB Output is correct
6 Correct 29 ms 18748 KB Output is correct
7 Correct 94 ms 26660 KB Output is correct
8 Correct 610 ms 66496 KB Output is correct
9 Correct 127 ms 27172 KB Output is correct
10 Correct 538 ms 63260 KB Output is correct
11 Correct 32 ms 18868 KB Output is correct
12 Correct 414 ms 53796 KB Output is correct
13 Correct 645 ms 69524 KB Output is correct
14 Correct 321 ms 49184 KB Output is correct
15 Correct 363 ms 49956 KB Output is correct
16 Correct 112 ms 27160 KB Output is correct
17 Correct 33 ms 18740 KB Output is correct
18 Correct 343 ms 51748 KB Output is correct
19 Runtime error 446 ms 262144 KB Execution killed with signal 9
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 29 ms 18740 KB Output is correct
2 Correct 33 ms 18716 KB Output is correct
3 Correct 31 ms 18736 KB Output is correct
4 Correct 30 ms 18748 KB Output is correct
5 Correct 26 ms 18684 KB Output is correct
6 Correct 32 ms 18740 KB Output is correct
7 Correct 25 ms 18748 KB Output is correct
8 Correct 30 ms 19244 KB Output is correct
9 Correct 26 ms 18748 KB Output is correct
10 Correct 30 ms 18704 KB Output is correct
11 Correct 29 ms 18740 KB Output is correct
12 Correct 32 ms 18744 KB Output is correct
13 Correct 32 ms 18740 KB Output is correct
14 Correct 32 ms 18824 KB Output is correct
15 Correct 29 ms 18748 KB Output is correct
16 Correct 29 ms 18716 KB Output is correct
17 Correct 28 ms 18740 KB Output is correct
18 Correct 31 ms 18740 KB Output is correct
19 Correct 28 ms 18748 KB Output is correct
20 Correct 28 ms 18748 KB Output is correct
21 Correct 31 ms 18748 KB Output is correct
22 Correct 30 ms 18748 KB Output is correct
23 Correct 28 ms 18780 KB Output is correct
24 Correct 28 ms 18724 KB Output is correct
25 Correct 27 ms 18740 KB Output is correct
26 Correct 158 ms 26824 KB Output is correct
27 Correct 122 ms 31424 KB Output is correct
28 Correct 122 ms 31268 KB Output is correct
29 Correct 102 ms 26224 KB Output is correct
30 Correct 130 ms 27168 KB Output is correct
31 Correct 30 ms 18748 KB Output is correct
32 Correct 133 ms 49328 KB Output is correct
33 Correct 37 ms 18748 KB Output is correct
34 Correct 104 ms 26688 KB Output is correct
35 Correct 101 ms 27172 KB Output is correct
36 Correct 122 ms 27168 KB Output is correct
37 Correct 124 ms 26912 KB Output is correct
38 Correct 33 ms 18772 KB Output is correct
39 Correct 129 ms 27624 KB Output is correct
40 Correct 113 ms 27688 KB Output is correct
41 Correct 125 ms 26660 KB Output is correct
42 Correct 126 ms 27532 KB Output is correct
43 Correct 107 ms 30252 KB Output is correct
44 Correct 29 ms 18740 KB Output is correct
45 Correct 121 ms 34340 KB Output is correct
46 Correct 120 ms 26916 KB Output is correct
47 Correct 36 ms 18892 KB Output is correct
48 Correct 136 ms 26916 KB Output is correct
49 Correct 95 ms 26404 KB Output is correct
50 Correct 126 ms 49700 KB Output is correct
51 Correct 128 ms 26916 KB Output is correct
52 Correct 110 ms 26736 KB Output is correct
53 Correct 112 ms 27172 KB Output is correct
54 Correct 122 ms 27640 KB Output is correct
55 Correct 31 ms 18748 KB Output is correct
56 Correct 331 ms 48736 KB Output is correct
57 Correct 454 ms 59404 KB Output is correct
58 Correct 122 ms 31524 KB Output is correct
59 Correct 34 ms 18740 KB Output is correct
60 Correct 29 ms 18768 KB Output is correct
61 Correct 450 ms 59428 KB Output is correct
62 Correct 96 ms 26640 KB Output is correct
63 Runtime error 384 ms 262144 KB Execution killed with signal 9
64 Halted 0 ms 0 KB -