結果
| 問題 |
No.583 鉄道同好会
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-10-28 16:24:40 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 21 ms / 2,000 ms |
| コード長 | 1,447 bytes |
| コンパイル時間 | 1,657 ms |
| コンパイル使用メモリ | 169,612 KB |
| 実行使用メモリ | 5,248 KB |
| 最終ジャッジ日時 | 2024-11-22 03:28:17 |
| 合計ジャッジ時間 | 2,614 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 16 |
ソースコード
#include <bits/stdc++.h>
using namespace std;
struct UnionFind{
vector<int> data;
UnionFind(int size) : data(size, -1){}
void unite(int x, int y){
x = root(x);
y = root(y);
if (x != y){
if (data[y] < data[x]) swap(x, y);
data[x] += data[y];
data[y] = x;
}
}
int root(int x) {return data[x] < 0 ? x : data[x] = root(data[x]);}
int size(int x) {return -data[root(x)];}
};
bool is_connected(UnionFind & uf){
int root = -1;
for (int i = 0; i < uf.data.size(); ++i){
int r = uf.root(i);
if (r == i) continue;
if (root == -1){
root = r;
}else if (root != r){
return false;
}
}
return true;
}
bool has_one_stroke_path(const vector<int> & degree){
int count_1 = 0;
for (auto & d : degree){
if (d & 1) ++count_1;
}
if (count_1 == 0 or count_1 == 2){
return true;
}else{
return false;
}
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int N, M;
cin >> N >> M;
vector<int> degree(N, 0);
UnionFind uf(N);
for (int i = 0; i < M; ++i){
int a, b;
cin >> a >> b;
++degree[a];
++degree[b];
uf.unite(a, b);
}
if (is_connected(uf) and has_one_stroke_path(degree)){
cout << "YES" << endl;
}else{
cout << "NO" << endl;
}
return 0;
}