Submission #1045445

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

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 34 ms 18768 KB Output is correct
2 Correct 36 ms 18736 KB Output is correct
3 Correct 35 ms 18740 KB Output is correct
4 Correct 30 ms 18740 KB Output is correct
5 Correct 30 ms 18748 KB Output is correct
6 Correct 37 ms 18900 KB Output is correct
7 Correct 29 ms 18748 KB Output is correct
8 Correct 34 ms 19248 KB Output is correct
9 Correct 29 ms 18740 KB Output is correct
10 Correct 29 ms 18696 KB Output is correct
11 Correct 33 ms 18728 KB Output is correct
12 Correct 30 ms 18744 KB Output is correct
13 Correct 30 ms 18748 KB Output is correct
14 Correct 29 ms 18748 KB Output is correct
15 Correct 31 ms 18740 KB Output is correct
16 Correct 30 ms 18748 KB Output is correct
17 Correct 26 ms 18700 KB Output is correct
18 Correct 29 ms 18752 KB Output is correct
19 Correct 27 ms 18748 KB Output is correct
20 Correct 30 ms 18732 KB Output is correct
21 Correct 30 ms 18716 KB Output is correct
22 Correct 30 ms 18748 KB Output is correct
23 Correct 27 ms 18700 KB Output is correct
24 Correct 27 ms 18748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 34 ms 18768 KB Output is correct
2 Correct 36 ms 18736 KB Output is correct
3 Correct 35 ms 18740 KB Output is correct
4 Correct 30 ms 18740 KB Output is correct
5 Correct 30 ms 18748 KB Output is correct
6 Correct 37 ms 18900 KB Output is correct
7 Correct 29 ms 18748 KB Output is correct
8 Correct 34 ms 19248 KB Output is correct
9 Correct 29 ms 18740 KB Output is correct
10 Correct 29 ms 18696 KB Output is correct
11 Correct 33 ms 18728 KB Output is correct
12 Correct 30 ms 18744 KB Output is correct
13 Correct 30 ms 18748 KB Output is correct
14 Correct 29 ms 18748 KB Output is correct
15 Correct 31 ms 18740 KB Output is correct
16 Correct 30 ms 18748 KB Output is correct
17 Correct 26 ms 18700 KB Output is correct
18 Correct 29 ms 18752 KB Output is correct
19 Correct 27 ms 18748 KB Output is correct
20 Correct 30 ms 18732 KB Output is correct
21 Correct 30 ms 18716 KB Output is correct
22 Correct 30 ms 18748 KB Output is correct
23 Correct 27 ms 18700 KB Output is correct
24 Correct 27 ms 18748 KB Output is correct
25 Correct 28 ms 18748 KB Output is correct
26 Correct 128 ms 26780 KB Output is correct
27 Correct 122 ms 31424 KB Output is correct
28 Correct 110 ms 31024 KB Output is correct
29 Correct 102 ms 26420 KB Output is correct
30 Correct 126 ms 27052 KB Output is correct
31 Correct 30 ms 18748 KB Output is correct
32 Runtime error 87 ms 27692 KB Execution failed because the return code was nonzero
33 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 34 ms 18768 KB Output is correct
2 Correct 36 ms 18736 KB Output is correct
3 Correct 35 ms 18740 KB Output is correct
4 Correct 30 ms 18740 KB Output is correct
5 Correct 30 ms 18748 KB Output is correct
6 Correct 37 ms 18900 KB Output is correct
7 Correct 29 ms 18748 KB Output is correct
8 Correct 34 ms 19248 KB Output is correct
9 Correct 29 ms 18740 KB Output is correct
10 Correct 29 ms 18696 KB Output is correct
11 Correct 33 ms 18728 KB Output is correct
12 Correct 30 ms 18744 KB Output is correct
13 Correct 30 ms 18748 KB Output is correct
14 Correct 29 ms 18748 KB Output is correct
15 Correct 31 ms 18740 KB Output is correct
16 Correct 30 ms 18748 KB Output is correct
17 Correct 26 ms 18700 KB Output is correct
18 Correct 29 ms 18752 KB Output is correct
19 Correct 27 ms 18748 KB Output is correct
20 Correct 30 ms 18732 KB Output is correct
21 Correct 30 ms 18716 KB Output is correct
22 Correct 30 ms 18748 KB Output is correct
23 Correct 27 ms 18700 KB Output is correct
24 Correct 27 ms 18748 KB Output is correct
25 Correct 30 ms 18896 KB Output is correct
26 Correct 333 ms 49088 KB Output is correct
27 Correct 469 ms 59288 KB Output is correct
28 Correct 121 ms 31520 KB Output is correct
29 Correct 30 ms 18740 KB Output is correct
30 Correct 29 ms 18740 KB Output is correct
31 Correct 456 ms 59424 KB Output is correct
32 Correct 92 ms 26672 KB Output is correct
33 Runtime error 190 ms 37680 KB Execution failed because the return code was nonzero
34 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 325 ms 49188 KB Output is correct
2 Correct 313 ms 50724 KB Output is correct
3 Correct 127 ms 26928 KB Output is correct
4 Correct 131 ms 31536 KB Output is correct
5 Correct 34 ms 18740 KB Output is correct
6 Correct 33 ms 18740 KB Output is correct
7 Correct 92 ms 26672 KB Output is correct
8 Correct 613 ms 66604 KB Output is correct
9 Correct 125 ms 27200 KB Output is correct
10 Correct 526 ms 63592 KB Output is correct
11 Correct 31 ms 18740 KB Output is correct
12 Correct 429 ms 54000 KB Output is correct
13 Correct 654 ms 69676 KB Output is correct
14 Correct 320 ms 49200 KB Output is correct
15 Correct 336 ms 50020 KB Output is correct
16 Correct 109 ms 27696 KB Output is correct
17 Correct 31 ms 18824 KB Output is correct
18 Correct 353 ms 51760 KB Output is correct
19 Runtime error 199 ms 37752 KB Execution failed because the return code was nonzero
20 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 34 ms 18768 KB Output is correct
2 Correct 36 ms 18736 KB Output is correct
3 Correct 35 ms 18740 KB Output is correct
4 Correct 30 ms 18740 KB Output is correct
5 Correct 30 ms 18748 KB Output is correct
6 Correct 37 ms 18900 KB Output is correct
7 Correct 29 ms 18748 KB Output is correct
8 Correct 34 ms 19248 KB Output is correct
9 Correct 29 ms 18740 KB Output is correct
10 Correct 29 ms 18696 KB Output is correct
11 Correct 33 ms 18728 KB Output is correct
12 Correct 30 ms 18744 KB Output is correct
13 Correct 30 ms 18748 KB Output is correct
14 Correct 29 ms 18748 KB Output is correct
15 Correct 31 ms 18740 KB Output is correct
16 Correct 30 ms 18748 KB Output is correct
17 Correct 26 ms 18700 KB Output is correct
18 Correct 29 ms 18752 KB Output is correct
19 Correct 27 ms 18748 KB Output is correct
20 Correct 30 ms 18732 KB Output is correct
21 Correct 30 ms 18716 KB Output is correct
22 Correct 30 ms 18748 KB Output is correct
23 Correct 27 ms 18700 KB Output is correct
24 Correct 27 ms 18748 KB Output is correct
25 Correct 28 ms 18748 KB Output is correct
26 Correct 128 ms 26780 KB Output is correct
27 Correct 122 ms 31424 KB Output is correct
28 Correct 110 ms 31024 KB Output is correct
29 Correct 102 ms 26420 KB Output is correct
30 Correct 126 ms 27052 KB Output is correct
31 Correct 30 ms 18748 KB Output is correct
32 Runtime error 87 ms 27692 KB Execution failed because the return code was nonzero
33 Halted 0 ms 0 KB -