結果
問題 |
No.416 旅行会社
|
ユーザー |
👑 |
提出日時 | 2022-07-08 09:54:16 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,013 ms / 4,000 ms |
コード長 | 1,996 bytes |
コンパイル時間 | 389 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 152,720 KB |
最終ジャッジ日時 | 2024-12-14 21:15:22 |
合計ジャッジ時間 | 11,575 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 21 |
ソースコード
class UnionFind: def __init__(self, n): self.n = n self.parents = [-1] * n self.group = n self.ans = [0] * n self.child = [[i] for i in range(n)] def find(self, x): if self.parents[x] < 0: return x else: self.parents[x] = self.find(self.parents[x]) return self.parents[x] def union(self, x, y, i): x = self.find(x) y = self.find(y) if x == y: return self.group -= 1 if self.parents[x] > self.parents[y]: x, y = y, x p = self.find(0) if x == p: for j in self.child[y]: self.ans[j] = i elif y == p: for j in self.child[x]: self.ans[j] = i self.parents[x] += self.parents[y] self.parents[y] = x for c in self.child[y]: self.child[x].append(c) def size(self, x): return -self.parents[self.find(x)] def same(self, x, y): return self.find(x) == self.find(y) def members(self, 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 self.group def all_group_members(self): dic = {r:[] for r in self.roots()} for i in range(self.n): dic[self.find(i)].append(i) return dic def __str__(self): return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots()) n, m, q = map(int, input().split()) edges = set(tuple(map(int, input().split())) for _ in range(m)) cd = [] for _ in range(q): c, d = map(int, input().split()) cd.append((c, d)) edges.remove((c, d)) UF = UnionFind(n) for a, b in edges: UF.union(a - 1, b - 1, -1) for i in range(q - 1, -1, -1): c, d = cd[i] UF.union(c - 1, d - 1, i + 1) print(*UF.ans[1:], sep="\n")