結果

問題 No.1805 Approaching Many Typhoon
ユーザー 8989
提出日時 2022-02-21 13:01:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 116 ms / 2,000 ms
コード長 1,764 bytes
コンパイル時間 882 ms
コンパイル使用メモリ 87,072 KB
実行使用メモリ 78,068 KB
最終ジャッジ日時 2023-09-12 04:26:23
合計ジャッジ時間 5,642 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 90 ms
71,648 KB
testcase_01 AC 89 ms
71,712 KB
testcase_02 AC 93 ms
71,784 KB
testcase_03 AC 92 ms
71,576 KB
testcase_04 AC 88 ms
71,428 KB
testcase_05 AC 86 ms
71,824 KB
testcase_06 AC 89 ms
71,848 KB
testcase_07 AC 87 ms
71,672 KB
testcase_08 AC 91 ms
71,476 KB
testcase_09 AC 89 ms
71,588 KB
testcase_10 AC 89 ms
71,404 KB
testcase_11 AC 88 ms
71,632 KB
testcase_12 AC 88 ms
71,540 KB
testcase_13 AC 91 ms
71,748 KB
testcase_14 AC 88 ms
71,836 KB
testcase_15 AC 89 ms
71,648 KB
testcase_16 AC 87 ms
71,604 KB
testcase_17 AC 88 ms
71,608 KB
testcase_18 AC 89 ms
71,736 KB
testcase_19 AC 92 ms
71,672 KB
testcase_20 AC 89 ms
71,888 KB
testcase_21 AC 88 ms
71,892 KB
testcase_22 AC 89 ms
71,736 KB
testcase_23 AC 88 ms
71,412 KB
testcase_24 AC 91 ms
71,788 KB
testcase_25 AC 86 ms
71,784 KB
testcase_26 AC 89 ms
71,840 KB
testcase_27 AC 88 ms
71,672 KB
testcase_28 AC 116 ms
77,796 KB
testcase_29 AC 113 ms
77,784 KB
testcase_30 AC 113 ms
78,068 KB
testcase_31 AC 114 ms
77,936 KB
testcase_32 AC 93 ms
71,840 KB
testcase_33 AC 90 ms
71,712 KB
testcase_34 AC 95 ms
72,424 KB
testcase_35 AC 99 ms
72,060 KB
testcase_36 AC 106 ms
76,432 KB
testcase_37 AC 88 ms
71,600 KB
testcase_38 AC 88 ms
71,584 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

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

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    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 len(self.roots())

    def all_group_members(self):
        group_members = defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return '\n'.join(f'{r}: {m}' for r, m in self.all_group_members().items())
n,m = map(int,input().split())
s,g = map(int,input().split())
ab = []
for i in range(m):
    z,x = map(int,input().split())
    ab.append([z,x])
u = int(input())
p = set(list(map(int,input().split())))
q = []
for i in range(m):
    if (ab[i][0] not in p) and (ab[i][1] not in p):
        q.append(ab[i])
P = UnionFind(n)
for i in range(len(q)):
    P.union(q[i][0]-1,q[i][1] - 1)
#print(s - 1,g - 1)
if P.same(s - 1,g - 1):
    print("Yes")
else:
    print("No")
#print(P)
0