結果
問題 |
No.556 仁義なきサルたち
|
ユーザー |
|
提出日時 | 2019-11-12 10:43:33 |
言語 | Python2 (2.7.18) |
結果 |
AC
|
実行時間 | 52 ms / 2,000 ms |
コード長 | 700 bytes |
コンパイル時間 | 949 ms |
コンパイル使用メモリ | 7,040 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-09-17 19:53:44 |
合計ジャッジ時間 | 2,343 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 22 |
ソースコード
#yuki556 class Unionfind: def __init__(self,N): self.par=[-1]*N def root(self,x): if self.par[x]<0: return x self.par[x]=self.root(self.par[x]) return self.par[x] def unite(self,x,y): rx,ry=self.root(x),self.root(y) if rx!=ry: rx_num,ry_num=-self.par[rx],-self.par[ry] if rx_num>ry_num or (rx_num==ry_num and rx<ry): self.par[rx]+=self.par[ry] self.par[ry]=rx elif ry_num>rx_num or (rx_num==ry_num and ry<rx): self.par[ry]+=self.par[rx] self.par[rx]=ry n,m=map(int,raw_input().split()) uf=Unionfind(n) for i in range(m): a,b=map(int,raw_input().split()) uf.unite(a-1,b-1) for i in range(n): print uf.root(i)+1