結果

問題 No.483 マッチ並べ
ユーザー startcppstartcpp
提出日時 2017-02-14 18:22:48
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,880 bytes
コンパイル時間 632 ms
コンパイル使用メモリ 74,288 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-28 16:51:23
合計ジャッジ時間 3,424 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,376 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 2 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 2 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 2 ms
4,380 KB
testcase_30 AC 2 ms
4,380 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 2 ms
4,376 KB
testcase_33 AC 2 ms
4,380 KB
testcase_34 AC 2 ms
4,380 KB
testcase_35 AC 2 ms
4,376 KB
testcase_36 AC 2 ms
4,380 KB
testcase_37 AC 2 ms
4,376 KB
testcase_38 AC 2 ms
4,380 KB
testcase_39 AC 2 ms
4,376 KB
testcase_40 AC 2 ms
4,376 KB
testcase_41 AC 2 ms
4,380 KB
testcase_42 AC 1 ms
4,376 KB
testcase_43 AC 2 ms
4,376 KB
testcase_44 AC 2 ms
4,376 KB
testcase_45 AC 2 ms
4,380 KB
testcase_46 AC 2 ms
4,376 KB
testcase_47 AC 2 ms
4,376 KB
testcase_48 AC 2 ms
4,380 KB
testcase_49 AC 1 ms
4,380 KB
testcase_50 AC 2 ms
4,380 KB
testcase_51 AC 2 ms
4,380 KB
testcase_52 AC 2 ms
4,380 KB
testcase_53 AC 1 ms
4,376 KB
testcase_54 AC 2 ms
4,380 KB
testcase_55 AC 2 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <map>
using namespace std;
typedef pair<int, int> P;

class UF {
	int par[1000];
public:
	UF() { for (int i = 0; i < 1000; i++) { par[i] = i; } }
	int root(int x) { if (par[x] == x) return x; return par[x] = root(par[x]); }
	void marge(int x, int y) {
		x = root(x);
		y = root(y);
		if (x == y) return;
		par[x] = y;
	}
	bool is_same(int x, int y) { return root(x) == root(y); }
}uf;

vector<int> et[1000];

void input() {
	int n;
	map<P, int> toId;
	
	cin >> n;
	
	int id = 0;
	for (int i = 0; i < n; i++) {
		int x1, y1, x2, y2;
		cin >> x1 >> y1 >> x2 >> y2;
		if (toId.find(P(x1, y1)) == toId.end()) { toId[P(x1, y1)] = id; id++; }
		if (toId.find(P(x2, y2)) == toId.end()) { toId[P(x2, y2)] = id; id++; }
		
		int src = toId[P(x1, y1)];
		int dst = toId[P(x2, y2)];
		//頂点src, dstに辺を貼る.
		et[src].push_back(dst);
		et[dst].push_back(src);
		uf.marge(src, dst);
	}
}

int main() {
	input();
	
	int i, j;
	for (i = 0; i < 200; i++) {
		//頂点iの連結成分の{頂点数, 辺の個数}を数える
		vector<int> vec;
		for (j = 0; j < 200; j++) { if (uf.is_same(i, j)) vec.push_back(j); }
		int ecnt = 0;
		for (j = 0; j < vec.size(); j++) { ecnt += et[vec[j]].size(); }
		if (ecnt > vec.size() * 2) {
			cout << "NO" << endl;
			return 0;
		}
	}
	cout << "YES" << endl;
	return 0;
}

//難しく考えすぎた…
//言い換え:
//マッチ棒の端点を頂点とし, マッチ棒を矢印(図薬が終点)で表示すると, 有向グラフになる。この有向グラフについて, どの頂点の入次数も1以下になるように, 
//矢印の向きを決めたい。可能か?
//自明な考察:
//①全てのマッチ棒を置いてから、各マッチ棒の(好きな)1端点に頭薬を塗ると考えても同じ。無向グラフが与えられ, 辺の向きをこれから決める~ということになる。
//②連結成分ごとに分けて, 独立に判定してもよい。
//①②より, 連結な単純無向グラフについて, 矢印の向きを決める問題が解ければよいことが分かった。どうやって?
//考察:
//N頂点の連結な単純無向グラフGについて、辺の数をMとおく。まず、M >= N - 1である。
//M = N - 1のとき、Gは木なのでOK. (根から葉に向ければよい)
//M = Nのとき、GはなもりグラフなのでOK. (サイクル+根から葉に向ける)
//M > Nのとき, Gの入次数の和はM(>N). 鳩ノ巣原理よりどう頑張っても入次数2以上の頂点ができてしまう. よって, NG.
//まとめる:
//マッチ棒の端点を頂点とし, マッチ棒を無向辺としたときのグラフG = <V, E>を考える。各連結成分について「辺の数 <= 頂点数」かを判定し、全てTrueなら可能, 
//そうでないなら不可能.
0