結果
問題 | No.2674 k-Walk on Bipartite |
ユーザー |
![]() |
提出日時 | 2024-03-15 21:55:16 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 475 ms / 2,000 ms |
コード長 | 1,391 bytes |
コンパイル時間 | 463 ms |
コンパイル使用メモリ | 82,312 KB |
実行使用メモリ | 100,080 KB |
最終ジャッジ日時 | 2024-09-30 00:59:57 |
合計ジャッジ時間 | 7,771 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 36 |
ソースコード
class UnionFind:def __init__(self, n):self.n = nself.parents = [-1] * ndef find(self, x):if self.parents[x] < 0:return xelse: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:returnif self.parents[x] > self.parents[y]:x, y = y, xself.parents[x] += self.parents[y]self.parents[y] = xn, m = map(int,input().split())s, t, k = map(int,input().split())s -= 1t -= 1if n == 1:print("No")exit()ikeru = [[] for i in range(n)]uf = UnionFind(n)for i in range(m):a, b = map(int,input().split())a -= 1b -= 1ikeru[a].append(b)ikeru[b].append(a)uf.union(a, b)if n == 2:if s == t:if k % 2 == 1:print("No")exit()if uf.find(0) != uf.find(1):print("Unknown")exit()print("Yes")else:if k % 2 == 0:print("No")exit()if uf.find(0) != uf.find(1):print("Unknown")exit()print("Yes")exit()if uf.find(s) != uf.find(t):print("Unknown")exit()from collections import dequedst = [0] * ntansaku = [0] * ntansaku[s] = 1mada = deque([])mada.append(s)while mada:i = mada.popleft()for j in ikeru[i]:if tansaku[j] == 0:tansaku[j] = 1mada.append(j)dst[j] = dst[i] + 1if dst[t] % 2 != k % 2:print("No")exit()if dst[t] <= k:print("Yes")exit()print("Unknown")