結果

問題 No.416 旅行会社
ユーザー neterukunneterukun
提出日時 2021-01-03 00:54:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,253 ms / 4,000 ms
コード長 1,747 bytes
コンパイル時間 326 ms
コンパイル使用メモリ 86,812 KB
実行使用メモリ 150,232 KB
最終ジャッジ日時 2023-08-21 10:43:46
合計ジャッジ時間 14,462 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 491 ms
121,588 KB
testcase_01 AC 73 ms
70,792 KB
testcase_02 AC 73 ms
71,088 KB
testcase_03 AC 71 ms
70,832 KB
testcase_04 AC 72 ms
71,080 KB
testcase_05 AC 77 ms
74,824 KB
testcase_06 AC 79 ms
75,148 KB
testcase_07 AC 116 ms
77,320 KB
testcase_08 AC 159 ms
78,296 KB
testcase_09 AC 255 ms
82,956 KB
testcase_10 AC 519 ms
121,612 KB
testcase_11 AC 586 ms
120,612 KB
testcase_12 AC 620 ms
122,412 KB
testcase_13 AC 558 ms
120,936 KB
testcase_14 AC 1,204 ms
150,156 KB
testcase_15 AC 1,223 ms
149,884 KB
testcase_16 AC 1,142 ms
149,576 KB
testcase_17 AC 1,253 ms
150,232 KB
testcase_18 AC 1,194 ms
149,884 KB
testcase_19 AC 945 ms
130,520 KB
testcase_20 AC 1,011 ms
130,552 KB
権限があれば一括ダウンロードができます

ソースコード

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