結果

問題 No.2674 k-Walk on Bipartite
ユーザー lam6er
提出日時 2025-03-31 17:52:56
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,502 bytes
コンパイル時間 271 ms
コンパイル使用メモリ 82,904 KB
実行使用メモリ 125,156 KB
最終ジャッジ日時 2025-03-31 17:53:59
合計ジャッジ時間 6,865 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 30 WA * 6
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from collections import deque

def main():
    input = sys.stdin.read().split()
    idx = 0
    N = int(input[idx]); idx +=1
    M = int(input[idx]); idx +=1
    s = int(input[idx])-1; t = int(input[idx+1])-1; k = int(input[idx+2]); idx +=3

    adj = [[] for _ in range(N)]
    for _ in range(M):
        a = int(input[idx])-1
        b = int(input[idx+1])-1
        adj[a].append(b)
        adj[b].append(a)
        idx +=2

    color = [-1] * N
    components = []
    is_bipartite = True

    for start in range(N):
        if color[start] == -1:
            queue = deque()
            queue.append(start)
            color[start] = 0
            comp = []
            while queue:
                u = queue.popleft()
                comp.append(u)
                for v in adj[u]:
                    if color[v] == -1:
                        color[v] = color[u] ^ 1
                        queue.append(v)
                    elif color[v] == color[u]:
                        pass
            components.append(comp)

    s_comp_idx = -1
    t_comp_idx = -1
    for i, comp in enumerate(components):
        if s in comp:
            s_comp_idx = i
        if t in comp:
            t_comp_idx = i

    parity_ok = True
    if s_comp_idx == t_comp_idx:
        if (color[s] ^ color[t]) != (k % 2):
            parity_ok = False
    else:
        pass

    if not parity_ok:
        print("No")
        return

    dist_even = [-1] * N
    dist_odd = [-1] * N
    q = deque()
    q.append((s, 0))
    dist_even[s] = 0

    while q:
        u, parity = q.popleft()
        current_dist = dist_even[u] if parity == 0 else dist_odd[u]
        for v in adj[u]:
            new_parity = 1 - parity
            if new_parity == 0:
                if dist_even[v] == -1 or (current_dist + 1) < dist_even[v]:
                    dist_even[v] = current_dist + 1
                    q.append((v, new_parity))
            else:
                if dist_odd[v] == -1 or (current_dist + 1) < dist_odd[v]:
                    dist_odd[v] = current_dist + 1
                    q.append((v, new_parity))

    target_parity = k % 2
    found = False
    if target_parity == 0:
        if dist_even[t] != -1 and dist_even[t] <= k:
            found = True
    else:
        if dist_odd[t] != -1 and dist_odd[t] <= k:
            found = True

    if found:
        print("Yes")
        return

    if M > 0:
        print("Unknown")
    else:
        print("No")

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