結果

問題 No.583 鉄道同好会
ユーザー kyunakyuna
提出日時 2019-08-13 18:37:55
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 53 ms / 2,000 ms
コード長 1,049 bytes
コンパイル時間 662 ms
コンパイル使用メモリ 73,400 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-19 17:13:20
合計ジャッジ時間 1,727 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 2 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 14 ms
4,348 KB
testcase_12 AC 19 ms
4,348 KB
testcase_13 AC 19 ms
4,348 KB
testcase_14 AC 19 ms
4,348 KB
testcase_15 AC 25 ms
4,348 KB
testcase_16 AC 44 ms
4,348 KB
testcase_17 AC 53 ms
4,348 KB
testcase_18 AC 53 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

struct UnionFind {
    vector<int> data;
    int __size;
    UnionFind(int size) : data(size, -1), __size(size) { }
    bool unionSet(int x, int y) {
        if ((x = root(x)) != (y = root(y))) {
            if (data[y] < data[x]) swap(x, y);
            data[x] += data[y]; data[y] = x; __size--;
        }
        return x != y;
    }
    bool findSet(int x, int y) { return root(x) == root(y); }
    int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
    int size(int x) { return -data[root(x)]; }
    int size() { return __size; }
};

int main() {
    int n, m; cin >> n >> m;
    vector<int> deg(n);
    UnionFind uf(n);
    while (m--) {
        int a, b; cin >> a >> b;
        deg[a]++, deg[b]++;
        uf.unionSet(a, b);
    }
    int odd = 0, cnt = 0;
    for (int i = 0; i < n; i++) if (deg[i]) {
        odd += deg[i] % 2;
        cnt += uf.root(i) == i;
    }
    cout << (odd <= 2 && cnt == 1 ? "YES" : "NO") << endl;
    return 0;
}
0