結果
| 問題 |
No.583 鉄道同好会
|
| コンテスト | |
| ユーザー |
kyuna
|
| 提出日時 | 2019-08-12 07:20:58 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 55 ms / 2,000 ms |
| コード長 | 1,351 bytes |
| コンパイル時間 | 892 ms |
| コンパイル使用メモリ | 77,964 KB |
| 実行使用メモリ | 6,948 KB |
| 最終ジャッジ日時 | 2024-09-14 18:31:00 |
| 合計ジャッジ時間 | 2,137 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge6 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 16 |
ソースコード
#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; }
};
bool euler_path(vector<vector<int>> &g, int s = -1) {
vector<int> cnt(2);
for (auto &v: g) cnt[v.size() % 2]++;
if (cnt[1] == 0) return true;
if (cnt[1] == 2 && (s == -1 || g[s].size() % 2 == 1)) return true;
return false;
}
int main() {
int n, m; cin >> n >> m;
vector<vector<int>> g(n);
UnionFind uf(n);
while (m--) {
int a, b; cin >> a >> b;
g[a].emplace_back(b);
g[b].emplace_back(a);
uf.unionSet(a, b);
}
int cnt = 0;
for (int i = 0; i < n; i++) {
if (uf.root(i) != i) continue;
if (uf.size(i) == 1) continue;
cnt++;
}
cout << (euler_path(g) && cnt == 1 ? "YES" : "NO") << endl;
return 0;
}
kyuna