結果

問題 No.416 旅行会社
ユーザー convexineqconvexineq
提出日時 2021-02-03 00:42:06
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 702 ms / 4,000 ms
コード長 707 bytes
コンパイル時間 707 ms
コンパイル使用メモリ 87,040 KB
実行使用メモリ 156,228 KB
最終ジャッジ日時 2023-08-21 10:44:05
合計ジャッジ時間 9,943 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 368 ms
124,532 KB
testcase_01 AC 77 ms
71,400 KB
testcase_02 AC 77 ms
71,300 KB
testcase_03 AC 75 ms
71,400 KB
testcase_04 AC 77 ms
71,384 KB
testcase_05 AC 77 ms
71,184 KB
testcase_06 AC 80 ms
71,148 KB
testcase_07 AC 97 ms
75,840 KB
testcase_08 AC 156 ms
79,156 KB
testcase_09 AC 206 ms
82,148 KB
testcase_10 AC 373 ms
124,728 KB
testcase_11 AC 378 ms
124,592 KB
testcase_12 AC 392 ms
124,584 KB
testcase_13 AC 369 ms
124,664 KB
testcase_14 AC 699 ms
156,224 KB
testcase_15 AC 702 ms
156,200 KB
testcase_16 AC 696 ms
156,132 KB
testcase_17 AC 702 ms
156,124 KB
testcase_18 AC 697 ms
156,228 KB
testcase_19 AC 540 ms
127,448 KB
testcase_20 AC 539 ms
127,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n,m,q = map(int,input().split())
ab = [list(map(int,input().split())) for _ in range(m)]
cd = [list(map(int,input().split())) for _ in range(q)]
M = 2**20
dic = {c*M+d:i for i,(c,d) in enumerate(cd)}
ans = [0]*(n+1)
lst = [[i] for i in range(n+1)]

def merge(a,b,v):
    a, b = gp[a], gp[b]
    if a == b: return 0
    if a == gp[1]:
        for i in lst[b]: ans[i] = v
    if b == gp[1]:
        for i in lst[a]: ans[i] = v
    if len(lst[a]) > len(lst[b]):
        a,b = b,a
    for x in lst[a]:
        gp[x] = b
    lst[b] += lst[a]
    return 1

gp = list(range(n+1))
for a,b in ab:
    if a*M+b not in dic:
        merge(a,b,-1)
for i in range(q)[::-1]:
    merge(*cd[i],i+1)

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