結果
問題 | No.812 Change of Class |
ユーザー | H20 |
提出日時 | 2021-02-19 14:00:54 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 960 ms / 4,000 ms |
コード長 | 2,003 bytes |
コンパイル時間 | 354 ms |
コンパイル使用メモリ | 82,432 KB |
実行使用メモリ | 111,484 KB |
最終ジャッジ日時 | 2024-09-16 03:23:47 |
合計ジャッジ時間 | 25,029 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 388 ms
106,316 KB |
testcase_01 | AC | 147 ms
78,976 KB |
testcase_02 | AC | 255 ms
92,160 KB |
testcase_03 | AC | 434 ms
111,484 KB |
testcase_04 | AC | 405 ms
107,348 KB |
testcase_05 | AC | 827 ms
96,768 KB |
testcase_06 | AC | 594 ms
91,940 KB |
testcase_07 | AC | 61 ms
73,216 KB |
testcase_08 | AC | 55 ms
64,384 KB |
testcase_09 | AC | 885 ms
100,196 KB |
testcase_10 | AC | 823 ms
99,440 KB |
testcase_11 | AC | 141 ms
107,020 KB |
testcase_12 | AC | 536 ms
100,908 KB |
testcase_13 | AC | 46 ms
54,784 KB |
testcase_14 | AC | 46 ms
54,400 KB |
testcase_15 | AC | 590 ms
92,212 KB |
testcase_16 | AC | 158 ms
78,208 KB |
testcase_17 | AC | 530 ms
93,704 KB |
testcase_18 | AC | 472 ms
90,240 KB |
testcase_19 | AC | 530 ms
91,152 KB |
testcase_20 | AC | 960 ms
98,236 KB |
testcase_21 | AC | 471 ms
88,576 KB |
testcase_22 | AC | 284 ms
83,624 KB |
testcase_23 | AC | 152 ms
78,208 KB |
testcase_24 | AC | 172 ms
78,252 KB |
testcase_25 | AC | 260 ms
83,072 KB |
testcase_26 | AC | 783 ms
94,976 KB |
testcase_27 | AC | 700 ms
93,512 KB |
testcase_28 | AC | 521 ms
91,652 KB |
testcase_29 | AC | 218 ms
79,136 KB |
testcase_30 | AC | 634 ms
92,644 KB |
testcase_31 | AC | 543 ms
90,404 KB |
testcase_32 | AC | 312 ms
85,888 KB |
testcase_33 | AC | 663 ms
93,588 KB |
testcase_34 | AC | 166 ms
78,464 KB |
testcase_35 | AC | 224 ms
83,720 KB |
testcase_36 | AC | 148 ms
78,208 KB |
testcase_37 | AC | 317 ms
85,376 KB |
testcase_38 | AC | 109 ms
90,240 KB |
testcase_39 | AC | 343 ms
86,144 KB |
testcase_40 | AC | 194 ms
78,936 KB |
testcase_41 | AC | 104 ms
88,576 KB |
testcase_42 | AC | 559 ms
91,668 KB |
testcase_43 | AC | 235 ms
97,308 KB |
testcase_44 | AC | 397 ms
88,704 KB |
testcase_45 | AC | 159 ms
78,464 KB |
testcase_46 | AC | 233 ms
97,236 KB |
testcase_47 | AC | 380 ms
88,448 KB |
testcase_48 | AC | 452 ms
90,260 KB |
testcase_49 | AC | 205 ms
79,104 KB |
testcase_50 | AC | 96 ms
76,160 KB |
testcase_51 | AC | 233 ms
86,000 KB |
testcase_52 | AC | 325 ms
92,868 KB |
testcase_53 | AC | 182 ms
78,720 KB |
testcase_54 | AC | 162 ms
78,720 KB |
testcase_55 | AC | 245 ms
95,532 KB |
testcase_56 | AC | 270 ms
98,064 KB |
testcase_57 | AC | 282 ms
102,896 KB |
testcase_58 | AC | 192 ms
87,808 KB |
testcase_59 | AC | 310 ms
110,084 KB |
testcase_60 | AC | 46 ms
54,016 KB |
testcase_61 | AC | 47 ms
54,144 KB |
testcase_62 | AC | 46 ms
54,272 KB |
ソースコード
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))