結果

問題 No.2674 k-Walk on Bipartite
ユーザー nu50218
提出日時 2024-03-13 21:08:49
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 211 ms / 2,000 ms
コード長 1,774 bytes
コンパイル時間 1,915 ms
コンパイル使用メモリ 201,332 KB
最終ジャッジ日時 2025-02-20 04:16:16
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 36
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {
    int N, M;
    cin >> N >> M;

    int s, t, k;
    cin >> s >> t >> k;
    s--;
    t--;

    vector<int> adj[N];
    for (int i = 0; i < M; i++) {
        int A, B;
        cin >> A >> B;
        A--;
        B--;
        adj[A].push_back(B);
        adj[B].push_back(A);
    }

    // ① N = 1
    // k > 0 より常に No
    if (N == 1) {
        cout << "No" << endl;
        return 0;
    }

    // ② N = 2
    // H の辺集合としてあり得るのは、以下のみ
    //   - {}
    //   - {{1, 2}}
    // 前者は正の長さの歩道無し。
    // 後者は (s != t) == (k % 2) なら歩道が存在。
    if (N == 2) {
        if (M == 0) {
            cout << ((s != t) == (k % 2) ? "Unknown" : "No") << endl;
            return 0;
        }
        if (M == 1) {
            cout << ((s != t) == (k % 2) ? "Yes" : "No") << endl;
            return 0;
        }
    }

    // ③ N > 2 "基本的な方針"

    // BFS
    queue<int> que;

    const int inf = 1e9;
    vector<int> dist(N, inf);

    que.push(s);
    dist[s] = 0;

    while (!que.empty()) {
        int v = que.front();
        que.pop();

        for (auto &&u : adj[v]) {
            if (dist[u] == inf) {
                que.push(u);
                dist[u] = dist[v] + 1;
            }
        }
    }

    // sとtが同じ連結成分に属する
    if (dist[t] < inf) {
        // 偶奇が一致
        if (dist[t] % 2 == k % 2) {
            cout << (dist[t] <= k ? "Yes" : "Unknown") << endl;
            return 0;
        }
        // 偶奇が一致しない
        cout << "No" << endl;
        return 0;
    }

    // sとtが別の連結成分に属する
    cout << "Unknown" << endl;
    return 0;
}
0