結果

問題 No.416 旅行会社
ユーザー chocorusk
提出日時 2020-09-26 17:47:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 457 ms / 4,000 ms
コード長 1,058 bytes
コンパイル時間 414 ms
コンパイル使用メモリ 82,168 KB
実行使用メモリ 224,540 KB
最終ジャッジ日時 2024-12-14 20:53:53
合計ジャッジ時間 6,438 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
read=sys.stdin.buffer.read
readline=sys.stdin.buffer.readline
readlines=sys.stdin.buffer.readlines

n, m, q=map(int, readline().split())
abcd=list(map(int ,read().split()))
a=abcd[:2*m:2]
b=abcd[1:2*m:2]
c=abcd[2*m::2]
d=abcd[2*m+1::2]
par=list(range(n))
v=[[i] for i in range(n)]
ans=[0]*n
def merge(x, y):
    if len(v[x])>len(v[y]):
        x, y=y, x
    while v[x]:
        z=v[x].pop()
        par[z]=y
        v[y].append(z)
cd=set(zip(c, d))
for x, y in zip(a, b):
    if (x, y) not in cd:
        x1, y1=par[x-1], par[y-1]
        if x1==y1:
            continue
        if x1==par[0]:
            for z in v[y1]:
                ans[z]=-1
        elif y1==par[0]:
            for z in v[x1]:
                ans[z]=-1
        merge(x1, y1)
for i in range(q-1, -1, -1):
    x, y=c[i]-1, d[i]-1
    x1, y1=par[x], par[y]
    if x1==y1:
        continue
    if x1==par[0]:
        for z in v[y1]:
            ans[z]=i+1
    elif y1==par[0]:
        for z in v[x1]:
            ans[z]=i+1
    merge(x1, y1)
print('\n'.join(map(str, ans[1:])))
0