結果
問題 | No.556 仁義なきサルたち |
ユーザー | 👑 Kazun |
提出日時 | 2021-01-29 19:05:35 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 174 ms / 2,000 ms |
コード長 | 2,542 bytes |
コンパイル時間 | 748 ms |
コンパイル使用メモリ | 86,876 KB |
実行使用メモリ | 79,084 KB |
最終ジャッジ日時 | 2023-09-09 12:34:55 |
合計ジャッジ時間 | 3,872 ms |
ジャッジサーバーID (参考情報) |
judge12 / judge11 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 71 ms
71,212 KB |
testcase_01 | AC | 73 ms
71,244 KB |
testcase_02 | AC | 71 ms
71,112 KB |
testcase_03 | AC | 71 ms
71,176 KB |
testcase_04 | AC | 72 ms
71,260 KB |
testcase_05 | AC | 74 ms
71,132 KB |
testcase_06 | AC | 74 ms
71,236 KB |
testcase_07 | AC | 75 ms
71,056 KB |
testcase_08 | AC | 75 ms
71,244 KB |
testcase_09 | AC | 81 ms
75,860 KB |
testcase_10 | AC | 88 ms
76,040 KB |
testcase_11 | AC | 92 ms
76,096 KB |
testcase_12 | AC | 92 ms
76,536 KB |
testcase_13 | AC | 90 ms
76,604 KB |
testcase_14 | AC | 125 ms
77,476 KB |
testcase_15 | AC | 135 ms
78,068 KB |
testcase_16 | AC | 117 ms
77,776 KB |
testcase_17 | AC | 152 ms
78,052 KB |
testcase_18 | AC | 174 ms
79,084 KB |
testcase_19 | AC | 104 ms
77,844 KB |
testcase_20 | AC | 103 ms
77,244 KB |
testcase_21 | AC | 103 ms
77,332 KB |
ソースコード
class Union_Find(): def __init__(self,N): """0,1,...,n-1を要素として初期化する. N:要素数 """ self.n=N self.parents=[-1]*N self.rank=[0]*N def find(self, x): """要素xの属している族を調べる. x:要素 """ V=[] while self.parents[x]>=0: V.append(x) x=self.parents[x] for v in V: self.parents[v]=x return x def union(self, x, y): """要素x,yを同一視する. x,y:要素 """ x=self.find(x) y=self.find(y) if x==y: return if self.size(x)>self.size(y): win,lose=x,y elif self.size(x)<self.size(y): win,lose=y,x elif self.find(x)<self.find(y): win,lose=x,y else: win,lose=y,x if self.rank[x]<self.rank[y]: x,y=y,x self.parents[win]+=self.parents[lose] self.parents[lose]=win def size(self, x): """要素xの属している要素の数. x:要素 """ return -self.parents[self.find(x)] def same(self, x, y): """要素x,yは同一視されているか? x,y:要素 """ return self.find(x) == self.find(y) def members(self, x): """要素xが属している族の要素. ※族の要素の個数が欲しいときはsizeを使うこと!! 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): """全ての族の出力 """ X={r:[] for r in self.roots()} for k in range(self.n): X[self.find(k)].append(k) return X def refresh(self): for i in range(self.n): _=self.find(i) def __str__(self): return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots()) def __repr__(self): return self.__str__() #================================================ import sys input=sys.stdin.readline N,M=map(int,input().split()) U=Union_Find(N+1) for _ in range(M): A,B=map(int,input().split()) U.union(A,B) for i in range(1,N+1): print(U.find(i))