結果

問題 No.2674 k-Walk on Bipartite
ユーザー lam6er
提出日時 2025-03-26 15:47:32
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,971 bytes
コンパイル時間 161 ms
コンパイル使用メモリ 82,420 KB
実行使用メモリ 125,180 KB
最終ジャッジ日時 2025-03-26 15:48:30
合計ジャッジ時間 6,574 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24 WA * 12
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

import sys
from collections import deque
def main():
input = sys.stdin.read().split()
ptr = 0
N = int(input[ptr])
ptr += 1
M = int(input[ptr])
ptr += 1
s = int(input[ptr]) - 1 # 0-based
ptr += 1
t = int(input[ptr]) - 1
ptr += 1
k = int(input[ptr])
ptr += 1
edges = [[] for _ in range(N)]
for _ in range(M):
a = int(input[ptr]) - 1
ptr += 1
b = int(input[ptr]) - 1
ptr += 1
edges[a].append(b)
edges[b].append(a)
# Bipartition coloring
color = [-1] * N
is_bipartite = True
for start in range(N):
if color[start] == -1:
q = deque()
q.append(start)
color[start] = 0
while q:
u = q.popleft()
for v in edges[u]:
if color[v] == -1:
color[v] = color[u] ^ 1
q.append(v)
elif color[v] == color[u]:
is_bipartite = False
# Since F is guaranteed to be bipartite, we don't need to handle non-bipartite case
s_color = color[s]
t_color = color[t]
# Check parity
if (s_color == t_color and k % 2 != 0) or (s_color != t_color and k % 2 != 1):
print("No")
return
# Check connectivity and compute shortest distance d from s to t
visited = [False] * N
dist = [-1] * N
q = deque()
q.append(s)
dist[s] = 0
visited[s] = True
found = False
while q:
u = q.popleft()
if u == t:
found = True
break
for v in edges[u]:
if not visited[v]:
visited[v] = True
dist[v] = dist[u] + 1
q.append(v)
if not found:
print("Unknown")
return
d = dist[t]
if d > k or (k - d) % 2 != 0:
print("Unknown")
else:
print("Yes")
if __name__ == "__main__":
main()
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0