結果

問題 No.2325 Skill Tree
ユーザー lloyzlloyz
提出日時 2023-06-04 21:27:45
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,004 bytes
コンパイル時間 576 ms
コンパイル使用メモリ 86,972 KB
実行使用メモリ 261,384 KB
最終ジャッジ日時 2023-08-28 08:27:44
合計ジャッジ時間 49,525 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 89 ms
71,288 KB
testcase_01 AC 89 ms
71,516 KB
testcase_02 WA -
testcase_03 AC 89 ms
71,592 KB
testcase_04 AC 88 ms
71,528 KB
testcase_05 WA -
testcase_06 AC 97 ms
71,368 KB
testcase_07 AC 588 ms
109,928 KB
testcase_08 AC 828 ms
137,624 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 980 ms
160,692 KB
testcase_12 AC 1,630 ms
261,384 KB
testcase_13 AC 1,499 ms
242,636 KB
testcase_14 AC 1,630 ms
252,136 KB
testcase_15 WA -
testcase_16 AC 1,573 ms
256,040 KB
testcase_17 AC 1,322 ms
254,048 KB
testcase_18 AC 1,360 ms
240,140 KB
testcase_19 AC 1,320 ms
253,272 KB
testcase_20 WA -
testcase_21 AC 1,311 ms
238,884 KB
testcase_22 AC 1,773 ms
240,180 KB
testcase_23 AC 1,926 ms
249,448 KB
testcase_24 AC 1,701 ms
249,880 KB
testcase_25 AC 1,588 ms
231,644 KB
testcase_26 AC 1,673 ms
246,152 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 AC 1,666 ms
240,548 KB
testcase_36 AC 1,700 ms
244,880 KB
testcase_37 AC 1,676 ms
236,760 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

class UnionFind():
    def __init__(self, n):
        '''
        UnionFindクラス。nは要素数を表す。
        '''
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        '''
        要素xを含む集合の親を見つける関数。
        '''
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        '''
        要素xを含む集合と要素yを含む集合を合体する関数。
        基本的には、要素数が多い集合に統合される。
        要素数が同じときは要素yを含む集合に統合される。
        '''
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        '''
        要素xを含む集合の要素数を出す関数。
        '''
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())

n = int(input())
L = [list(map(int, input().split())) + [i + 2] for i in range(n - 1)]
L.sort(key=lambda x: x[0])
C = [set([i]) for i in range(n + 1)]
M = [-1 for _ in range(n + 1)]
M[1] = 0 
q = int(input())
X, Y = [], []
for i in range(q):
    op, d = map(int, input().split())
    if op == 1:
        X.append((d, i))
    else:
        Y.append((d, i))
X.sort(key=lambda x: x[0])

UF = UnionFind(n + 1)
ANS = [-1 for _ in range(q)]
xi = 0
for i in range(n - 1):
    nl, x, y = L[i]
    root1 = UF.find(1)
    while xi < len(X) and X[xi][0] < nl:
        ANS[X[xi][1]] = len(C[root1])
        xi += 1
    xroot = UF.find(x)
    yroot = UF.find(y)
    UF.union(x, y)
    root = UF.find(x)
    if root == xroot:
        C[xroot] |= C[yroot]
    else:
        C[yroot] |= C[xroot]
    root1 = UF.find(1)
    if root1 == root:
        if root1 == xroot:
            for ym in C[yroot]:
                M[ym] = nl
        elif root1 == yroot:
            for xm in C[xroot]:
                M[xm] = nl
root1 = UF.find(1)
while xi < len(X):
    ANS[X[xi][1]] = len(C[root1])
    xi += 1
    if xi < len(X):
        cl = X[xi][0]
for yi in range(len(Y)):
    m, i = Y[yi]
    ANS[i] = M[m]
print(*ANS, sep='\n')
0