結果

問題 No.416 旅行会社
ユーザー qibqib
提出日時 2022-11-15 22:14:56
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,718 bytes
コンパイル時間 1,897 ms
コンパイル使用メモリ 87,052 KB
実行使用メモリ 200,096 KB
最終ジャッジ日時 2023-10-15 03:26:39
合計ジャッジ時間 13,758 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 405 ms
136,776 KB
testcase_01 AC 76 ms
71,268 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 77 ms
71,504 KB
testcase_05 AC 77 ms
71,360 KB
testcase_06 AC 80 ms
71,344 KB
testcase_07 WA -
testcase_08 AC 181 ms
78,844 KB
testcase_09 AC 283 ms
83,324 KB
testcase_10 AC 436 ms
136,808 KB
testcase_11 AC 445 ms
136,856 KB
testcase_12 AC 464 ms
136,824 KB
testcase_13 AC 395 ms
136,780 KB
testcase_14 AC 947 ms
199,716 KB
testcase_15 AC 1,025 ms
199,804 KB
testcase_16 AC 986 ms
200,096 KB
testcase_17 AC 947 ms
199,852 KB
testcase_18 AC 946 ms
199,684 KB
testcase_19 AC 833 ms
158,512 KB
testcase_20 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
  def __init__(self, n):
    self.node = [-1 for _ in range(n)]

  def root(self, v):
    if self.node[v] < 0:
      return v

    st = []
    while self.node[v] >= 0:
      st.append(v)
      v = self.node[v]

    for u in st:
      self.node[u] = v

    return v

  def size(self, v):
    v = self.root(v)
    return (- self.node[v])

  def same(self, u, v):
    return self.root(u) == self.root(v)

  def unite(self, u, v):
    ru = self.root(u)
    rv = self.root(v)
    if ru == rv:
      return

    du = self.node[ru]
    dv = self.node[rv]
    if du <= dv:
      self.node[rv] = ru
      self.node[ru] += dv
    else:
      self.node[ru] = rv
      self.node[rv] += du

n, m, q = map(int, input().split())
edges = []
idx = {}
for _ in range(m):
  a, b = map(int, input().split())
  a -= 1
  b -= 1
  edges.append((a, b))
  idx[a, b] = len(idx)

queries = []
for _ in range(q):
  c, d = map(int, input().split())
  c -= 1
  d -= 1
  queries.append(idx[c, d])

es = set([i for i in range(m)]) - set(queries)
uf = UnionFind(n)
comps = [[] for _ in range(n)]
for e in es:
  a, b = edges[e]
  uf.unite(a, b)

for v in range(n):
  r = uf.root(v)
  comps[r].append(v)

ans = [None for _ in range(n)]
for v in comps[0]:
  ans[v] = -1

for i in range(q - 1, -1, -1):
  c, d = edges[queries[i]]
  rc = uf.root(c)
  rd = uf.root(d)
  if rc == rd:
    continue

  r0 = uf.root(0)
  if r0 == rc:
    for v in comps[rd]:
      ans[v] = i + 1
  elif r0 == rd:
    for v in comps[rc]:
      ans[v] = i + 1

  uf.unite(rc, rd)
  r = uf.root(rc)
  while len(comps[rc + rd - r]) > 0:
    comps[r].append(comps[rc + rd - r].pop())

for v in range(n):
  if ans[v] is None:
    ans[v] = 0

print(*ans[1:], sep="\n")
0