답안 #713620

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
713620 2023-03-22T16:35:58 Z alex_2008 결혼 문제 (IZhO14_marriage) C++14
100 / 100
514 ms 3164 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <cmath>
using namespace std;
const int N = 50050;
int n, m, k;
vector <int> g[N];
bool used[N];
int mt[N];
bool kuhn(int v, int L, int R) {
	if (used[v]) return false;
	used[v] = true;
	for (auto it : g[v]) {
		if (it >= L && it <= R) {
			if (mt[it] == -1 || kuhn(mt[it], L, R)) {
				mt[it] = v;
				return true;
			}
		}
	}
	return false;
}
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cin >> n >> m >> k;
	for (int i = 1; i <= k; i++) {
		int a, b;
		cin >> a >> b;
		a += m;
		g[a].push_back(b);
		g[b].push_back(a);
	}
	int l = m, r = n, ind = -1;
	while (l <= r) {
		int mid = (l + r) / 2;
		for (int i = 1; i <= mid; i++) {
			mt[i + m] = -1;
		}
		int cnt = 0;
		for (int i = 1; i <= m; i++) {
			for (int j = 1; j <= m; j++) {
				used[j] = false;
			}
			if (kuhn(i, 1, mid + m)) ++cnt;
		}
		if (cnt == m) {
			ind = mid;
			r = mid - 1;
		}
		else l = mid + 1;
	}
	if (ind == -1) {
		cout << "0\n";
		return 0;
	}
	for (int i = 1; i <= n; i++) {
		mt[i + m] = -1;
	}
	for (int i = 1; i <= m; i++) {
		for (int j = 1; j <= m; j++) {
			used[j] = false;
		}
		kuhn(i, 1 + m, ind + m);
	}
	int answ = n - ind + 1;
	for (int i = 1; i <= n; i++) {
		if (mt[i + m] == -1) {
			answ += n - ind + 1;
			continue;
		}
		int girl = mt[i + m];
		while (ind <= n) {
			for (int j = 1; j <= m; j++) {
				used[j] = false;
			}
			if (kuhn(girl, i + 1 + m, ind + m)) break;
			ind++;
		}
		if (ind == n + 1) break;
		answ += n - ind + 1;
	}
	cout << answ << "\n";
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 1492 KB Output is correct
2 Correct 1 ms 1492 KB Output is correct
3 Correct 1 ms 1492 KB Output is correct
4 Correct 1 ms 1492 KB Output is correct
5 Correct 1 ms 1492 KB Output is correct
6 Correct 1 ms 1492 KB Output is correct
7 Correct 1 ms 1492 KB Output is correct
8 Correct 1 ms 1492 KB Output is correct
9 Correct 1 ms 1492 KB Output is correct
10 Correct 1 ms 1492 KB Output is correct
11 Correct 1 ms 1492 KB Output is correct
12 Correct 1 ms 1620 KB Output is correct
13 Correct 1 ms 1492 KB Output is correct
14 Correct 1 ms 1492 KB Output is correct
15 Correct 1 ms 1492 KB Output is correct
16 Correct 1 ms 1492 KB Output is correct
17 Correct 1 ms 1492 KB Output is correct
18 Correct 1 ms 1492 KB Output is correct
19 Correct 2 ms 1492 KB Output is correct
20 Correct 1 ms 1492 KB Output is correct
21 Correct 1 ms 1492 KB Output is correct
22 Correct 1 ms 1492 KB Output is correct
23 Correct 1 ms 1492 KB Output is correct
24 Correct 1 ms 1492 KB Output is correct
25 Correct 19 ms 1700 KB Output is correct
26 Correct 4 ms 1492 KB Output is correct
27 Correct 1 ms 1492 KB Output is correct
28 Correct 2 ms 1492 KB Output is correct
29 Correct 3 ms 1492 KB Output is correct
30 Correct 2 ms 1620 KB Output is correct
31 Correct 163 ms 2004 KB Output is correct
32 Correct 3 ms 1492 KB Output is correct
33 Correct 2 ms 1492 KB Output is correct
34 Correct 15 ms 1492 KB Output is correct
35 Correct 62 ms 2540 KB Output is correct
36 Correct 44 ms 2516 KB Output is correct
37 Correct 514 ms 2360 KB Output is correct
38 Correct 16 ms 2772 KB Output is correct
39 Correct 289 ms 1788 KB Output is correct
40 Correct 5 ms 1748 KB Output is correct
41 Correct 7 ms 1972 KB Output is correct
42 Correct 8 ms 2048 KB Output is correct
43 Correct 11 ms 2260 KB Output is correct
44 Correct 26 ms 2644 KB Output is correct
45 Correct 13 ms 2400 KB Output is correct
46 Correct 33 ms 3156 KB Output is correct
47 Correct 22 ms 2964 KB Output is correct
48 Correct 21 ms 3004 KB Output is correct
49 Correct 35 ms 3164 KB Output is correct
50 Correct 389 ms 1996 KB Output is correct