結果

問題 No.556 仁義なきサルたち
ユーザー pluto77pluto77
提出日時 2019-11-12 10:43:33
言語 Python2
(2.7.18)
結果
AC  
実行時間 57 ms / 2,000 ms
コード長 700 bytes
コンパイル時間 969 ms
コンパイル使用メモリ 7,172 KB
実行使用メモリ 6,960 KB
最終ジャッジ日時 2023-10-17 22:43:18
合計ジャッジ時間 2,961 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
6,492 KB
testcase_01 AC 11 ms
6,492 KB
testcase_02 AC 11 ms
6,492 KB
testcase_03 AC 11 ms
6,492 KB
testcase_04 AC 11 ms
6,492 KB
testcase_05 AC 11 ms
6,492 KB
testcase_06 AC 12 ms
6,492 KB
testcase_07 AC 12 ms
6,492 KB
testcase_08 AC 12 ms
6,492 KB
testcase_09 AC 12 ms
6,500 KB
testcase_10 AC 14 ms
6,500 KB
testcase_11 AC 15 ms
6,504 KB
testcase_12 AC 15 ms
6,504 KB
testcase_13 AC 15 ms
6,660 KB
testcase_14 AC 24 ms
6,688 KB
testcase_15 AC 30 ms
6,680 KB
testcase_16 AC 27 ms
6,900 KB
testcase_17 AC 37 ms
6,924 KB
testcase_18 AC 54 ms
6,960 KB
testcase_19 AC 52 ms
6,928 KB
testcase_20 AC 57 ms
6,928 KB
testcase_21 AC 57 ms
6,928 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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
0