結果

問題 No.583 鉄道同好会
ユーザー autumn-eelautumn-eel
提出日時 2017-10-27 22:56:45
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 22 ms / 2,000 ms
コード長 988 bytes
コンパイル時間 3,469 ms
コンパイル使用メモリ 168,388 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-08-14 04:23:24
合計ジャッジ時間 3,149 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;

int cnt[500];
class UnionFind {
	vector<int>par, high;
public:
	UnionFind(int n) {
		par = high = vector<int>(n);
		for (int i = 0; i < n; i++)par[i] = i;
	}
	int find(int x) {
		if (par[x] == x)return x;
		return par[x] = find(par[x]);
	}
	void unite(int x, int y) {
		x = find(x); y = find(y);
		if (x == y)return;
		if (high[x] > high[y])par[y] = x;
		else {
			par[x] = y;
			if (high[x] == high[y])high[y]++;
		}
	}
	bool same(int x, int y) {
		return find(x) == find(y);
	}
};
int main(){
	int n,m;scanf("%d%d",&n,&m);
	UnionFind uf(n);
	rep(i,m){
		int a,b;scanf("%d%d",&a,&b);
		cnt[a]++;cnt[b]++;
		uf.unite(a,b);
	}
	int c=0,d=0;
	rep(i,n){
		if(cnt[i]%2)c++;
		else d++;
	}
	int t=0;
	rep(i,n){
		if(cnt[i]==0)t++;
	}
	rep(i,n){
		int cnt=0;
		rep(j,n){
			if(uf.same(i,j))cnt++;
		}
		if(cnt!=1&&cnt!=n-t){
			puts("NO");
			return 0;
		}
	}
	if(c==0||c==2)puts("YES");
	else puts("NO");
}
0