結果

問題 No.2674 k-Walk on Bipartite
ユーザー gew1fw
提出日時 2025-06-12 16:25:03
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,383 bytes
コンパイル時間 523 ms
コンパイル使用メモリ 82,184 KB
実行使用メモリ 124,988 KB
最終ジャッジ日時 2025-06-12 16:25:09
合計ジャッジ時間 5,724 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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 = [[] for _ in range(N+1)]
    for _ in range(M):
        a = int(input[ptr])
        ptr += 1
        b = int(input[ptr])
        ptr += 1
        edges[a].append(b)
        edges[b].append(a)

    # Compute bipartition and check for cycles
    color = [-1] * (N + 1)
    parent = [-1] * (N + 1)
    has_cycle = False

    for node in range(1, N+1):
        if color[node] == -1:
            q = deque()
            q.append(node)
            color[node] = 0
            parent[node] = -1
            while q:
                u = q.popleft()
                for v in edges[u]:
                    if color[v] == -1:
                        color[v] = color[u] ^ 1
                        parent[v] = u
                        q.append(v)
                    else:
                        if v != parent[u]:
                            has_cycle = True

    same_partition = (color[s] == color[t])

    # Check parity
    if (same_partition and k % 2 != 0) or (not same_partition and k % 2 != 1):
        print("No")
        return

    # Compute minimal distance in F
    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]

    # Check step 3a
    if d != -1:
        if s == t:
            if k >= 2 and k % 2 == 0:
                if has_cycle:
                    print("Yes")
                    return
        else:
            if k >= d and (k - d) % 2 == 0:
                print("Yes")
                return

    # Check step 3b
    if same_partition:
        if s == t:
            complete_exists = (k >= 2 and k % 2 == 0)
        else:
            complete_exists = (k >= 2 and k % 2 == 0)
    else:
        complete_exists = (k >= 1)

    if not complete_exists:
        print("No")
    else:
        print("Unknown")

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