답안 #91352

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
91352 2018-12-27T08:34:40 Z Just_Solve_The_Problem 결혼 문제 (IZhO14_marriage) C++11
86 / 100
1500 ms 1464 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;
	for (int i = 1; i <= m; i++) {
		sort(gr[i].begin(), gr[i].end());
		lr[i].first = 0;
	}
	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);
		}
	} else {
		for (int i = 1; i <= n - m + 1; i++) {
			int res = check(i);
			//cout << i << ' ' << res << endl;
			ans += (n - res + 1);
		}
	}
	cout << ans << 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 512 KB Output is correct
3 Correct 2 ms 680 KB Output is correct
4 Correct 2 ms 680 KB Output is correct
5 Correct 2 ms 680 KB Output is correct
6 Correct 2 ms 680 KB Output is correct
7 Correct 2 ms 680 KB Output is correct
8 Incorrect 5 ms 680 KB Output isn't correct
9 Correct 2 ms 748 KB Output is correct
10 Correct 2 ms 748 KB Output is correct
11 Correct 2 ms 748 KB Output is correct
12 Correct 2 ms 764 KB Output is correct
13 Incorrect 2 ms 764 KB Output isn't correct
14 Correct 2 ms 764 KB Output is correct
15 Correct 2 ms 764 KB Output is correct
16 Correct 2 ms 764 KB Output is correct
17 Correct 2 ms 764 KB Output is correct
18 Correct 2 ms 764 KB Output is correct
19 Correct 7 ms 764 KB Output is correct
20 Correct 6 ms 764 KB Output is correct
21 Correct 4 ms 764 KB Output is correct
22 Correct 4 ms 764 KB Output is correct
23 Correct 5 ms 764 KB Output is correct
24 Correct 5 ms 764 KB Output is correct
25 Correct 82 ms 788 KB Output is correct
26 Correct 52 ms 788 KB Output is correct
27 Correct 28 ms 788 KB Output is correct
28 Correct 28 ms 844 KB Output is correct
29 Correct 34 ms 844 KB Output is correct
30 Correct 32 ms 844 KB Output is correct
31 Correct 1044 ms 1132 KB Output is correct
32 Correct 237 ms 1132 KB Output is correct
33 Correct 97 ms 1132 KB Output is correct
34 Correct 88 ms 1132 KB Output is correct
35 Correct 1344 ms 1348 KB Output is correct
36 Correct 1101 ms 1396 KB Output is correct
37 Incorrect 129 ms 1396 KB Output isn't correct
38 Correct 78 ms 1396 KB Output is correct
39 Correct 355 ms 1396 KB Output is correct
40 Correct 395 ms 1396 KB Output is correct
41 Incorrect 481 ms 1396 KB Output isn't correct
42 Correct 297 ms 1396 KB Output is correct
43 Correct 227 ms 1396 KB Output is correct
44 Correct 390 ms 1464 KB Output is correct
45 Incorrect 1205 ms 1464 KB Output isn't correct
46 Execution timed out 1563 ms 1464 KB Time limit exceeded
47 Correct 840 ms 1464 KB Output is correct
48 Correct 808 ms 1464 KB Output is correct
49 Execution timed out 1563 ms 1464 KB Time limit exceeded
50 Correct 1148 ms 1464 KB Output is correct