結果

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

struct BipartiteMatching {
	int V;
	vector<vector<bool> > G;
	vector<int> match;
	vector<bool> used;

	BipartiteMatching(int v) {
		V = v;
		G = vector<vector<bool> >(v, vector<bool>(v));
		match = vector<int>(v);
		used = vector<bool>(v);
	}

	void add_edge(int v, int u) {
		G[v][u] = G[u][v] = true;
	}

	bool dfs(int v) {
		used[v] = true;
		for(int i = 0; i < V; i++) {
			if(!G[v][i]) continue;
			int u = i, w = match[u];
			if(w < 0 || (!used[w] && dfs(w))) {
				match[v] = u;
				match[u] = v;
				return true;
			}
		}
		return false;
	}

	int calc() {
		int res = 0;
		fill(match.begin(), match.end(), -1);
		for(int v = 0; v < V; v++) {
			if(match[v] < 0) {
				fill(used.begin(), used.end(), false);
				if(dfs(v)) {
					res++;
				}
			}
		}
		return res;
	}
};

typedef pair<int, int> P;

int x[100][4];

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	int N;
	cin >> N;

	map<P, int> m;
	for(int i = 0; i < N; i++) {
		for(int j = 0; j < 4; j++) {
			cin >> x[i][j];
		}
		m[{x[i][0], x[i][1]}] = 0;
		m[{x[i][2], x[i][3]}] = 0;
	}

	int cnt = 0;
	for(auto v : m) {
		m[v.first] = cnt++;
	}

	BipartiteMatching bp(N + m.size());
	for(int i = 0; i < N; i++) {
		bp.add_edge(i, N + m[{x[i][0], x[i][1]}]);
		bp.add_edge(i, N + m[{x[i][2], x[i][3]}]);
	}
	if(bp.calc() == N) {
		cout << "YES" << endl;
	}
	else {
		cout << "NO" << endl;
	}
}
0