結果

問題 No.1983 [Cherry 4th Tune C] 南の島のマーメイド
ユーザー strangerxxxstrangerxxx
提出日時 2022-08-30 09:30:48
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
WA  
実行時間 -
コード長 2,481 bytes
コンパイル時間 254 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 56,448 KB
最終ジャッジ日時 2024-04-24 19:38:58
合計ジャッジ時間 59,290 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
10,880 KB
testcase_01 AC 31 ms
11,008 KB
testcase_02 AC 31 ms
10,880 KB
testcase_03 AC 32 ms
10,880 KB
testcase_04 AC 31 ms
11,008 KB
testcase_05 AC 32 ms
11,008 KB
testcase_06 AC 32 ms
10,880 KB
testcase_07 AC 32 ms
11,008 KB
testcase_08 AC 80 ms
11,520 KB
testcase_09 AC 105 ms
11,904 KB
testcase_10 AC 113 ms
12,672 KB
testcase_11 AC 131 ms
12,544 KB
testcase_12 AC 89 ms
11,776 KB
testcase_13 AC 1,620 ms
29,952 KB
testcase_14 AC 1,733 ms
32,768 KB
testcase_15 AC 1,911 ms
39,552 KB
testcase_16 AC 1,028 ms
29,696 KB
testcase_17 AC 1,721 ms
34,048 KB
testcase_18 AC 1,541 ms
39,808 KB
testcase_19 AC 1,680 ms
46,080 KB
testcase_20 AC 1,597 ms
37,888 KB
testcase_21 WA -
testcase_22 AC 1,724 ms
40,448 KB
testcase_23 AC 2,490 ms
48,640 KB
testcase_24 AC 2,522 ms
48,640 KB
testcase_25 AC 2,451 ms
48,768 KB
testcase_26 AC 2,460 ms
48,640 KB
testcase_27 AC 2,525 ms
48,640 KB
testcase_28 AC 2,418 ms
48,640 KB
testcase_29 WA -
testcase_30 AC 2,415 ms
48,640 KB
testcase_31 AC 2,423 ms
48,640 KB
testcase_32 AC 2,405 ms
48,640 KB
testcase_33 AC 31 ms
11,008 KB
testcase_34 AC 1,391 ms
28,288 KB
testcase_35 AC 1,891 ms
47,104 KB
testcase_36 AC 1,995 ms
46,976 KB
testcase_37 AC 31 ms
11,008 KB
testcase_38 WA -
testcase_39 AC 2,332 ms
46,976 KB
testcase_40 AC 2,482 ms
56,448 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def resolve():
    n, m, q = map(int, input().split())
    edges = [[] for _ in range(n)]
    for _ in range(m):
        u, v = map(lambda x: int(x) - 1, input().split())
        edges[u].append(v)
        edges[v].append(u)
    u = decompositon(edges)
    for _ in range(q):
        x, y = map(lambda x: int(x) - 1, input().split())
        if u.same(x, y):
            print("Yes")
        else:
            print("No")


def decompositon(edges):
    in_cnt = [len(x) for x in edges]
    q = [i for i, x in enumerate(in_cnt) if x == 1]
    u = UnionFind(len(edges))
    while q:
        i = q.pop()
        for j in edges[i]:
            u.union(i, j)
            in_cnt[j] -= 1
            if in_cnt[j] == 1:
                q.append(j)
    return u


class UnionFind:
    def __init__(self, n: int) -> None:
        self.n = n
        self.parent = [-1] * n
        self.groups = n

    def find(self, x: int) -> int:
        if self.parent[x] < 0:
            return x
        else:
            p = x
            while self.parent[p] >= 0:
                p = self.parent[p]
            while self.parent[x] >= 0:
                self.parent[x], x = p, self.parent[x]
            return p

    def union(self, x: int, y: int) -> bool:
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False
        if self.parent[x] > self.parent[y]:
            x, y = y, x
        self.parent[x] += self.parent[y]
        self.parent[y] = x
        self.groups -= 1
        return True

    def size(self, x: int) -> int:
        return -self.parent[self.find(x)]

    def same(self, x: int, y: int) -> bool:
        return self.find(x) == self.find(y)

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

    def roots(self) -> list:
        return [i for i, x in enumerate(self.parent) if x < 0]

    def group_count(self) -> int:
        return self.groups

    def sizes(self) -> dict:
        return {i: -x for i, x in enumerate(self.parent) if x < 0}

    def all_group_members(self) -> dict:
        from collections import defaultdict
        d = defaultdict(list)
        for i in range(self.n):
            p = self.find(i)
            d[p].append(i)
        return d

    def __str__(self) -> str:
        return '\n'.join('{}: {}'.format(k, v)
                         for k, v in self.all_group_members().items())


if __name__ == '__main__':
    resolve()
0