結果

問題 No.583 鉄道同好会
ユーザー MisterMister
提出日時 2020-08-02 15:36:12
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 49 ms / 2,000 ms
コード長 1,837 bytes
コンパイル時間 1,187 ms
コンパイル使用メモリ 98,016 KB
実行使用メモリ 5,016 KB
最終ジャッジ日時 2023-09-25 19:03:37
合計ジャッジ時間 2,817 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 13 ms
4,376 KB
testcase_12 AC 18 ms
4,380 KB
testcase_13 AC 17 ms
4,376 KB
testcase_14 AC 17 ms
4,380 KB
testcase_15 AC 22 ms
4,376 KB
testcase_16 AC 40 ms
4,836 KB
testcase_17 AC 49 ms
5,016 KB
testcase_18 AC 48 ms
5,012 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <numeric>
#include <vector>
#include <map>

template <class T>
std::map<T, int> compress(std::vector<T>& v) {
    std::sort(v.begin(), v.end());
    v.erase(std::unique(v.begin(), v.end()), v.end());

    std::map<T, int> rev;
    for (int i = 0; i < (int)v.size(); ++i) rev[v[i]] = i;
    return rev;
}

struct UnionFind {
    std::vector<int> par, sz;
    int gnum;

    explicit UnionFind(int n)
        : par(n), sz(n, 1), gnum(n) {
        std::iota(par.begin(), par.end(), 0);
    }

    int find(int v) {
        return (par[v] == v) ? v : (par[v] = find(par[v]));
    }

    void unite(int u, int v) {
        u = find(u), v = find(v);
        if (u == v) return;

        if (sz[u] < sz[v]) std::swap(u, v);
        sz[u] += sz[v];
        par[v] = u;
        --gnum;
    }

    bool same(int u, int v) { return find(u) == find(v); }
    bool ispar(int v) { return v == find(v); }
    int size(int v) { return sz[find(v)]; }
};

void solve() {
    int n, m;
    std::cin >> n >> m;

    std::vector<std::pair<int, int>> es(m);
    std::vector<int> vs;
    for (auto& e : es) {
        std::cin >> e.first >> e.second;
        vs.push_back(e.first);
        vs.push_back(e.second);
    }

    auto vrev = compress(vs);
    n = vs.size();

    UnionFind uf(n);
    std::vector<int> deg(n, 0);
    for (auto& e : es) {
        e.first = vrev[e.first];
        e.second = vrev[e.second];

        uf.unite(e.first, e.second);
        ++deg[e.first];
        ++deg[e.second];
    }

    int onum = std::count_if(deg.begin(), deg.end(),
                             [](auto d) { return d % 2 != 0; });

    std::cout << (uf.gnum == 1 && onum <= 2 ? "YES" : "NO") << "\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0