結果

問題 No.416 旅行会社
ユーザー Navier_BoltzmannNavier_Boltzmann
提出日時 2023-06-17 13:19:22
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,623 ms / 4,000 ms
コード長 2,703 bytes
コンパイル時間 669 ms
コンパイル使用メモリ 82,440 KB
実行使用メモリ 304,372 KB
最終ジャッジ日時 2024-06-25 04:20:18
合計ジャッジ時間 24,115 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 819 ms
254,784 KB
testcase_01 AC 47 ms
56,640 KB
testcase_02 AC 45 ms
56,272 KB
testcase_03 AC 47 ms
56,272 KB
testcase_04 AC 47 ms
58,048 KB
testcase_05 AC 53 ms
64,252 KB
testcase_06 AC 56 ms
66,808 KB
testcase_07 AC 130 ms
78,236 KB
testcase_08 AC 317 ms
82,008 KB
testcase_09 AC 574 ms
92,844 KB
testcase_10 AC 855 ms
255,420 KB
testcase_11 AC 700 ms
219,756 KB
testcase_12 AC 729 ms
228,872 KB
testcase_13 AC 660 ms
229,264 KB
testcase_14 AC 2,623 ms
277,940 KB
testcase_15 AC 2,516 ms
279,436 KB
testcase_16 AC 2,245 ms
304,372 KB
testcase_17 AC 2,381 ms
284,820 KB
testcase_18 AC 2,464 ms
277,420 KB
testcase_19 AC 2,022 ms
266,224 KB
testcase_20 AC 1,961 ms
232,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import *
from itertools import *
from functools import *
from heapq import *
import sys,math
input = sys.stdin.readline

class DSU:
    def __init__(self, n):
        self._n = n
        self.parent_or_size = [-1] * n

    def merge(self, a, b):
        assert 0 <= a < self._n
        assert 0 <= b < self._n
        x, y = self.leader(a), self.leader(b)
        if x == y: return x
        if -self.parent_or_size[x] < -self.parent_or_size[y]: x, y = y, x
        self.parent_or_size[x] += self.parent_or_size[y]
        self.parent_or_size[y] = x
        return x

    def same(self, a, b):
        assert 0 <= a < self._n
        assert 0 <= b < self._n
        return self.leader(a) == self.leader(b)

    def leader(self, a):
        assert 0 <= a < self._n
        if self.parent_or_size[a] < 0: return a
        self.parent_or_size[a] = self.leader(self.parent_or_size[a])
        return self.parent_or_size[a]

    def size(self, a):
        assert 0 <= a < self._n
        return -self.parent_or_size[self.leader(a)]

    def groups(self):
        leader_buf = [self.leader(i) for i in range(self._n)]
        result = [[] for _ in range(self._n)]
        for i in range(self._n): result[leader_buf[i]].append(i)
        return [r for r in result if r != []]

N,M,Q = map(int,input().split())

AB = [tuple(map(int,input().split())) for _ in range(M)]
CD = [tuple(map(int,input().split())) for _ in range(Q)]

X = set(CD)

D = DSU(N+1)
Y = []
for a,b in AB:
    if (a,b) in X:
        continue
    Y.append((a,b))
    D.merge(a,b)

check = defaultdict(lambda:set())
ok = defaultdict(lambda:Q-1)
ng = defaultdict(lambda:-1)
mid = defaultdict(lambda:(Q-1)//2)

ans = defaultdict(lambda:-2)
F = set()
S = set([i for i in range(2,N+1)])
for i in range(2,N+1):
    if D.same(1,i):
        ans[i]=-1
        F.add(i)
        S.discard(i)
        

for c,d in CD:
    D.merge(c,d)

for i in range(2,N+1):
    if i in F:
        continue
    if not D.same(1,i):
        S.discard(i)
        F.add(i)
        ans[i] = 0
        

for s in S:
    check[(Q-1)//2].add(s)

cnt = 0
n = len(S)

    
while cnt!=n:
    
    D = DSU(N+1)
    for a,b in Y:
        D.merge(a,b)
    
    
    for i in range(Q):
        c,d = CD[-1-i]
        D.merge(c,d)
        for s in check[i]:
            
            if D.same(1,s):
                ok[s] = i
            else:
                ng[s] = i
            
            if ok[s]-ng[s]==1:
                cnt += 1
                continue

            mid[s] = (ok[s]+ng[s])//2
            check[mid[s]].add(s)
        
        check[i] = set()
for s in S:
    
    ans[s] = Q - ok[s]
    
for i in range(2,N+1):
    print(ans[i])
    
    
    
0