結果

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

ソースコード

diff #

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

using namespace std;

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(vector<set<int>> vs) {
	printf("----------\n");
	for (int i = 0; i < vs.size(); i++) {
		printf("%2d: ", i);
		for (auto e : vs[i]) {
			printf("%2d ", 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;
	}

	vector<set<int>> connect(n);
	for (int i = 0; i < n; i++) {
		Match m1 = matches[i];
		for (int j = i + 1; j < n; j++) {
			Match m2 = matches[j];

			if (touches(m1, m2)) {
				connect[i].insert(j);
				connect[j].insert(i);
			}
		}
	}

	bool updates = true;
	do {
		updates = false;
		for (int i = 0; i < n; i++) {
			if (connect[i].size() == 1) {
				int tmp = *(connect[i].begin());
				connect[i].erase(tmp);
				connect[tmp].erase(i);
				updates = true;
			}
		}
	} while (updates);

	int maxi = 0;
	for (int i = 0; i < n; i++) {
		int next1 = 0;
		int next2 = 0;
		Match m = matches[i];
		for (auto e : connect[i]) {
			if (touches(matches[e], m.x1, m.y1)) {
				next1++;
			} else {
				next2++;
			}
		}
		maxi = max({maxi, next1, next2});
	}

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