結果
問題 |
No.1451 集団登校
|
ユーザー |
![]() |
提出日時 | 2024-01-17 12:13:52 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 473 ms / 2,000 ms |
コード長 | 1,725 bytes |
コンパイル時間 | 506 ms |
コンパイル使用メモリ | 82,032 KB |
実行使用メモリ | 118,656 KB |
最終ジャッジ日時 | 2024-09-28 03:11:07 |
合計ジャッジ時間 | 7,831 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 27 |
ソースコード
import sys input = sys.stdin.readline sys.setrecursionlimit(10**7) mod = 10 ** 9 + 7 inv = pow(2, mod - 2, mod) class UnionFind(): def __init__(self, n=1): self.par = [i for i in range(n)] self.rank = [0 for _ in range(n)] self.size = [1 for _ in range(n)] self.st = [set([i]) for i in range(n)] self.ans = [1 for _ in range(n)] def find(self, x): if self.par[x] == x: return x else: self.par[x] = self.find(self.par[x]) return self.par[x] def union(self, x, y): x = self.find(x) y = self.find(y) if x != y: if self.size[x] == self.size[y]: for sx in self.st[x]: self.ans[sx] *= inv self.ans[sx] %= mod for sy in self.st[y]: self.ans[sy] *= inv self.ans[sy] %= mod elif self.size[x] < self.size[y]: for sx in self.st[x]: self.ans[sx] = 0 x, y = y, x else: for sy in self.st[y]: self.ans[sy] = 0 self.par[y] = x self.size[x] += self.size[y] while self.st[y]: self.st[x].add(self.st[y].pop()) def is_same(self, x, y): return self.find(x) == self.find(y) def get_size(self, x): x = self.find(x) return self.size[x] N, M = map(int, input().split()) U = UnionFind(N) for i in range(M): a, b = map(int, input().split()) a, b = a - 1, b - 1 U.union(a, b) for a in U.ans: print(a)