結果

問題 No.416 旅行会社
ユーザー neterukunneterukun
提出日時 2021-01-03 00:53:20
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,730 bytes
コンパイル時間 158 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 149,212 KB
最終ジャッジ日時 2024-04-20 19:32:58
合計ジャッジ時間 12,286 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 405 ms
121,908 KB
testcase_01 AC 39 ms
52,736 KB
testcase_02 AC 38 ms
52,608 KB
testcase_03 AC 39 ms
52,992 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 432 ms
121,872 KB
testcase_11 AC 483 ms
121,012 KB
testcase_12 AC 505 ms
122,680 KB
testcase_13 AC 449 ms
120,472 KB
testcase_14 AC 987 ms
149,080 KB
testcase_15 AC 984 ms
148,952 KB
testcase_16 AC 942 ms
148,700 KB
testcase_17 AC 949 ms
149,212 KB
testcase_18 AC 970 ms
149,212 KB
testcase_19 WA -
testcase_20 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

from bisect import bisect_left


class PertiallyPersistentUnionFind:
    def __init__(self, n):
        self.INF = 10 ** 9
        self.parent = [-1] * n
        self.time = [self.INF] * n
        self.size = [[(-1, -1)] for i in range(n)]

    def root(self, t, x):
        while self.time[x] <= t:
            x = self.parent[x]
        return x

    def merge(self, t, x, y):
        x = self.root(t, x)
        y = self.root(t, y)
        if x == y:
            return False
        if self.parent[x] > self.parent[y]:
            x, y = y, x
        self.parent[x] += self.parent[y]
        self.size[x].append((t, self.parent[x]))
        self.parent[y] = x
        self.time[y] = t
        return True

    def same(self, t, x, y):
        return self.root(t, x) == self.root(t, y)

    def size(self, t, x):
        x = self.root(t, x)
        idx = bisect_left(self.size[x], (t, self.INF)) - 1
        return -self.size[x][idx][1]


n, m, q = map(int, input().split())
edges = [tuple(map(int, input().split())) for i in range(m)]
queries = [tuple(map(int, input().split())) for i in range(q)]


remain = set(edges)
for e in queries:
    remain.remove(e)

uf = PertiallyPersistentUnionFind(n)
for a, b in remain:
    a -= 1
    b -= 1
    uf.merge(0, a, b)

for t, (a, b) in enumerate(queries[::-1]):
    a -= 1
    b -= 1
    t += 1
    uf.merge(t, a, b)

ans = []
for v in range(1, n):
    if not uf.same(q, 0, v):
        ans.append(0)
    ok = q
    ng = -1
    while abs(ok - ng) > 1:
        mid = (ok + ng) // 2
        if uf.same(mid, 0, v):
            ok = mid
        else:
            ng = mid
    if ok == 0:
        ans.append(-1)
    else:
        ans.append(q - ok + 1)
    
print('\n'.join(map(str, ans)))
0