結果
問題 | No.416 旅行会社 |
ユーザー | lllllll88938494 |
提出日時 | 2023-05-11 11:56:27 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,142 ms / 4,000 ms |
コード長 | 1,854 bytes |
コンパイル時間 | 709 ms |
コンパイル使用メモリ | 86,828 KB |
実行使用メモリ | 175,564 KB |
最終ジャッジ日時 | 2023-08-18 07:28:14 |
合計ジャッジ時間 | 14,453 ms |
ジャッジサーバーID (参考情報) |
judge13 / judge12 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 418 ms
146,764 KB |
testcase_01 | AC | 72 ms
71,220 KB |
testcase_02 | AC | 74 ms
71,128 KB |
testcase_03 | AC | 72 ms
71,224 KB |
testcase_04 | AC | 74 ms
71,284 KB |
testcase_05 | AC | 75 ms
71,280 KB |
testcase_06 | AC | 76 ms
71,184 KB |
testcase_07 | AC | 97 ms
76,580 KB |
testcase_08 | AC | 173 ms
79,976 KB |
testcase_09 | AC | 297 ms
86,708 KB |
testcase_10 | AC | 444 ms
147,100 KB |
testcase_11 | AC | 439 ms
147,052 KB |
testcase_12 | AC | 450 ms
147,172 KB |
testcase_13 | AC | 417 ms
147,252 KB |
testcase_14 | AC | 1,070 ms
173,272 KB |
testcase_15 | AC | 1,142 ms
173,944 KB |
testcase_16 | AC | 1,035 ms
172,140 KB |
testcase_17 | AC | 1,127 ms
174,420 KB |
testcase_18 | AC | 1,104 ms
175,564 KB |
testcase_19 | AC | 887 ms
148,576 KB |
testcase_20 | AC | 822 ms
148,444 KB |
ソースコード
class UnionFind2: def __init__(self, n): self.n = n self.parents = [-1] * n self.family = [{i} for i in range(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 self.family[x] |= self.family[y] self.family[y] = {} def same(self,i,j): return uf.find(i) == uf.find(j) def size(self, x): return -self.parents[self.find(x)] n,m,h=map(int,input().split()) ms=[tuple(map(int,input().split())) for i in range(m)] hs=[tuple(map(int,input().split())) for i in range(h)] shs = set(hs) uf = UnionFind2(n+1) for i in range(m): if ms[i] not in shs: uf.union(ms[i][0],ms[i][1]) ans = [0]*(n+1) for i in range(2,n+1): if uf.same(1,i): ans[i] = -1 cnt = h for i,j in hs[::-1]: if uf.same(i,j): #ここでもマイナス cnt-=1 continue #もしiと1が連結ならば 上でi,jが連結でないことがわかっているので #1とj集合は連結でないよってj集合は新たに1とつながる #ここでansに破壊時刻を記憶 unionの時に実際にマージする #family[] は xではなく find(x) if i in uf.family[uf.find(1)]: for k in uf.family[uf.find(j)]: ans[k] = cnt if j in uf.family[uf.find(1)]: for k in uf.family[uf.find(i)]: ans[k] = cnt uf.union(i,j) cnt-=1 for i in range(2,n+1): print(ans[i])