結果

問題 No.583 鉄道同好会
ユーザー srup٩(๑`н´๑)۶srup٩(๑`н´๑)۶
提出日時 2017-10-27 23:42:07
言語 C++11
(gcc 11.4.0)
結果
RE  
実行時間 -
コード長 2,293 bytes
コンパイル時間 1,732 ms
コンパイル使用メモリ 169,320 KB
実行使用メモリ 17,152 KB
最終ジャッジ日時 2024-05-01 17:57:49
合計ジャッジ時間 2,497 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 1 ms
5,376 KB
testcase_11 AC 13 ms
5,376 KB
testcase_12 AC 16 ms
5,376 KB
testcase_13 AC 17 ms
5,376 KB
testcase_14 AC 17 ms
5,376 KB
testcase_15 AC 21 ms
5,376 KB
testcase_16 AC 38 ms
5,376 KB
testcase_17 AC 50 ms
5,376 KB
testcase_18 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vint;
typedef pair<int,int> pint;
typedef vector<pint> vpint;
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=n-1;i>=(0);i--)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
#define all(v) (v).begin(),(v).end()
#define eall(v) unique(all(v), v.end())
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define chmax(a, b) a = (((a)<(b)) ? (b) : (a))
#define chmin(a, b) a = (((a)>(b)) ? (b) : (a))
const int MOD = 1e9 + 7;
const int INF = 1e9;
const ll INFF = 1e18;

int Sa[300000], Sb[300000];
int memo[10100];
class DisjointSet{
public:
	vector<int> rank, p;//rank:木の高さ p:親の頂点番号
	DisjointSet(){}
	DisjointSet(int size){//頂点の数
		rank.resize(size, 0);
		p.resize(size, 0);
		rep(i, size) makeSet(i);
	}
	bool same(int x, int y){ //同じ木にあるか
		return findSet(x) == findSet(y);
	}
	void unite(int x, int y){ // 木どうしをくっつける
		link(findSet(x), findSet(y));
	}
private:
	void makeSet(int x){
		p[x] = x;
		rank[x] = 0;
	}
	int findSet(int x){ //親を探す(ルートまで)
		if(x != p[x]){
			p[x] = findSet(p[x]);
		}
		return p[x];
	}
	void link(int x, int y){ //木の高さを考慮して木どうしをくっつける
		if(rank[x] > rank[y]){
			p[y] = x;
		}else{
			p[x] = y;
			if(rank[x] == rank[y]) rank[y]++;
		}
	}
};

set<int> se[10100];

int main(void) {
	int N, M; cin >> N >> M;
	rep(i, M) cin >> Sa[i] >> Sb[i];
	rep(i, M) memo[Sa[i]]++, memo[Sb[i]]++;
	
	int cnt = 0;
	rep(i, N) if(memo[i] % 2) cnt++;
	if (cnt == 2) printf("YES\n");
	else if(cnt == 0) {
		rep(i, M) se[Sa[i]].insert(2 * i), se[Sb[i]].insert(2 * i + 1);
		// 連結か見る
		DisjointSet uf(100010);
		rep(i, N) uf.unite(2 * i, 2 * i + 1);
		rep(i, N) {
			// printf("i %d\n", i);
			for(auto u : se[i]) for(auto v : se[i]) {
				// printf("u %d v %d\n", u, v);
				uf.unite(u, v);
			}
		}

		bool f = true;
		rep(i, 2 * M)rep(j, 2 * M) {
			auto d = uf.same(i, j);
			// printf("%d %d %d\n", i, j, d);
			if(!d) f = false;
		}
		if(f) printf("YES\n");
		else printf("NO\n");
	}
	else printf("NO\n");
	
	// printf("YES\n");
	return 0;
}
0