답안 #1045489

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1045489 2024-08-06T03:13:37 Z RauPro Mergers (JOI19_mergers) PyPy 3
0 / 100
3000 ms 49440 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):
    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+1)
up = [0] * (n+1)
depth = [0] * (n+1)

for _ in range(n - 1):
    a, b = map(int, input().split())
    graf[a].append(b)
    graf[b].append(a)

setup(1, -1)

groups = [[] for i in range(n+1)]
for i in range(n):
    a = int(input())
    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 27 ms 18740 KB Output is correct
2 Correct 30 ms 18748 KB Output is correct
3 Execution timed out 3044 ms 19492 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 18740 KB Output is correct
2 Correct 30 ms 18748 KB Output is correct
3 Execution timed out 3044 ms 19492 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 18740 KB Output is correct
2 Correct 30 ms 18748 KB Output is correct
3 Execution timed out 3044 ms 19492 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 355 ms 49440 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 27 ms 18740 KB Output is correct
2 Correct 30 ms 18748 KB Output is correct
3 Execution timed out 3044 ms 19492 KB Time limit exceeded
4 Halted 0 ms 0 KB -