結果

問題 No.2674 k-Walk on Bipartite
ユーザー lam6er
提出日時 2025-04-16 15:53:08
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 3,625 bytes
コンパイル時間 308 ms
コンパイル使用メモリ 82,348 KB
実行使用メモリ 144,660 KB
最終ジャッジ日時 2025-04-16 15:54:04
合計ジャッジ時間 8,679 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 26 WA * 10
権限があれば一括ダウンロードができます

ソースコード

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)]
    original_edges = set()
    for _ in range(m):
        a, b = map(int, sys.stdin.readline().split())
        edges[a].append(b)
        edges[b].append(a)
        if a > b:
            a, b = b, a
        original_edges.add((a, b))
    
    # Step 1: Determine bipartite colors
    color = [-1] * (n + 1)
    color[s] = 0
    q = deque([s])
    while q:
        u = q.popleft()
        for v in edges[u]:
            if color[v] == -1:
                color[v] = 1 - color[u]
                q.append(v)
    
    # Check parity condition
    if (color[s] == color[t]) != (k % 2 == 0):
        print("No")
        return
    
    # Step 2: BFS to find shortest path and connected component
    visited = [False] * (n + 1)
    prev = [-1] * (n + 1)
    distance = {s: 0}
    visited[s] = True
    q = deque([s])
    cc = set()
    cc.add(s)
    while q:
        u = q.popleft()
        for v in edges[u]:
            if not visited[v]:
                visited[v] = True
                distance[v] = distance[u] + 1
                prev[v] = u
                q.append(v)
                cc.add(v)
    
    if t not in cc:
        # Not connected in F, check complete bipartite
        if color[s] == color[t]:
            if k >= 2 and k % 2 == 0:
                print("Unknown")
            else:
                print("No")
        else:
            if k >= 1 and k % 2 == 1:
                print("Unknown")
            else:
                print("No")
        return
    
    d = distance[t]
    if d > k:
        # Check complete bipartite
        if color[s] == color[t]:
            if k >= 2 and k % 2 == 0:
                print("Unknown")
            else:
                print("No")
        else:
            if k >= 1 and k % 2 == 1:
                print("Unknown")
            else:
                print("No")
        return
    
    if d == k:
        print("Yes")
        return
    
    # Check if the connected component has a cycle
    V = len(cc)
    E = 0
    for a, b in original_edges:
        if a in cc and b in cc:
            E += 1
    if E >= V:
        print("Yes")
        return
    
    # Tree case: check (k - d) even and path has a node with degree >= 2
    if (k - d) % 2 != 0:
        # Check complete bipartite
        if color[s] == color[t]:
            if k >= 2 and k % 2 == 0:
                print("Unknown")
            else:
                print("No")
        else:
            if k >= 1 and k % 2 == 1:
                print("Unknown")
            else:
                print("No")
        return
    
    # Reconstruct path
    path = []
    current = t
    while current != s:
        path.append(current)
        current = prev[current]
    path.append(s)
    path.reverse()
    
    # Compute degrees
    degree = [0] * (n + 1)
    for a, b in original_edges:
        if a in cc and b in cc:
            degree[a] += 1
            degree[b] += 1
    
    # Check if any node in path has degree >= 2
    found = False
    for u in path:
        if degree[u] >= 2:
            found = True
            break
    if found:
        print("Yes")
        return
    
    # Check complete bipartite
    if color[s] == color[t]:
        if k >= 2 and k % 2 == 0:
            print("Unknown")
        else:
            print("No")
    else:
        if k >= 1 and k % 2 == 1:
            print("Unknown")
        else:
            print("No")

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