結果
問題 | No.1805 Approaching Many Typhoon |
ユーザー |
![]() |
提出日時 | 2022-01-20 21:57:39 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 161 ms / 2,000 ms |
コード長 | 2,383 bytes |
コンパイル時間 | 165 ms |
コンパイル使用メモリ | 82,688 KB |
実行使用メモリ | 89,728 KB |
最終ジャッジ日時 | 2024-11-24 07:04:25 |
合計ジャッジ時間 | 7,317 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 35 |
ソースコード
import itertools as iterimport collections as collimport heapq as hqimport bisect as bisfrom decimal import Decimal as decfrom copy import deepcopy as dcopyimport mathimport syssys.setrecursionlimit(10**6)def input():return sys.stdin.readline().rstrip()def getN():return int(sys.stdin.readline())def getNs():return map(int,sys.stdin.readline().split())def getList():return list(map(int,sys.stdin.readline().split()))def strinps(n):return [sys.stdin.readline().rstrip() for _ in range(n)]pi = 3.141592653589793mod = 10**9+7MOD = 998244353INF = math.infdx = [1,0,-1,0]; dy = [0,1,0,-1]"""Union-Findfrom : https://github.com/customaddone/beginPython/blob/master/cgi-bin/library/unionfind/unionfind.pyref : https://algo-logic.info/union-find-tree/"""class UnionFind():#Uni = UnionFind(n) のようにするdef __init__(self, n):self.n = nself.parents = [-1] * n#xの親(親がいないときは自身の番号を返す)def find(self, x):if self.parents[x] < 0:return xelse:self.parents[x] = self.find(self.parents[x])return self.parents[x]#xとyを関係付けるdef union(self, x, y):x = self.find(x)y = self.find(y)if x == y:returnif self.parents[x] > self.parents[y]:x, y = y, x# if x > y: # よりrootのインデックスが小さい方が親# x, y = y, xself.parents[x] += self.parents[y]self.parents[y] = x#xとyが同じ組に属するかどうかdef same(self, x, y):return self.find(x) == self.find(y)#xが属する組の大きさdef size(self, x):return -self.parents[self.find(x)]#xが属する組の全要素をリストとして取得def members(self, x):root = self.find(x)return [i for i in range(self.n) if self.find(i) == root]#Union-Find木の根に当たる要素全てをリストとして取得def roots(self):return [i for i, x in enumerate(self.parents) if x < 0]#Union-Find木の根とそれに属する組の全要素def all_group_members(self):return {r: self.members(r) for r in self.roots()}"""Main Code"""n, m = getNs()s, g = [i - 1 for i in getNs()]ship = [[i - 1 for i in getNs()] for _ in [0] * m]t = getN()st = set(i - 1 for i in getNs())uni = UnionFind(n)for a, b in ship:if(a not in st and b not in st):uni.union(a, b)if(uni.same(s, g)):print("Yes")else:print("No")