結果
| 問題 |
No.416 旅行会社
|
| コンテスト | |
| ユーザー |
vwxyz
|
| 提出日時 | 2022-02-07 02:14:07 |
| 言語 | PyPy2 (7.3.15) |
| 結果 |
MLE
|
| 実行時間 | - |
| コード長 | 3,191 bytes |
| コンパイル時間 | 1,614 ms |
| コンパイル使用メモリ | 76,328 KB |
| 実行使用メモリ | 818,712 KB |
| 最終ジャッジ日時 | 2024-06-11 15:02:22 |
| 合計ジャッジ時間 | 12,138 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | MLE * 1 -- * 20 |
ソースコード
import bisect
import copy
import decimal
import fractions
import heapq
import itertools
import math
import random
import sys
import time
from collections import Counter, deque,defaultdict
readline=sys.stdin.readline
class UnionFind:
def __init__(self,N,label=None,f=None,weighted=False):
self.N=N
self.parents=[None]*self.N
self.size=[1]*self.N
self.roots={i for i in range(self.N)}
self.label=label
if self.label!=None:
self.label=[x for x in label]
self.f=f
self.weighted=weighted
if self.weighted:
self.weight=[0]*self.N
def Find(self,x):
stack=[]
while self.parents[x]!=None:
stack.append(x)
x=self.parents[x]
if self.weighted:
w=0
for y in stack[::-1]:
self.parents[y]=x
w+=self.weight[y]
self.weight[y]=w
else:
for y in stack[::-1]:
self.parents[y]=x
return x
def Union(self,x,y,w=None):
root_x=self.Find(x)
root_y=self.Find(y)
if root_x==root_y:
if self.weighted:
if self.weight[y]-self.weight[x]==w:
return True
else:
return False
else:
if self.size[root_x]>self.size[root_y]:
x,y=y,x
root_x,root_y=root_y,root_x
if self.weighted:
w=-w
self.parents[root_y]=root_x
self.size[root_x]+=self.size[root_y]
self.roots.remove(root_y)
if self.label!=None:
self.label[root_x]=self.f(self.label[root_x],self.label[root_y])
if self.weighted:
self.weight[root_y]=w+self.weight[x]-self.weight[y]
def Size(self,x):
return self.size[self.Find(x)]
def Same(self,x,y):
return self.Find(x)==self.Find(y)
def Label(self,x):
return self.label[self.Find(x)]
def Weight(self,x,y):
root_x=self.Find(x)
root_y=self.Find(y)
if root_x!=root_y:
return None
return self.weight[y]-self.weight[x]
def Roots(self):
return list(self.roots)
def Link_Components_Count(self):
return len(self.roots)
N,M,Q=map(int,readline().split())
A,B=[],[]
for _ in range(M):
a,b=map(int,readline().split())
a-=1;b-=1
A.append(a)
B.append(b)
C,D=[],[]
CD=set()
for i in range(Q):
c,d=map(int,readline().split())
c-=1;d-=1
C.append(c)
D.append(d)
CD.add((c,d))
def f(lst0,lst1):
for x in lst1:
lst0.append(x)
return lst0
UF=UnionFind(N,label=[[x] for x in range(N)],f=f)
for a,b in zip(A,B):
if not (a,b) in CD:
UF.Union(a,b)
ans_lst=[0]*N
for x in UF.Label(0):
ans_lst[x]=-1
for i in range(Q-1,-1,-1):
c,d=C[i],D[i]
bl_c=UF.Same(c,0)
bl_d=UF.Same(d,0)
if not bl_c and bl_d:
for x in UF.Label(c):
ans_lst[x]=i+1
if bl_c and not bl_d:
for x in UF.Label(d):
ans_lst[x]=i+1
UF.Union(c,d)
for ans in ans_lst[1:]:
print(ans)
vwxyz