結果

問題 No.2674 k-Walk on Bipartite
ユーザー ゼットゼット
提出日時 2024-03-15 22:50:14
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 654 ms / 2,000 ms
コード長 1,881 bytes
コンパイル時間 256 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 126,668 KB
最終ジャッジ日時 2024-03-15 22:50:24
合計ジャッジ時間 9,986 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
55,616 KB
testcase_01 AC 43 ms
55,616 KB
testcase_02 AC 42 ms
55,616 KB
testcase_03 AC 37 ms
53,588 KB
testcase_04 AC 37 ms
53,588 KB
testcase_05 AC 37 ms
53,588 KB
testcase_06 AC 38 ms
53,588 KB
testcase_07 AC 520 ms
125,012 KB
testcase_08 AC 509 ms
111,188 KB
testcase_09 AC 296 ms
93,932 KB
testcase_10 AC 628 ms
118,752 KB
testcase_11 AC 315 ms
91,216 KB
testcase_12 AC 574 ms
109,776 KB
testcase_13 AC 315 ms
91,880 KB
testcase_14 AC 236 ms
110,492 KB
testcase_15 AC 654 ms
121,940 KB
testcase_16 AC 555 ms
126,668 KB
testcase_17 AC 476 ms
109,532 KB
testcase_18 AC 220 ms
87,776 KB
testcase_19 AC 370 ms
93,712 KB
testcase_20 AC 469 ms
123,776 KB
testcase_21 AC 502 ms
114,244 KB
testcase_22 AC 515 ms
98,844 KB
testcase_23 AC 40 ms
53,588 KB
testcase_24 AC 38 ms
53,588 KB
testcase_25 AC 38 ms
53,584 KB
testcase_26 AC 38 ms
53,588 KB
testcase_27 AC 38 ms
53,588 KB
testcase_28 AC 38 ms
53,584 KB
testcase_29 AC 38 ms
53,588 KB
testcase_30 AC 39 ms
53,588 KB
testcase_31 AC 38 ms
53,588 KB
testcase_32 AC 38 ms
53,588 KB
testcase_33 AC 39 ms
53,588 KB
testcase_34 AC 39 ms
53,588 KB
testcase_35 AC 38 ms
53,588 KB
testcase_36 AC 39 ms
53,588 KB
testcase_37 AC 38 ms
53,588 KB
testcase_38 AC 38 ms
53,588 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class unif:
  def __init__(self,n):
    self.pare=[-1]*n
    self.size=[1]*n
  def root(self,x):
    while self.pare[x]!=-1:
      x=self.pare[x]
    return x
  def unite(self,u,v):
    rootu=self.root(u)
    rootv=self.root(v)
    if rootu!=rootv:
      if self.size[rootu]>=self.size[rootv]:
        self.pare[rootv]=rootu
        self.size[rootu]+=self.size[rootv]
      else:
        self.pare[rootu]=rootv
        self.size[rootv]+=self.size[rootu]
  def same(self,s,t):
    return self.root(s)==self.root(t)
N,M=map(int,input().split())
s,t,K=map(int,input().split())
if N==1:
  print('No')
  exit()
if N==2:
  if s==t:
    if K%2==0:
      if M==1:
        print('Yes')
      else:
        print('Unknown')
    else:
      print('No')
  else:
    if K%2==0:
      print('No')
    else:
      if M==1:
        print('Yes')
      else:
        print('Unknown')
  exit()
G=[[] for i in range(N)]
Z=unif(N)
for i in range(M):
  a,b=map(int,input().split())
  G[a-1].append(b-1)
  G[b-1].append(a-1)
  Z.unite(a-1,b-1)
if Z.same(s-1,t-1)==False:
  print('Unknown')
  exit()
dist=[-1]*N
from collections import deque
S=deque()
p=[[0]*N for i in range(20)]
for i in range(N):
  if dist[i]>=0:
    continue
  dist[i]=0
  S.append(i)
  p[0][i]=i
  while S:
    x=S.popleft()
    for y in G[x]:
      if dist[y]>=0:
        continue
      dist[y]=dist[x]+1
      p[0][y]=x
      S.append(y)
for k in range(1,20):
  for i in range(N):
    p[k][i]=p[k-1][p[k-1][i]]
def LCA(x,y):
  a,b=x,y
  if dist[a]>dist[b]:
    a,b=b,a
  e=dist[b]-dist[a]
  for k in range(20):
    if (e>>k)&1:
      b=p[k][b]
  if a==b:
    return a
  for k in range(19,-1,-1):
    if p[k][a]!=p[k][b]:
      a=p[k][a]
      b=p[k][b]
  a=p[0][a]
  return a
s-=1
t-=1
if K%2!=(dist[s]-dist[t])%2:
  print('No')
  exit()
pos=LCA(s,t)
d=dist[s]+dist[t]-2*dist[pos]
if d>K:
  print('Unknown')
else:
  print('Yes')
  
0