結果

問題 No.416 旅行会社
ユーザー lllllll88938494lllllll88938494
提出日時 2023-05-11 11:49:04
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,155 ms / 4,000 ms
コード長 1,523 bytes
コンパイル時間 519 ms
コンパイル使用メモリ 87,244 KB
実行使用メモリ 175,592 KB
最終ジャッジ日時 2023-08-18 07:24:49
合計ジャッジ時間 14,481 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 410 ms
147,180 KB
testcase_01 AC 68 ms
71,300 KB
testcase_02 AC 68 ms
71,160 KB
testcase_03 AC 70 ms
71,256 KB
testcase_04 AC 68 ms
71,292 KB
testcase_05 AC 71 ms
71,408 KB
testcase_06 AC 70 ms
71,160 KB
testcase_07 AC 93 ms
76,264 KB
testcase_08 AC 170 ms
80,200 KB
testcase_09 AC 292 ms
86,792 KB
testcase_10 AC 438 ms
147,116 KB
testcase_11 AC 445 ms
147,224 KB
testcase_12 AC 450 ms
147,280 KB
testcase_13 AC 404 ms
147,216 KB
testcase_14 AC 1,053 ms
174,320 KB
testcase_15 AC 1,146 ms
174,128 KB
testcase_16 AC 1,023 ms
171,976 KB
testcase_17 AC 1,155 ms
174,428 KB
testcase_18 AC 1,111 ms
175,592 KB
testcase_19 AC 891 ms
148,768 KB
testcase_20 AC 823 ms
148,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind2:
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        self.family = [{i} for i in range(n)]
 
    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]
 
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        
        if self.parents[x] > self.parents[y]:
            x, y = y, x
 
        self.parents[x] += self.parents[y]
        self.parents[y] = x
        self.family[x] |= self.family[y]
        self.family[y] = {}
 
    def same(self,i,j):
        return uf.find(i) == uf.find(j)
 
    def size(self, x):
        return -self.parents[self.find(x)]

    
n,m,h=map(int,input().split())
ms=[tuple(map(int,input().split())) for i in range(m)]
hs=[tuple(map(int,input().split())) for i in range(h)]
shs = set(hs)
uf = UnionFind2(n+1)

for i in range(m):
    if ms[i] not in shs:
        uf.union(ms[i][0],ms[i][1])

ans = [0]*(n+1)
for i in range(2,n+1):
    if uf.same(1,i):
        ans[i] = -1

cnt = h
for i,j in hs[::-1]:
    if uf.same(i,j):
        cnt-=1
        continue

    if i in uf.family[uf.find(1)]:
        for k in uf.family[uf.find(j)]:
            ans[k] = cnt
    if j in uf.family[uf.find(1)]:
        for k in uf.family[uf.find(i)]:
            ans[k] = cnt
            
    uf.union(i,j)
    cnt-=1

for i in range(2,n+1):
    print(ans[i])
    
        
0