結果

問題 No.483 マッチ並べ
ユーザー masa
提出日時 2017-02-11 00:53:09
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 2,000 ms
コード長 1,847 bytes
コンパイル時間 1,518 ms
コンパイル使用メモリ 96,124 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-29 11:41:11
合計ジャッジ時間 2,995 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 53
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <utility>

using namespace std;

typedef pair<int, int> PII;

typedef struct __match {
	int x1;
	int y1;
	int x2;
	int y2;
} Match;

bool touches(Match m, int x, int y) {
	if ((m.x1 == x && m.y1 == y) || (m.x2 == x && m.y2 == y)) {
		return true;
	} else {
		return false;
	}
}

bool touches(Match a, Match b) {
	if (touches(a, b.x1, b.y1) || touches(a, b.x2, b.y2)) {
		return true;
	} else {
		return false;
	}
}

void show(map<PII, set<int>> mps) {
	printf("----------\n");
	for (auto ppp : mps) {
		PII p = ppp.first;
		printf("(%d, %d): ", p.first, p.second);
		for (auto e : ppp.second) {
			printf("%d ", e);
		}
		printf("\n");
	}
}

int main() {
	int n, a, b, c, d;
	cin >> n;

	vector<Match> matches(n);
	for (int i = 0; i < n; i++) {
		cin >> a >> b >> c >> d;
		Match m = {a, b, c, d};
		matches[i] = m;
	}

	map<PII, set<int>> have_match;
	for (int i = 0; i < n; i++) {
		Match m = matches[i];
		have_match[make_pair(m.x1, m.y1)].insert(i);
		have_match[make_pair(m.x2, m.y2)].insert(i);
	}


	bool updates = true;
	do {
		updates = false;
		for (auto xxx : have_match) {
			if (xxx.second.size() > 1) {
				continue;
			}
// show(have_match);
			if (xxx.second.size() == 0) {
				have_match.erase(xxx.first);
				updates = true;
				break;
			}
			PII p = xxx.first;
			int idx = *(xxx.second.begin());
			Match m = matches[idx];

			PII q;
			if (p == make_pair(m.x1, m.y1)) {
				q = make_pair(m.x2, m.y2);
			} else {
				q = make_pair(m.x1, m.y1);
			}

			have_match[p].erase(idx);
			have_match[q].erase(idx);
			updates = true;
			break;
		}
	} while (updates);

	int maxi = 0;
	for (auto x : have_match) {
		maxi = max(maxi, (int)x.second.size());
	}

	cout << (maxi > 2 ? "NO" : "YES") << endl;
	return 0;
}
0