結果

問題 No.2674 k-Walk on Bipartite
ユーザー gew1fw
提出日時 2025-06-12 19:40:08
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,177 bytes
コンパイル時間 208 ms
コンパイル使用メモリ 82,152 KB
実行使用メモリ 99,572 KB
最終ジャッジ日時 2025-06-12 19:40:22
合計ジャッジ時間 5,965 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30 WA * 6
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque

def main():
    sys.setrecursionlimit(1 << 25)
    N, M = map(int, sys.stdin.readline().split())
    s, t, k = map(int, sys.stdin.readline().split())
    s -= 1
    t -= 1

    # Build adjacency list
    adj = [[] for _ in range(N)]
    for _ in range(M):
        a, b = map(int, sys.stdin.readline().split())
        a -= 1
        b -= 1
        adj[a].append(b)
        adj[b].append(a)

    # Check if s and t are in the same connected component
    visited = [False] * N
    component = [ -1 ] * N
    comp_id = 0

    q = deque()
    q.append(s)
    visited[s] = True
    component[s] = comp_id

    while q:
        u = q.popleft()
        for v in adj[u]:
            if not visited[v]:
                visited[v] = True
                component[v] = comp_id
                q.append(v)
    
    if component[s] != component[t]:
        print("Unknown")
        return

    # Compute bipartition using BFS
    color = [ -1 ] * N
    queue = deque()
    queue.append(s)
    color[s] = 0
    is_bipartite = True
    while queue:
        u = queue.popleft()
        for v in adj[u]:
            if color[v] == -1:
                color[v] = color[u] ^ 1
                queue.append(v)
            else:
                if color[v] == color[u]:
                    is_bipartite = False
    if not is_bipartite:
        print("Unknown")
        return

    s_color = color[s]
    t_color = color[t]
    required_parity = (s_color != t_color)
    k_parity = (k % 2) == 1
    if required_parity != k_parity:
        print("No")
        return

    # Compute minimal path length using BFS
    def bfs(start):
        dist = [ -1 ] * N
        q = deque()
        q.append(start)
        dist[start] = 0
        while q:
            u = q.popleft()
            for v in adj[u]:
                if dist[v] == -1:
                    dist[v] = dist[u] + 1
                    q.append(v)
        return dist

    dist = bfs(s)
    if dist[t] == -1:
        print("Unknown")
        return
    d = dist[t]

    if k >= d and (k - d) % 2 == 0:
        print("Yes")
    else:
        print("Unknown")

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