結果
問題 | No.416 旅行会社 |
ユーザー | Akijin_007 |
提出日時 | 2023-12-09 17:09:45 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 869 ms / 4,000 ms |
コード長 | 2,338 bytes |
コンパイル時間 | 390 ms |
コンパイル使用メモリ | 82,432 KB |
実行使用メモリ | 130,236 KB |
最終ジャッジ日時 | 2024-09-27 03:36:41 |
合計ジャッジ時間 | 10,096 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 255 ms
110,080 KB |
testcase_01 | AC | 37 ms
52,608 KB |
testcase_02 | AC | 37 ms
53,120 KB |
testcase_03 | AC | 35 ms
53,120 KB |
testcase_04 | AC | 37 ms
52,992 KB |
testcase_05 | AC | 37 ms
53,888 KB |
testcase_06 | AC | 41 ms
54,144 KB |
testcase_07 | AC | 60 ms
68,608 KB |
testcase_08 | AC | 147 ms
78,512 KB |
testcase_09 | AC | 233 ms
81,408 KB |
testcase_10 | AC | 255 ms
109,952 KB |
testcase_11 | AC | 259 ms
112,236 KB |
testcase_12 | AC | 261 ms
111,824 KB |
testcase_13 | AC | 249 ms
112,240 KB |
testcase_14 | AC | 730 ms
130,204 KB |
testcase_15 | AC | 815 ms
130,196 KB |
testcase_16 | AC | 725 ms
130,152 KB |
testcase_17 | AC | 869 ms
130,236 KB |
testcase_18 | AC | 848 ms
129,132 KB |
testcase_19 | AC | 664 ms
106,056 KB |
testcase_20 | AC | 572 ms
106,616 KB |
ソースコード
#int(input()) #map(int, input().split()) #list(map(int, input().split())) import sys sys.setrecursionlimit(200000) class UnionFind(): def __init__(self, n): self.n = n self.parents = [-1] * 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): x = self.find(x) y = self.find(y) if x == y: return if self.parents[x] > self.parents[y]: x, y = y, x self.parents[x] += self.parents[y] self.parents[y] = x 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 len(self.roots()) def all_group_members(self): return {r: self.members(r) for r in self.roots()} def __str__(self): return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots()) N, M, Q = map(int, input().split()) A = [0] * M for i in range(M): a, b = map(int, input().split()) A[i] = (a-1, b-1) C = [0] * Q for i in range(Q): a, b = map(int, input().split()) C[i] = (a-1, b-1) u = UnionFind(N) to = [[] for i in range(N)] # print(C) sc = set(C) # print(sc) for i in range(M): if A[i] not in sc: a, b = A[i] to[a].append(b) to[b].append(a) u.union(a, b) ans = [0] * N r = u.members(0) for x in r: ans[x] = -1 v = [0] * N for i in range(Q-1, -1, -1): # print(i, u.members(0), to, v) a, b = C[i] f = 0 if u.same(0, a): f += 1 if u.same(0, b): f += 2 if f == 1 or f == 2: if f == 1: a, b = b, a v[a] = 1 ans[a] = i+1 q = [a] while q: t = q.pop() for x in to[t]: if v[x] == 0: q.append(x) v[x] = 1 ans[x] = i+1 u.union(a, b) to[a].append(b) to[b].append(a) for x in ans[1:]: print(x)