結果

問題 No.812 Change of Class
ユーザー 👑 H20H20
提出日時 2021-02-19 14:00:54
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 822 ms / 4,000 ms
コード長 2,003 bytes
コンパイル時間 245 ms
コンパイル使用メモリ 87,148 KB
実行使用メモリ 112,160 KB
最終ジャッジ日時 2023-10-14 08:16:48
合計ジャッジ時間 23,785 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 380 ms
107,200 KB
testcase_01 AC 158 ms
81,212 KB
testcase_02 AC 263 ms
93,660 KB
testcase_03 AC 413 ms
112,160 KB
testcase_04 AC 407 ms
107,928 KB
testcase_05 AC 751 ms
97,440 KB
testcase_06 AC 597 ms
94,340 KB
testcase_07 AC 89 ms
88,172 KB
testcase_08 AC 83 ms
77,140 KB
testcase_09 AC 822 ms
101,780 KB
testcase_10 AC 784 ms
102,076 KB
testcase_11 AC 157 ms
108,444 KB
testcase_12 AC 512 ms
102,952 KB
testcase_13 AC 81 ms
71,724 KB
testcase_14 AC 81 ms
71,656 KB
testcase_15 AC 552 ms
95,080 KB
testcase_16 AC 178 ms
79,956 KB
testcase_17 AC 471 ms
95,684 KB
testcase_18 AC 433 ms
91,628 KB
testcase_19 AC 522 ms
94,256 KB
testcase_20 AC 748 ms
101,164 KB
testcase_21 AC 421 ms
90,612 KB
testcase_22 AC 287 ms
86,864 KB
testcase_23 AC 157 ms
79,352 KB
testcase_24 AC 184 ms
80,220 KB
testcase_25 AC 248 ms
85,348 KB
testcase_26 AC 628 ms
97,672 KB
testcase_27 AC 558 ms
96,392 KB
testcase_28 AC 452 ms
94,596 KB
testcase_29 AC 230 ms
81,660 KB
testcase_30 AC 526 ms
95,508 KB
testcase_31 AC 470 ms
92,520 KB
testcase_32 AC 313 ms
87,440 KB
testcase_33 AC 543 ms
97,852 KB
testcase_34 AC 178 ms
80,704 KB
testcase_35 AC 229 ms
86,176 KB
testcase_36 AC 159 ms
80,064 KB
testcase_37 AC 307 ms
87,520 KB
testcase_38 AC 118 ms
91,444 KB
testcase_39 AC 332 ms
87,784 KB
testcase_40 AC 200 ms
81,216 KB
testcase_41 AC 115 ms
89,824 KB
testcase_42 AC 517 ms
93,388 KB
testcase_43 AC 240 ms
99,464 KB
testcase_44 AC 372 ms
90,920 KB
testcase_45 AC 169 ms
80,244 KB
testcase_46 AC 239 ms
99,436 KB
testcase_47 AC 362 ms
90,156 KB
testcase_48 AC 408 ms
93,316 KB
testcase_49 AC 205 ms
81,496 KB
testcase_50 AC 113 ms
77,344 KB
testcase_51 AC 235 ms
87,892 KB
testcase_52 AC 306 ms
94,516 KB
testcase_53 AC 191 ms
80,812 KB
testcase_54 AC 168 ms
80,176 KB
testcase_55 AC 243 ms
97,044 KB
testcase_56 AC 261 ms
101,676 KB
testcase_57 AC 281 ms
102,528 KB
testcase_58 AC 204 ms
90,820 KB
testcase_59 AC 294 ms
105,644 KB
testcase_60 AC 81 ms
71,792 KB
testcase_61 AC 80 ms
71,996 KB
testcase_62 AC 81 ms
72,024 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
from collections import deque
import math

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, 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 = 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):
        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,M = map(int,input().split())
uf = UnionFind(N+1)
L = []
for i in range(N+1):
    L.append([])
for i in range(M):
    p,q = map(int,input().split())
    uf.union(p,q)
    L[p].append(q)
    L[q].append(p)

def dfs(A):
    P = [0]*(N+1)
    d = deque()
    d.append(A)
    P[A]=1
    cnt = 0
    while len(d):
        dd = deque()
        while len(d):
            for l in L[d.pop()]:
                if P[l]==0:
                    P[l]=1
                    dd.append(l)
        d=dd
        cnt+=1
    if cnt <= 1 :
        return 0
    return math.ceil(math.log2(cnt-1))




Q = int(input())
for i in range(Q):
    A = int(input())
    print(uf.size(A)-1, dfs(A))
0