結果

問題 No.416 旅行会社
ユーザー 👑 rin204rin204
提出日時 2022-07-08 09:54:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,063 ms / 4,000 ms
コード長 1,996 bytes
コンパイル時間 446 ms
コンパイル使用メモリ 86,304 KB
実行使用メモリ 154,332 KB
最終ジャッジ日時 2023-08-21 10:49:43
合計ジャッジ時間 12,484 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 404 ms
114,252 KB
testcase_01 AC 72 ms
71,080 KB
testcase_02 AC 71 ms
71,124 KB
testcase_03 AC 71 ms
70,824 KB
testcase_04 AC 72 ms
70,924 KB
testcase_05 AC 73 ms
70,940 KB
testcase_06 AC 75 ms
71,036 KB
testcase_07 AC 98 ms
75,880 KB
testcase_08 AC 166 ms
78,096 KB
testcase_09 AC 272 ms
82,480 KB
testcase_10 AC 432 ms
114,348 KB
testcase_11 AC 428 ms
113,904 KB
testcase_12 AC 443 ms
117,392 KB
testcase_13 AC 389 ms
113,992 KB
testcase_14 AC 1,031 ms
154,332 KB
testcase_15 AC 1,063 ms
153,448 KB
testcase_16 AC 975 ms
151,356 KB
testcase_17 AC 1,045 ms
154,196 KB
testcase_18 AC 1,043 ms
153,948 KB
testcase_19 AC 863 ms
135,932 KB
testcase_20 AC 856 ms
135,924 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        self.group = n
        self.ans = [0] * n
        self.child = [[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, i):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return
        self.group -= 1
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        p = self.find(0)
        if x == p:
            for j in self.child[y]:
                self.ans[j] = i
        elif y == p:
            for j in self.child[x]:
                self.ans[j] = i


        self.parents[x] += self.parents[y]
        self.parents[y] = x
        for c in self.child[y]:
            self.child[x].append(c)

    def size(self, x):
        return -self.parents[self.find(x)]

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

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return self.group

    def all_group_members(self):
        dic = {r:[] for r in self.roots()}
        for i in range(self.n):
            dic[self.find(i)].append(i)
        return dic

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

n, m, q = map(int, input().split())
edges = set(tuple(map(int, input().split())) for _ in range(m))
cd = []
for _ in range(q):
    c, d = map(int, input().split())
    cd.append((c, d))
    edges.remove((c, d))

UF = UnionFind(n)
for a, b in edges:
    UF.union(a - 1, b - 1, -1)

for i in range(q - 1, -1, -1):
    c, d = cd[i]
    UF.union(c - 1, d - 1, i + 1)
print(*UF.ans[1:], sep="\n")
0