結果
問題 | No.416 旅行会社 |
ユーザー | はむ吉🐹 |
提出日時 | 2016-08-27 19:49:05 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,497 ms / 4,000 ms |
コード長 | 2,433 bytes |
コンパイル時間 | 247 ms |
コンパイル使用メモリ | 82,404 KB |
実行使用メモリ | 212,216 KB |
最終ジャッジ日時 | 2024-05-08 15:10:59 |
合計ジャッジ時間 | 15,528 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 523 ms
159,328 KB |
testcase_01 | AC | 43 ms
55,296 KB |
testcase_02 | AC | 42 ms
54,912 KB |
testcase_03 | AC | 44 ms
54,784 KB |
testcase_04 | AC | 44 ms
54,912 KB |
testcase_05 | AC | 46 ms
55,680 KB |
testcase_06 | AC | 48 ms
56,704 KB |
testcase_07 | AC | 85 ms
77,216 KB |
testcase_08 | AC | 179 ms
80,180 KB |
testcase_09 | AC | 337 ms
86,920 KB |
testcase_10 | AC | 530 ms
157,792 KB |
testcase_11 | AC | 576 ms
167,132 KB |
testcase_12 | AC | 599 ms
167,184 KB |
testcase_13 | AC | 552 ms
166,880 KB |
testcase_14 | AC | 1,497 ms
212,216 KB |
testcase_15 | AC | 1,456 ms
212,172 KB |
testcase_16 | AC | 1,400 ms
211,912 KB |
testcase_17 | AC | 1,463 ms
207,300 KB |
testcase_18 | AC | 1,488 ms
212,144 KB |
testcase_19 | AC | 1,165 ms
183,136 KB |
testcase_20 | AC | 1,101 ms
183,548 KB |
ソースコード
#!/usr/bin/env pypy3 import array import collections Edge = collections.namedtuple("Edge", "source dest") class UnionFind(object): def __init__(self, number_of_nodes): self.par = array.array("L", range(number_of_nodes)) self.rank = array.array("L", (0 for i in range(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): rest_edges = set(all_edges) - set(removed_edges) uf = UnionFind(n) con_time = array.array("l", (0 for _ in range(n))) root = 0 for source, dest in rest_edges: uf.unite(source, dest) 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 = [Edge(*(int(x) - 1 for x in input().split())) for _ in range(m)] removed_edges = [Edge(*(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()