結果

問題 No.1190 Points
ユーザー lam6er
提出日時 2025-04-15 21:35:34
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,521 bytes
コンパイル時間 183 ms
コンパイル使用メモリ 82,372 KB
実行使用メモリ 107,920 KB
最終ジャッジ日時 2025-04-15 21:37:24
合計ジャッジ時間 5,013 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 13 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
    P = int(input[ptr]); ptr += 1
    S = int(input[ptr]); ptr += 1
    G = int(input[ptr]); ptr += 1

    adj = [[] for _ in range(N + 1)]
    for _ in range(M):
        u = int(input[ptr]); ptr += 1
        v = int(input[ptr]); ptr += 1
        adj[u].append(v)
        adj[v].append(u)

    # Compute d_S using BFS from S
    d_S = [-1] * (N + 1)
    q = deque([S])
    d_S[S] = 0
    while q:
        u = q.popleft()
        for v in adj[u]:
            if d_S[v] == -1:
                d_S[v] = d_S[u] + 1
                q.append(v)

    # Compute d_G using BFS from G
    d_G = [-1] * (N + 1)
    q = deque([G])
    d_G[G] = 0
    while q:
        u = q.popleft()
        for v in adj[u]:
            if d_G[v] == -1:
                d_G[v] = d_G[u] + 1
                q.append(v)

    if d_S[G] == -1:
        print(-1)
        return

    D = d_S[G]
    if D > P or (P - D) % 2 != 0:
        print(-1)
        return

    candidates = []
    for u in range(1, N + 1):
        if d_S[u] == -1 or d_G[u] == -1:
            continue
        total = d_S[u] + d_G[u]
        if total <= P and (P - total) % 2 == 0:
            candidates.append(u)

    if not candidates:
        print(-1)
    else:
        candidates.sort()
        print(len(candidates))
        for u in candidates:
            print(u)

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