結果

問題 No.416 旅行会社
ユーザー vwxyzvwxyz
提出日時 2022-02-07 02:16:39
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 983 ms / 4,000 ms
コード長 3,243 bytes
コンパイル時間 1,551 ms
コンパイル使用メモリ 86,672 KB
実行使用メモリ 145,396 KB
最終ジャッジ日時 2023-08-21 10:48:44
合計ジャッジ時間 10,851 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 290 ms
116,836 KB
testcase_01 AC 72 ms
71,308 KB
testcase_02 AC 72 ms
71,040 KB
testcase_03 AC 71 ms
71,044 KB
testcase_04 AC 73 ms
71,204 KB
testcase_05 AC 77 ms
71,308 KB
testcase_06 AC 76 ms
70,920 KB
testcase_07 AC 87 ms
75,964 KB
testcase_08 AC 141 ms
78,892 KB
testcase_09 AC 267 ms
82,956 KB
testcase_10 AC 302 ms
116,904 KB
testcase_11 AC 297 ms
116,368 KB
testcase_12 AC 328 ms
117,168 KB
testcase_13 AC 292 ms
116,380 KB
testcase_14 AC 902 ms
143,152 KB
testcase_15 AC 879 ms
141,752 KB
testcase_16 AC 792 ms
140,996 KB
testcase_17 AC 983 ms
145,396 KB
testcase_18 AC 872 ms
142,964 KB
testcase_19 AC 702 ms
120,128 KB
testcase_20 AC 700 ms
120,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
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)

    def __str__(self):
        linked_components=defaultdict(list)
        for x in range(self.N):
            linked_components[self.Find(x)].append(x)
        return '\n'.join(f'{r}: {m}' for r,m in linked_components.items())

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)
0