結果

問題 No.2674 k-Walk on Bipartite
ユーザー gew1fw
提出日時 2025-06-12 20:32:07
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,772 bytes
コンパイル時間 277 ms
コンパイル使用メモリ 81,536 KB
実行使用メモリ 99,072 KB
最終ジャッジ日時 2025-06-12 20:33:34
合計ジャッジ時間 7,247 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 24 WA * 12
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque

def main():
    n, m = map(int, sys.stdin.readline().split())
    s, t, k = map(int, sys.stdin.readline().split())
    edges = [[] for _ in range(n + 1)]
    for _ in range(m):
        a, b = map(int, sys.stdin.readline().split())
        edges[a].append(b)
        edges[b].append(a)
    
    # Bipartition coloring
    color = [-1] * (n + 1)
    for start in range(1, n + 1):
        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]:
                        pass  # Input is guaranteed to be bipartite
    
    # Check parity condition
    if (color[s] == color[t] and k % 2 != 0) or (color[s] != color[t] and k % 2 == 0):
        print("No")
        return
    
    # Compute shortest path from s to t
    dist = [-1] * (n + 1)
    q = deque()
    q.append(s)
    dist[s] = 0
    found = False
    while q and not found:
        u = q.popleft()
        for v in edges[u]:
            if dist[v] == -1:
                dist[v] = dist[u] + 1
                q.append(v)
                if v == t:
                    found = True
                    break
    
    d = dist[t]
    if d == -1:
        # s and t are not connected
        if color[s] == color[t]:
            print("No")
        else:
            print("Unknown")
    else:
        # s and t are connected
        if k >= d and (k - d) % 2 == 0:
            print("Yes")
        else:
            print("Unknown")

if __name__ == "__main__":
    main()
0