結果

問題 No.416 旅行会社
ユーザー brthyyjpbrthyyjp
提出日時 2020-04-03 03:05:06
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,824 bytes
コンパイル時間 1,454 ms
コンパイル使用メモリ 86,892 KB
実行使用メモリ 131,508 KB
最終ジャッジ日時 2023-09-11 03:07:56
合計ジャッジ時間 10,142 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 228 ms
104,260 KB
testcase_01 AC 75 ms
71,180 KB
testcase_02 AC 76 ms
71,172 KB
testcase_03 AC 76 ms
71,308 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 253 ms
104,332 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

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]])
0