結果

問題 No.1805 Approaching Many Typhoon
ユーザー H3PO4H3PO4
提出日時 2022-01-12 20:47:49
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 40 ms / 2,000 ms
コード長 1,876 bytes
コンパイル時間 97 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 11,264 KB
最終ジャッジ日時 2024-11-15 15:05:03
合計ジャッジ時間 2,396 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 35
権限があれば一括ダウンロードができます

ソースコード

diff #

"""2022/1/12現在、Mが実際に与えられる辺の数に一致しないケースと、Uが0であるケースがあります"""


class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parent = [i for i in range(n)]
        self.height = [1] * n
        self.size = [1] * n

    def find(self, x):
        if self.parent[x] == x:
            return x
        else:
            self.parent[x] = self.find(self.parent[x])
            return self.parent[x]

    def unite(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x != y:
            if self.height[x] < self.height[y]:
                self.parent[x] = y
                self.size[y] += self.size[x]
            else:
                self.parent[y] = x
                self.size[x] += self.size[y]
                if self.height[x] == self.height[y]:
                    self.height[x] += 1

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

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

    def group_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.parent) if i == x]

    def group_count(self):
        return len(self.roots())


N, M = map(int, input().split())  # Mは信用してはいけない
S, G = (int(x) - 1 for x in input().split())
edges = []
while True:
    s = input().split()
    if len(s) == 2:
        edges.append(tuple(int(x) - 1 for x in s))
    else:
        U = int(s[0])
        break
if U:
    I = set(int(x) - 1 for x in input().split())
else:  # U=0のケースがある
    I = set()
uf = UnionFind(N)
for f, t in edges:
    if (f not in I) and (t not in I):
        if 0 <= f < N and 0 <= t < N:
            uf.unite(f, t)
print("Yes" if uf.issame(S, G) else "No")
0