結果
問題 | No.1451 集団登校 |
ユーザー |
|
提出日時 | 2021-05-03 23:05:12 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 387 ms / 2,000 ms |
コード長 | 1,414 bytes |
コンパイル時間 | 204 ms |
コンパイル使用メモリ | 82,160 KB |
実行使用メモリ | 93,472 KB |
最終ジャッジ日時 | 2024-07-22 13:17:42 |
合計ジャッジ時間 | 6,580 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 27 |
ソースコード
class Unionfind: def __init__(self,n): self.uf = [-1]*n self.leader = [[i] for i in range(n)] self.prob = [1]*n def find(self,x): if self.uf[x] < 0: return x else: self.uf[x] = self.find(self.uf[x]) return self.uf[x] def same(self,x,y): return self.find(x) == self.find(y) def union(self,x,y): x = self.find(x) y = self.find(y) if x == y: return False if self.uf[x] == self.uf[y]: for j in self.leader[x]: self.prob[j] *= 5*10**8+4 self.prob[j] %= mod for j in self.leader[y]: self.prob[j] *= 5*10**8+4 self.prob[j] %= mod self.leader[x] += self.leader[y] self.uf[x] += self.uf[y] self.uf[y] = x self.leader[y] = [] return True if self.uf[x] > self.uf[y]: x,y = y,x for j in self.leader[y]: self.prob[j] = 0 self.uf[x] += self.uf[y] self.uf[y] = x self.leader[y] = [] return True def size(self,x): x = self.find(x) return -self.uf[x] n,m = map(int,input().split()) uf = Unionfind(n) mod = 10**9+7 for i in range(m): a,b = map(int,input().split()) uf.union(a-1,b-1) for i in uf.prob: print(i)