結果

問題 No.2664 Prime Sum
ユーザー みここみここ
提出日時 2024-03-08 21:05:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 2,289 bytes
コンパイル時間 775 ms
コンパイル使用メモリ 84,252 KB
実行使用メモリ 6,676 KB
最終ジャッジ日時 2024-03-08 21:05:19
合計ジャッジ時間 1,892 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 2 ms
6,676 KB
testcase_03 AC 2 ms
6,676 KB
testcase_04 AC 2 ms
6,676 KB
testcase_05 AC 2 ms
6,676 KB
testcase_06 AC 2 ms
6,676 KB
testcase_07 AC 2 ms
6,676 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 2 ms
6,676 KB
testcase_10 AC 2 ms
6,676 KB
testcase_11 AC 1 ms
6,676 KB
testcase_12 AC 2 ms
6,676 KB
testcase_13 AC 3 ms
6,676 KB
testcase_14 AC 2 ms
6,676 KB
testcase_15 AC 2 ms
6,676 KB
testcase_16 AC 2 ms
6,676 KB
testcase_17 AC 2 ms
6,676 KB
testcase_18 AC 2 ms
6,676 KB
testcase_19 AC 2 ms
6,676 KB
testcase_20 AC 2 ms
6,676 KB
testcase_21 AC 3 ms
6,676 KB
testcase_22 AC 2 ms
6,676 KB
testcase_23 AC 3 ms
6,676 KB
testcase_24 AC 3 ms
6,676 KB
testcase_25 AC 2 ms
6,676 KB
testcase_26 AC 2 ms
6,676 KB
testcase_27 AC 2 ms
6,676 KB
testcase_28 AC 2 ms
6,676 KB
testcase_29 AC 2 ms
6,676 KB
testcase_30 AC 2 ms
6,676 KB
testcase_31 AC 1 ms
6,676 KB
testcase_32 AC 2 ms
6,676 KB
testcase_33 AC 2 ms
6,676 KB
testcase_34 AC 2 ms
6,676 KB
testcase_35 AC 2 ms
6,676 KB
testcase_36 AC 2 ms
6,676 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cassert>
#include <queue>
using namespace std;

template <typename T = int>
struct Graph
{
public:
    using value_type = T;

    struct Edge
    {
        int from, to;
        T cost;
        int id;

        operator int() const
        {
            return to;
        }
    };

    Graph() {}

    Graph(int n) : n(n), m(0), g(n) {}

    void add_directed_edge(int from, int to, T cost = 1)
    {
        assert(0 <= from && from < n);
        assert(0 <= to && to < n);
        g[from].push_back((Edge){from, to, cost, m++});
    }

    void add_undirected_edge(int from, int to, T cost = 1)
    {
        assert(0 <= from && from < n);
        assert(0 <= to && to < n);
        g[from].push_back((Edge){from, to, cost, m});
        g[to].push_back((Edge){to, from, cost, m++});
    }

    int size()
    {
        return n;
    }

    int edge_size()
    {
        return m;
    }

    inline const std::vector<Edge> &operator[](const int &u) const
    {
        return g[u];
    }

    inline std::vector<Edge> &operator[](const int &u)
    {
        return g[u];
    }

private:
    int n, m;
    std::vector<std::vector<Edge>> g;
};

std::vector<int> is_bipartite(Graph<int> &g)     // b[n] == 1 <=> g is bipartite
{
    int n = g.size();
    std::vector<int> b(n + 1, -1);
    b[n] = 0;
    std::queue<int> que;
    for (int i = 0; i < n; i++)
    {
        if (b[i] != -1)
        {
            continue;
        }
        b[i] = 0;
        que.push(i);
        while (que.size())
        {
            int u = que.front();
            que.pop();
            for (int v : g[u])
            {
                if (b[u] == b[v])
                {
                    return b;
                }
                if (b[v] == -1)
                {
                    b[v] = 1 - b[u];
                    que.push(v);
                }
            }
        }
    }
    b[n] = 1;
    return b;
}

int main()
{
    int n, m;
    cin >> n >> m;
    Graph g(n);
    for(int i = 0; i < m; i++)
    {
        int a, b;
        cin >> a >> b;
        a--;
        b--;
        g.add_undirected_edge(a, b);
    }
    vector<int> b = is_bipartite(g);
    if(b[n])
    {
        cout << "Yes" << endl;
    }
    else
    {
        cout << "No" << endl;
    }
}
0