結果
問題 |
No.812 Change of Class
|
ユーザー |
![]() |
提出日時 | 2025-02-02 20:28:42 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,353 ms / 4,000 ms |
コード長 | 1,036 bytes |
コンパイル時間 | 528 ms |
コンパイル使用メモリ | 82,688 KB |
実行使用メモリ | 132,732 KB |
最終ジャッジ日時 | 2025-02-02 20:29:20 |
合計ジャッジ時間 | 33,053 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 60 |
ソースコード
from bisect import bisect_left def root(x): if P[x] < 0: return x P[x] = root(P[x]) # 経路圧縮 return P[x] def merge(x,y): x = root(x) y = root(y) if x == y: return if x > y: x,y = y,x P[x] += P[y] P[y] = x def same(x,y): return root(x) == root(y) def size(x): x = root(x) return -P[x] N,M = map(int,input().split()) UV = [list(map(int,input().split())) for _ in range(M)] P = [-1] * N L = [] tmp = 1 while tmp <= N: L.append(tmp) tmp <<= 1 E = [[] for _ in range(N)] for u, v in UV: u -= 1 v -= 1 merge(u,v) E[u].append(v) E[v].append(u) def bfs(x): Q = [x] Q2 = [] nv = [1] * N nv[x] = 0 cnt = -1 while Q: while Q: x = Q.pop() for y in E[x]: if nv[y]: nv[y] = 0 Q2.append(y) Q,Q2 = Q2,Q cnt += 1 return cnt for _ in range(int(input())): a = int(input()) a -= 1 print(size(a)-1, bisect_left(L,bfs(a)))