結果
問題 | No.416 旅行会社 |
ユーザー | はむ吉🐹 |
提出日時 | 2016-08-27 20:01:45 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,187 ms / 4,000 ms |
コード長 | 2,316 bytes |
コンパイル時間 | 178 ms |
コンパイル使用メモリ | 82,432 KB |
実行使用メモリ | 207,880 KB |
最終ジャッジ日時 | 2024-05-08 15:10:43 |
合計ジャッジ時間 | 12,947 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 454 ms
167,520 KB |
testcase_01 | AC | 37 ms
52,352 KB |
testcase_02 | AC | 37 ms
52,608 KB |
testcase_03 | AC | 39 ms
52,608 KB |
testcase_04 | AC | 39 ms
53,120 KB |
testcase_05 | AC | 40 ms
53,632 KB |
testcase_06 | AC | 43 ms
53,888 KB |
testcase_07 | AC | 72 ms
74,496 KB |
testcase_08 | AC | 148 ms
78,812 KB |
testcase_09 | AC | 255 ms
83,972 KB |
testcase_10 | AC | 477 ms
167,652 KB |
testcase_11 | AC | 489 ms
167,780 KB |
testcase_12 | AC | 485 ms
157,140 KB |
testcase_13 | AC | 464 ms
167,776 KB |
testcase_14 | AC | 1,180 ms
206,600 KB |
testcase_15 | AC | 1,158 ms
207,880 KB |
testcase_16 | AC | 1,133 ms
205,312 KB |
testcase_17 | AC | 1,187 ms
205,820 KB |
testcase_18 | AC | 1,185 ms
205,584 KB |
testcase_19 | AC | 914 ms
177,700 KB |
testcase_20 | AC | 917 ms
177,832 KB |
ソースコード
#!/usr/bin/env pypy3 class UnionFind(object): def __init__(self, number_of_nodes): self.par = list(range(number_of_nodes)) self.rank = [0] * number_of_nodes self.group = [{i} for i in range(number_of_nodes)] def root(self, node): if self.par[node] == node: return node else: r = self.root(self.par[node]) self.par[node] = r return r def in_the_same_set(self, node1, node2): return self.root(node1) == self.root(node2) def unite(self, node1, node2): x = self.root(node1) y = self.root(node2) if x == y: pass elif self.rank[x] < self.rank[y]: self.par[x] = y self.group[y].update(self.group[x]) self.group[x].clear() else: self.par[y] = x self.group[x].update(self.group[y]) self.group[y].clear() if self.rank[x] == self.rank[y]: self.rank[x] += 1 def solve(n, q, all_edges, removed_edges): removed_edges_set = set(removed_edges) uf = UnionFind(n) con_time = [0 for _ in range(n)] root = 0 for edge in all_edges: if edge in removed_edges_set: continue uf.unite(*edge) for i in range(1, n): if uf.in_the_same_set(root, i): con_time[i] = -1 for t, (source, dest) in enumerate(removed_edges[::-1]): if not (uf.in_the_same_set(source, dest)): is_same_s = uf.in_the_same_set(root, source) is_same_d = uf.in_the_same_set(root, dest) if not (is_same_s or is_same_d): uf.unite(source, dest) else: if is_same_d: source, dest = dest, source for v in uf.group[uf.root(dest)]: con_time[v] = q - t uf.unite(source, dest) return con_time def main(): n, m, q = map(int, input().split()) all_edges = {tuple((int(x) - 1 for x in input().split())) for _ in range(m)} removed_edges = [tuple((int(x) - 1 for x in input().split())) for _ in range(q)] ans = solve(n, q, all_edges, removed_edges) print(*map(str, ans[1:]), sep="\n") if __name__ == '__main__': main()