結果

問題 No.2674 k-Walk on Bipartite
ユーザー nono00nono00
提出日時 2024-03-15 23:13:18
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 47 ms / 2,000 ms
コード長 5,007 bytes
コンパイル時間 2,693 ms
コンパイル使用メモリ 262,016 KB
実行使用メモリ 10,984 KB
最終ジャッジ日時 2024-09-30 02:46:25
合計ジャッジ時間 4,430 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,248 KB
testcase_03 AC 1 ms
5,248 KB
testcase_04 AC 1 ms
5,248 KB
testcase_05 AC 1 ms
5,248 KB
testcase_06 AC 2 ms
5,248 KB
testcase_07 AC 26 ms
8,196 KB
testcase_08 AC 36 ms
9,068 KB
testcase_09 AC 22 ms
7,788 KB
testcase_10 AC 46 ms
10,604 KB
testcase_11 AC 28 ms
7,788 KB
testcase_12 AC 45 ms
10,708 KB
testcase_13 AC 22 ms
7,400 KB
testcase_14 AC 8 ms
5,248 KB
testcase_15 AC 47 ms
10,984 KB
testcase_16 AC 31 ms
8,936 KB
testcase_17 AC 34 ms
8,932 KB
testcase_18 AC 13 ms
6,116 KB
testcase_19 AC 27 ms
8,424 KB
testcase_20 AC 23 ms
7,908 KB
testcase_21 AC 37 ms
9,324 KB
testcase_22 AC 47 ms
10,984 KB
testcase_23 AC 2 ms
5,248 KB
testcase_24 AC 2 ms
5,248 KB
testcase_25 AC 2 ms
5,248 KB
testcase_26 AC 1 ms
5,248 KB
testcase_27 AC 2 ms
5,248 KB
testcase_28 AC 2 ms
5,248 KB
testcase_29 AC 2 ms
5,248 KB
testcase_30 AC 2 ms
5,248 KB
testcase_31 AC 1 ms
5,248 KB
testcase_32 AC 2 ms
5,248 KB
testcase_33 AC 2 ms
5,248 KB
testcase_34 AC 2 ms
5,248 KB
testcase_35 AC 2 ms
5,248 KB
testcase_36 AC 1 ms
5,248 KB
testcase_37 AC 1 ms
5,248 KB
testcase_38 AC 1 ms
5,248 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#include <iterator>
#include <ranges>
#include <vector>

namespace nono {

template <class T>
struct EdgeBase {
    int from;
    int to;
    T weight;
    EdgeBase() {}
    EdgeBase(int from, int to, T weight = 1): from(from), to(to), weight(weight) {}
};

using Edge = EdgeBase<int>;
template <class T>
using WeightedEdge = EdgeBase<T>;

template <class T>
class Graph {
    struct Edge_ {
        int to;
        T weight;
        int id;
    };

    using iterator = std::vector<Edge_>::iterator;
    using const_iterator = std::vector<Edge_>::const_iterator;
    using subrange = std::ranges::subrange<iterator, iterator>;
    using const_subrange = std::ranges::subrange<const_iterator, const_iterator>;

  public:
    template <class U>
    friend Graph<U> to_undirected_graph(int n, const std::vector<EdgeBase<U>>& edges);
    template <class U>
    friend Graph<U> to_directed_graph(int n, const std::vector<EdgeBase<U>>& edges);

    subrange operator[](int i) {
        return std::ranges::subrange(edges_.begin() + indptr_[i], edges_.begin() + indptr_[i + 1]);
    }
    const_subrange operator[](int i) const {
        return std::ranges::subrange(edges_.begin() + indptr_[i], edges_.begin() + indptr_[i + 1]);
    }

    int size() const {
        return n_;
    }
    int edge_size() const {
        return m_;
    }
    bool is_directed() const {
        return directed_;
    }
    bool is_undirected() const {
        return !is_directed();
    }

  private:
    Graph(int n, const std::vector<EdgeBase<T>>& edges, bool directed)
        : n_(n),
          m_(edges.size()),
          indptr_(n_ + 1),
          edges_(directed ? edges.size() : 2 * edges.size()),
          directed_(directed) {
        for (const auto& e: edges) {
            indptr_[e.from + 1]++;
            if (!directed_) indptr_[e.to + 1]++;
        }
        for (int i = 0; i < n_; i++) {
            indptr_[i + 1] += indptr_[i];
        }
        auto index = indptr_;
        for (int i = 0; i < std::ssize(edges); i++) {
            const auto& e = edges[i];
            edges_[index[e.from]++] = Edge_(e.to, e.weight, i);
            if (!directed_) edges_[index[e.to]++] = Edge_(e.from, e.weight, i);
        }
    }

    int n_;
    int m_;
    std::vector<int> indptr_;
    std::vector<Edge_> edges_;
    bool directed_;
};

template <class T>
Graph<T> to_undirected_graph(int n, const std::vector<EdgeBase<T>>& edges) {
    return Graph<T>(n, edges, false);
}

template <class T>
Graph<T> to_directed_graph(int n, const std::vector<EdgeBase<T>>& edges) {
    return Graph<T>(n, edges, true);
}

}  //  namespace nono

#include <limits>
#include <queue>
#include <vector>

namespace nono {

template <class T>
std::vector<T> bfs(const Graph<T>& graph, int source) {
    constexpr T NONE = std::numeric_limits<T>::min();

    std::vector<T> dist(graph.size(), NONE);
    dist[source] = 0;
    std::queue<int> que;
    que.push(source);

    while (!que.empty()) {
        int u = que.front();
        que.pop();
        for (const auto& e: graph[u]) {
            if (dist[e.to] == NONE) {
                dist[e.to] = dist[u] + e.weight;
                que.push(e.to);
            }
        }
    }

    return dist;
}

}  //  namespace nono

namespace nono {

void solve() {
    int n, m;
    int s, t, k;
    std::cin >> n >> m >> s >> t >> k;
    if (n == 1) {
        std::cout << "No" << '\n';
        return;
    }
    if (n == 2) {
        if (s == t) {
            if (k % 2 == 0) {
                if (m == 1) {
                    std::cout << "Yes" << '\n';
                } else {
                    std::cout << "Unknown" << '\n';
                }
            } else {
                std::cout << "No" << '\n';
            }
        } else {
            if (k % 2 == 0) {
                std::cout << "No" << '\n';
            } else if (m == 1) {
                std::cout << "Yes" << '\n';
            } else {
                std::cout << "Unknown" << '\n';
            }
        }
        return;
    }
    s--;
    t--;
    std::vector<Edge> edges;
    for (int i = 0; i < m; i++) {
        int u, v;
        std::cin >> u >> v;
        u--;
        v--;
        edges.emplace_back(u, v);
    }
    auto graph = to_undirected_graph(n, edges);
    auto dist = bfs(graph, s);
    int count = 0;
    for (int i = 0; i < n; i++) {
        if (dist[i] >= 0) {
            count++;
        }
    }

    if (dist[t] < 0) {
        std::cout << "Unknown" << '\n';
        return;
    }

    if (dist[t] % 2 != k % 2) {
        std::cout << "No" << '\n';
        return;
    }

    if (count == 1) {
        std::cout << "Unknown" << '\n';
        return;
    }

    if (dist[t] <= k) {
        std::cout << "Yes" << '\n';
        return;
    }
    std::cout << "Unknown" << '\n';
}

}  //  namespace nono

int main() {
    std::cin.tie(0)->sync_with_stdio(0);
    std::cout << std::fixed << std::setprecision(16);
    int t = 1;

    while (t--) nono::solve();
}
0