結果
問題 | No.416 旅行会社 |
ユーザー | 双六 |
提出日時 | 2020-08-10 04:01:26 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 570 ms / 4,000 ms |
コード長 | 2,149 bytes |
コンパイル時間 | 199 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 123,396 KB |
最終ジャッジ日時 | 2024-05-08 15:56:32 |
合計ジャッジ時間 | 7,241 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 212 ms
96,260 KB |
testcase_01 | AC | 45 ms
54,016 KB |
testcase_02 | AC | 50 ms
54,400 KB |
testcase_03 | AC | 44 ms
54,016 KB |
testcase_04 | AC | 44 ms
54,784 KB |
testcase_05 | AC | 45 ms
54,912 KB |
testcase_06 | AC | 46 ms
54,912 KB |
testcase_07 | AC | 61 ms
66,048 KB |
testcase_08 | AC | 109 ms
78,208 KB |
testcase_09 | AC | 203 ms
79,872 KB |
testcase_10 | AC | 221 ms
96,692 KB |
testcase_11 | AC | 231 ms
96,440 KB |
testcase_12 | AC | 246 ms
96,440 KB |
testcase_13 | AC | 212 ms
96,380 KB |
testcase_14 | AC | 525 ms
123,292 KB |
testcase_15 | AC | 526 ms
123,396 KB |
testcase_16 | AC | 534 ms
123,148 KB |
testcase_17 | AC | 558 ms
123,392 KB |
testcase_18 | AC | 570 ms
123,012 KB |
testcase_19 | AC | 485 ms
101,484 KB |
testcase_20 | AC | 496 ms
101,608 KB |
ソースコード
import sys; input = sys.stdin.buffer.readline sys.setrecursionlimit(10**7) from collections import defaultdict con = 10 ** 9 + 7; INF = float("inf") def getlist(): return list(map(int, input().split())) class UnionFind: def __init__(self, n): self.par = [i for i in range(n + 1)] self.rank = [0] * (n + 1) self.cnt = [INF] * (n + 1) self.cnt[0] = -1 def ansfind(self, x): while True: if self.cnt[x] != INF: return self.cnt[x] else: x = self.par[x] if x == self.par[x] and self.cnt[x] == INF: return INF def find(self, x): while True: if self.par[x] == x: return x else: x = self.par[x] # if self.par[x] == x: # return x # else: # self.par[x] = self.find(self.par[x]) # return self.par[x] def same_check(self, x, y): return self.find(x) == self.find(y) def union(self, x, y, itr): x2 = self.find(x); y2 = self.find(y) root = self.find(0) if x2 != root and y2 != root: if self.rank[x2] < self.rank[y2]: self.par[x2] = y2 else: self.par[y2] = x2 if self.rank[x2] == self.rank[y2]: self.rank[x2] += 1 return if y2 == root: x2, y2, x, y = y2, x2, y, x if self.rank[x2] < self.rank[y2]: self.par[x2] = y2 self.cnt[y2] = itr else: self.par[y2] = x2 self.cnt[y2] = itr if self.rank[x2] == self.rank[y2]: self.rank[x2] += 1 return #処理内容 def main(): N, M, Q = getlist() UF = UnionFind(N) edge = defaultdict(int) for i in range(M): A, B = getlist() A -= 1; B -= 1 edge[A * (10 ** 6) + B] = 1 bridge = [] for i in range(Q): C, D = getlist() C -= 1; D -= 1 bridge.append([C, D]) edge[C * (10 ** 6) + D] = 0 for key, value in edge.items(): if value == 1: a, b = int(key // (10 ** 6)), key % (10 ** 6) if not UF.same_check(a, b): UF.union(a, b, -1) # print(UF.par) for i in range(Q): c, d = bridge[-1 - i] if not UF.same_check(c, d): UF.union(c, d, i) # print(UF.cnt) # print(UF.par) for i in range(1, N): anspre = UF.ansfind(i) if anspre == INF: print(0) elif anspre == -1: print(-1) else: print(Q - anspre) if __name__ == '__main__': main()