結果

問題 No.2674 k-Walk on Bipartite
ユーザー lam6er
提出日時 2025-04-15 22:43:34
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,873 bytes
コンパイル時間 161 ms
コンパイル使用メモリ 82,412 KB
実行使用メモリ 124,784 KB
最終ジャッジ日時 2025-04-15 22:44:49
合計ジャッジ時間 6,541 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])
    ptr += 1
    t = int(input[ptr])
    ptr += 1
    k = int(input[ptr])
    ptr += 1

    edges = []
    adj = [[] for _ in range(N + 1)]
    for _ in range(M):
        a = int(input[ptr])
        ptr += 1
        b = int(input[ptr])
        ptr += 1
        edges.append((a, b))
        adj[a].append(b)
        adj[b].append(a)

    # Compute bipartition
    color = [-1] * (N + 1)
    for node in range(1, N + 1):
        if color[node] == -1:
            q = deque()
            q.append(node)
            color[node] = 0
            while q:
                u = q.popleft()
                for v in adj[u]:
                    if color[v] == -1:
                        color[v] = color[u] ^ 1
                        q.append(v)
                    elif color[v] == color[u]:
                        pass  # F is bipartite, so this won't happen

    same_color = (color[s] == color[t])
    required_parity = k % 2

    # Check parity
    if same_color:
        if required_parity != 0:
            print("No")
            return
    else:
        if required_parity != 1:
            print("No")
            return

    # Check reachability in F
    dist = [-1] * (N + 1)
    q = deque()
    q.append(s)
    dist[s] = 0
    while q:
        u = q.popleft()
        for v in adj[u]:
            if dist[v] == -1:
                dist[v] = dist[u] + 1
                q.append(v)

    if dist[t] == -1:
        # s and t are not connected in F
        # Check complete bipartition
        if same_color:
            min_complete = 2
            if k >= min_complete and (k % 2 == 0):
                print("Unknown")
            else:
                print("No")
        else:
            min_complete = 1
            if k >= min_complete and (k % 2 == 1):
                print("Unknown")
            else:
                print("No")
        return
    else:
        d = dist[t]
        if d % 2 != k % 2:
            print("No")
            return
        else:
            if k >= d and (k - d) % 2 == 0:
                print("Yes")
                return
            else:
                # Check complete bipartition
                if same_color:
                    min_complete = 2
                    if k >= min_complete and (k - min_complete) % 2 == 0:
                        print("Unknown")
                    else:
                        print("No")
                else:
                    min_complete = 1
                    if k >= min_complete and (k - min_complete) % 2 == 0:
                        print("Unknown")
                    else:
                        print("No")
                return

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