結果

問題 No.416 旅行会社
ユーザー titiatitia
提出日時 2023-03-15 01:58:59
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,684 bytes
コンパイル時間 247 ms
コンパイル使用メモリ 81,752 KB
実行使用メモリ 156,308 KB
最終ジャッジ日時 2023-10-18 12:11:13
合計ジャッジ時間 14,046 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 409 ms
116,020 KB
testcase_01 AC 37 ms
53,548 KB
testcase_02 AC 37 ms
53,548 KB
testcase_03 AC 37 ms
53,548 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 436 ms
116,020 KB
testcase_11 AC 443 ms
116,048 KB
testcase_12 AC 462 ms
116,232 KB
testcase_13 AC 424 ms
116,048 KB
testcase_14 AC 1,133 ms
153,240 KB
testcase_15 AC 1,181 ms
155,432 KB
testcase_16 AC 1,152 ms
153,376 KB
testcase_17 AC 1,297 ms
156,308 KB
testcase_18 AC 1,225 ms
154,616 KB
testcase_19 WA -
testcase_20 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

N,M,Q=map(int,input().split())
E=[tuple(map(int,input().split())) for i in range(M)]
E2=[tuple(map(int,input().split())) for i in range(Q)]

# UnionFind

Group = [i for i in range(N+1)] # グループ分け
Nodes = [1]*(N+1) # 各グループのノードの数

def find(x):
    while Group[x] != x:
        x=Group[x]
    return x

def Union(x,y):
    if find(x) != find(y):
        if Nodes[find(x)] < Nodes[find(y)]:
            
            Nodes[find(y)] += Nodes[find(x)]
            Nodes[find(x)] = 0
            Group[find(x)] = find(y)
            
        else:
            Nodes[find(x)] += Nodes[find(y)]
            Nodes[find(y)] = 0
            Group[find(y)] = find(x)

EDGE=[[] for i in range(N+1)]

for x,y in set(E)-set(E2):
    EDGE[x].append(y)
    EDGE[y].append(x)

    Union(x,y)

ANS=[-1]*(N+1)

Q=[1]

while Q:
    x=Q.pop()
    ANS[x]=0
    for to in EDGE[x]:
        if ANS[to]==-1:
            ANS[to]=0
            Q.append(to)

#print(ANS)


for i in range(len(E2)-1,-1,-1):
    x,y = E2[i]
    Union(x,y)
    EDGE[x].append(y)
    EDGE[y].append(x)

    if ANS[x]==-1 and find(x)==find(1):
        Q=[x]

        while Q:
            x=Q.pop()
            ANS[x]=i+1
            for to in EDGE[x]:
                if ANS[to]==-1:
                    ANS[to]=i+1
                    Q.append(to)

    if ANS[y]==-1 and find(y)==find(1):
        Q=[y]

        while Q:
            x=Q.pop()
            ANS[x]=i+1
            for to in EDGE[x]:
                if ANS[to]==-1:
                    ANS[to]=i+1
                    Q.append(to)

    #print(i,ANS)

for i in range(N+1):
    if ANS[i]==0:
        ANS[i]=-1

for a in ANS[2:]:
    print(a)

0