結果
問題 | No.416 旅行会社 |
ユーザー | はむ吉🐹 |
提出日時 | 2016-08-27 19:45:14 |
言語 | Python3 (3.12.2 + numpy 1.26.4 + scipy 1.12.0) |
結果 |
AC
|
実行時間 | 2,668 ms / 4,000 ms |
コード長 | 2,435 bytes |
コンパイル時間 | 130 ms |
コンパイル使用メモリ | 12,800 KB |
実行使用メモリ | 104,604 KB |
最終ジャッジ日時 | 2024-05-08 15:09:25 |
合計ジャッジ時間 | 27,258 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 1,398 ms
75,120 KB |
testcase_01 | AC | 31 ms
11,136 KB |
testcase_02 | AC | 33 ms
11,008 KB |
testcase_03 | AC | 31 ms
11,008 KB |
testcase_04 | AC | 32 ms
11,136 KB |
testcase_05 | AC | 31 ms
11,008 KB |
testcase_06 | AC | 33 ms
11,008 KB |
testcase_07 | AC | 39 ms
11,392 KB |
testcase_08 | AC | 62 ms
12,544 KB |
testcase_09 | AC | 180 ms
17,744 KB |
testcase_10 | AC | 1,409 ms
75,308 KB |
testcase_11 | AC | 1,447 ms
75,148 KB |
testcase_12 | AC | 1,481 ms
75,128 KB |
testcase_13 | AC | 1,440 ms
75,300 KB |
testcase_14 | AC | 2,668 ms
104,376 KB |
testcase_15 | AC | 2,614 ms
104,220 KB |
testcase_16 | AC | 2,621 ms
104,048 KB |
testcase_17 | AC | 2,633 ms
104,604 KB |
testcase_18 | AC | 2,601 ms
104,336 KB |
testcase_19 | AC | 1,683 ms
83,512 KB |
testcase_20 | AC | 1,738 ms
83,520 KB |
ソースコード
#!/usr/bin/env python3 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()