結果
問題 | No.416 旅行会社 |
ユーザー |
![]() |
提出日時 | 2023-03-15 02:02:54 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 1,033 ms / 4,000 ms |
コード長 | 1,727 bytes |
コンパイル時間 | 503 ms |
コンパイル使用メモリ | 81,960 KB |
実行使用メモリ | 155,496 KB |
最終ジャッジ日時 | 2024-09-18 08:33:26 |
合計ジャッジ時間 | 11,104 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 21 |
ソースコード
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)]# UnionFindGroup = [i for i in range(N+1)] # グループ分けNodes = [1]*(N+1) # 各グループのノードの数def find(x):while Group[x] != x:x=Group[x]return xdef Union(x,y):if find(x) != find(y):if Nodes[find(x)] < Nodes[find(y)]:Nodes[find(y)] += Nodes[find(x)]Nodes[find(x)] = 0Group[find(x)] = find(y)else:Nodes[find(x)] += Nodes[find(y)]Nodes[find(y)] = 0Group[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]=0for to in EDGE[x]:if ANS[to]==-1:ANS[to]=0Q.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+1for to in EDGE[x]:if ANS[to]==-1:ANS[to]=i+1Q.append(to)if ANS[y]==-1 and find(y)==find(1):Q=[y]while Q:x=Q.pop()ANS[x]=i+1for to in EDGE[x]:if ANS[to]==-1:ANS[to]=i+1Q.append(to)#print(i,ANS)for i in range(N+1):if ANS[i]==0:ANS[i]=-1if find(i)!=find(1):ANS[i]=0for a in ANS[2:]:print(a)