結果

問題 No.416 旅行会社
ユーザー titiatitia
提出日時 2023-03-15 02:02:54
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,364 ms / 4,000 ms
コード長 1,727 bytes
コンパイル時間 186 ms
コンパイル使用メモリ 81,756 KB
実行使用メモリ 156,288 KB
最終ジャッジ日時 2023-10-18 12:11:46
合計ジャッジ時間 13,768 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 444 ms
115,952 KB
testcase_01 AC 40 ms
53,488 KB
testcase_02 AC 39 ms
53,488 KB
testcase_03 AC 39 ms
53,488 KB
testcase_04 AC 39 ms
53,488 KB
testcase_05 AC 40 ms
53,488 KB
testcase_06 AC 45 ms
59,092 KB
testcase_07 AC 67 ms
72,400 KB
testcase_08 AC 139 ms
77,368 KB
testcase_09 AC 277 ms
82,468 KB
testcase_10 AC 483 ms
115,952 KB
testcase_11 AC 494 ms
116,016 KB
testcase_12 AC 511 ms
116,228 KB
testcase_13 AC 452 ms
115,976 KB
testcase_14 AC 1,216 ms
153,488 KB
testcase_15 AC 1,301 ms
154,540 KB
testcase_16 AC 1,202 ms
153,624 KB
testcase_17 AC 1,364 ms
156,288 KB
testcase_18 AC 1,306 ms
154,548 KB
testcase_19 AC 1,021 ms
130,032 KB
testcase_20 AC 948 ms
130,056 KB
権限があれば一括ダウンロードができます

ソースコード

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

    if find(i)!=find(1):
        ANS[i]=0

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

0