結果
問題 | No.416 旅行会社 |
ユーザー |
![]() |
提出日時 | 2020-04-03 03:05:06 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,824 bytes |
コンパイル時間 | 214 ms |
コンパイル使用メモリ | 82,324 KB |
実行使用メモリ | 131,204 KB |
最終ジャッジ日時 | 2024-06-28 17:39:21 |
合計ジャッジ時間 | 7,428 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 5 WA * 16 |
ソースコード
import sys input = sys.stdin.readline def Find(x, par): if par[x] < 0: return x else: # 経路圧縮 par[x] = Find(par[x], par) return par[x] def Unite(x, y, par, rank): x = Find(x, par) y = Find(y, par) if x != y: # rankの低い方を高い方につなげる if rank[x] < rank[y]: par[y] += par[x] par[x] = y else: par[x] += par[y] par[y] = x if rank[x] == rank[y]: rank[x] += 1 def Same(x, y, par): return Find(x, par) == Find(y, par) def Size(x, par): return -par[Find(x, par)] n, m, q = map(int, input().split()) AB = [] for i in range(m): a, b = map(int, input().split()) a, b = a-1, b-1 AB.append((a, b)) CD = [] for i in range(q): c, d = map(int, input().split()) c, d = c-1, d-1 CD.append((c, d)) S = set(CD) par = [-1]*n rank = [0]*n ans = [-2]*n for i in range(m): if AB[i] not in S: a, b = AB[i] Unite(a, b, par, rank) ori_par = [0]*n for i in range(n): ori_par[i] = Find(i, par) CD.reverse() for i in range(q): c, d = CD[i] #pc = ori_par[c] #pd = ori_par[d] pc = Find(c, par) pd = Find(d, par) x = Same(0, c, par) y = Same(0, d, par) Unite(c, d, par, rank) z = Same(0, c, par) w = Same(0, d, par) if x and z: if ans[pc] == -2: ans[pc] = -1 elif not x and z: if ans[pc] == -2: ans[pc] = q-i ori_par[c] = pc else: ori_par[c] = pc if y and w: if ans[pd] == -2: ans[pd] = -1 elif not y and w: if ans[pd] == -2: ori_par[d] = pd ans[pd] = q-i else: ori_par[d] = pd for i in range(n): if not Same(0, i, par): ans[ori_par[i]] = 0 for i in range(1, n): print(ans[ori_par[i]])