結果

問題 No.416 旅行会社
ユーザー vwxyzvwxyz
提出日時 2022-02-07 02:17:46
言語 PyPy2
(7.3.15)
結果
AC  
実行時間 1,132 ms / 4,000 ms
コード長 3,014 bytes
コンパイル時間 688 ms
コンパイル使用メモリ 77,976 KB
実行使用メモリ 182,036 KB
最終ジャッジ日時 2023-08-21 10:49:10
合計ジャッジ時間 12,509 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 371 ms
127,204 KB
testcase_01 AC 74 ms
76,268 KB
testcase_02 AC 74 ms
76,264 KB
testcase_03 AC 74 ms
76,232 KB
testcase_04 AC 75 ms
76,528 KB
testcase_05 AC 76 ms
76,276 KB
testcase_06 AC 79 ms
77,596 KB
testcase_07 AC 99 ms
79,644 KB
testcase_08 AC 166 ms
82,420 KB
testcase_09 AC 310 ms
86,920 KB
testcase_10 AC 382 ms
127,020 KB
testcase_11 AC 377 ms
127,448 KB
testcase_12 AC 407 ms
127,212 KB
testcase_13 AC 364 ms
127,008 KB
testcase_14 AC 1,064 ms
180,888 KB
testcase_15 AC 1,030 ms
179,524 KB
testcase_16 AC 923 ms
177,784 KB
testcase_17 AC 1,132 ms
182,036 KB
testcase_18 AC 1,034 ms
179,604 KB
testcase_19 AC 815 ms
137,304 KB
testcase_20 AC 820 ms
138,628 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)

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