답안 #91354

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
91354 2018-12-27T08:37:03 Z Just_Solve_The_Problem 결혼 문제 (IZhO14_marriage) C++11
90 / 100
1500 ms 1404 KB
#include <bits/stdc++.h>
 
using namespace std;
 
#define ok puts("ok");
#define ll long long
 
const int N = (int)2e3 + 7;
 
int n, m, k;
 
vector < int > gr[N];
pair < int, int > lr[N];
int used[N], mt[(int)3e4 + 7], prr[N], cnt = 1;
 
bool kuhn(int v) {
	if (used[v] == cnt) return false;
	used[v] = cnt;
	for (int i = lr[v].first; i < gr[v].size(); i++) {
		int to = gr[v][i];
		if (mt[to] == -1 || kuhn(mt[to])) {
			mt[to] = v;
			prr[v] = to;
			return true;
		}
	}
	return false;
}
 
int check(int l) {
	for (int i = 1; i <= m; i++) {
		while (lr[i].first < (int)gr[i].size() && gr[i][lr[i].first] < l) lr[i].first++;
	}
	memset(mt, -1, sizeof mt);
	memset(prr, -1, sizeof prr);
	int run = 1;
	while (run) {
		run = 0;
		for (int i = 1; i <= m; i++) {
			if (prr[i] == -1 && kuhn(i)) {
				run = 1;
			}
		}
		cnt++;
	}
	int mx = 0;
	for (int i = 1; i <= m; i++) {
		if (prr[i] == -1) {
			return n + 1;
		}
		mx = max(mx, prr[i]);
	}
	return mx;
}

bool kuhn1(int v) {
	if (used[v] == cnt) return false;
	used[v] = cnt;
	for (int i = lr[v].first; i <= lr[v].second; i++) {
		int to = gr[v][i];
		if (mt[to] == -1 || kuhn1(mt[to])) {
			mt[to] = v;
			prr[v] = to;
			return true;
		}
	}
	return false;
}

bool check1(int l, int r) {
	for (int i = 1; i <= m; i++) {
		while (lr[i].first < (int)gr[i].size() && gr[i][lr[i].first] < l) lr[i].first++;
		while (lr[i].first > 0 && gr[i][lr[i].first - 1] >= l) lr[i].first--;
		while (lr[i].second + 1 < (int)gr[i].size() && gr[i][lr[i].second + 1] <= r) lr[i].second++;
		while (lr[i].second >= 0 && gr[i][lr[i].second] > r) lr[i].second--;
	}
	memset(mt, -1, sizeof mt);
	memset(prr, -1, sizeof prr);
	int run = 1;
	while (run) {
		run = 0;
		for (int i = 1; i <= m; i++) {
			if (prr[i] == -1 && kuhn1(i)) {
				run = 1;
			}
		}
		cnt++;
	}
	for (int i = 1; i <= m; i++) {
		if (prr[i] == -1) {
			return 0;
		}
	}
	return 1;
}
 
main() {
	scanf("%d %d %d", &n, &m, &k);
	for (int i = 1; i <= k; i++) {
		int a, b;
		scanf("%d %d", &a, &b);
		gr[b].push_back(a);
	}
	ll ans = 0, ans1 = 0;
	for (int i = 1; i <= m; i++) {
		sort(gr[i].begin(), gr[i].end());
	}
	if (n <= 1000 && m <= 500) {
		for (int i = 1; i <= m; i++) {
			lr[i].second = -1;
			lr[i].first = gr[i].size();
		}
		for (int i = 1; i <= n - m + 1; i++) {
			int lo = i;
			int hi = n + 1;
			while (hi - lo > 1) {
				int mid = (lo + hi) >> 1;
				if (check1(i, mid)) {
					hi = mid;
				} else {
					lo = mid;
				}
			}
			ans += (n - hi + 1);
		}
	}
	for (int i = 1; i <= m; i++) {
		lr[i].first = 0;
	}
	for (int i = 1; i <= n - m + 1; i++) {
		int res = check(i);
		//cout << i << ' ' << res << endl;
		ans1 += (n - res + 1);
	}
	cout << max(ans, ans1) << endl;
}

Compilation message

marriage.cpp: In function 'bool kuhn(int)':
marriage.cpp:19:30: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = lr[v].first; i < gr[v].size(); i++) {
                            ~~^~~~~~~~~~~~~~
marriage.cpp: At global scope:
marriage.cpp:97:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main() {
      ^
marriage.cpp: In function 'int main()':
marriage.cpp:98:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d %d", &n, &m, &k);
  ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
marriage.cpp:101:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d %d", &a, &b);
   ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 504 KB Output is correct
2 Correct 2 ms 632 KB Output is correct
3 Correct 2 ms 632 KB Output is correct
4 Correct 2 ms 632 KB Output is correct
5 Correct 2 ms 784 KB Output is correct
6 Correct 2 ms 784 KB Output is correct
7 Correct 2 ms 784 KB Output is correct
8 Correct 2 ms 784 KB Output is correct
9 Correct 2 ms 784 KB Output is correct
10 Correct 2 ms 784 KB Output is correct
11 Correct 2 ms 784 KB Output is correct
12 Correct 2 ms 784 KB Output is correct
13 Correct 2 ms 784 KB Output is correct
14 Correct 2 ms 784 KB Output is correct
15 Correct 2 ms 784 KB Output is correct
16 Correct 2 ms 784 KB Output is correct
17 Correct 2 ms 784 KB Output is correct
18 Correct 2 ms 784 KB Output is correct
19 Correct 7 ms 784 KB Output is correct
20 Correct 6 ms 784 KB Output is correct
21 Correct 4 ms 784 KB Output is correct
22 Correct 4 ms 784 KB Output is correct
23 Correct 5 ms 784 KB Output is correct
24 Correct 5 ms 864 KB Output is correct
25 Correct 88 ms 868 KB Output is correct
26 Correct 57 ms 868 KB Output is correct
27 Correct 33 ms 868 KB Output is correct
28 Correct 29 ms 868 KB Output is correct
29 Correct 37 ms 868 KB Output is correct
30 Correct 36 ms 868 KB Output is correct
31 Correct 1109 ms 1020 KB Output is correct
32 Correct 249 ms 1020 KB Output is correct
33 Correct 101 ms 1020 KB Output is correct
34 Correct 99 ms 1020 KB Output is correct
35 Correct 1449 ms 1404 KB Output is correct
36 Correct 1159 ms 1404 KB Output is correct
37 Incorrect 133 ms 1404 KB Output isn't correct
38 Correct 78 ms 1404 KB Output is correct
39 Correct 365 ms 1404 KB Output is correct
40 Correct 365 ms 1404 KB Output is correct
41 Incorrect 495 ms 1404 KB Output isn't correct
42 Correct 224 ms 1404 KB Output is correct
43 Correct 230 ms 1404 KB Output is correct
44 Correct 430 ms 1404 KB Output is correct
45 Incorrect 1228 ms 1404 KB Output isn't correct
46 Execution timed out 1549 ms 1404 KB Time limit exceeded
47 Correct 1047 ms 1404 KB Output is correct
48 Correct 824 ms 1404 KB Output is correct
49 Execution timed out 1569 ms 1404 KB Time limit exceeded
50 Correct 1074 ms 1404 KB Output is correct