답안 #91481

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
91481 2018-12-27T16:55:48 Z Just_Solve_The_Problem 결혼 문제 (IZhO14_marriage) C++11
100 / 100
382 ms 6024 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];
int used[N], mt[(int)3e4 + 7], prr[N], cnt = 1;
queue < int > q;
int l, r;

bool kuhn(int v) {
	if (used[v] == cnt) return false;
	used[v] = cnt;
	for (int i = 0; i < gr[v].size(); i++) {
		int to = gr[v][i];
		if (to < l) continue;
		if (to > r) break;
		if (mt[to] == -1 || kuhn(mt[to])) {
			mt[to] = v;
			return true;
		}
	}
	return false;
}

bool check() {
	int v;
	while (!q.empty()) {
		v = q.front();
		q.pop();
		cnt++;
		if (!kuhn(v)) {
			q.push(v);
			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());
	}
	memset(mt, -1, sizeof mt);
	for (int i = 1; i <= m; i++) {
		q.push(i);
	}
	r = 1;
	for (int i = 1; i <= n; i++) {
		l = i;
		while (r <= n && !check()) {
			r++;
		}
		ans += (n - r + 1);
		if (mt[i] != -1) {
			q.push(mt[i]);
			mt[i] = -1;
		}
	}
	cout << ans << endl;
}

Compilation message

marriage.cpp: In function 'bool kuhn(int)':
marriage.cpp:20:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < gr[v].size(); i++) {
                  ~~^~~~~~~~~~~~~~
marriage.cpp: At global scope:
marriage.cpp:46:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main() {
      ^
marriage.cpp: In function 'int main()':
marriage.cpp:47: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:50: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 632 KB Output is correct
6 Correct 2 ms 816 KB Output is correct
7 Correct 2 ms 816 KB Output is correct
8 Correct 2 ms 816 KB Output is correct
9 Correct 2 ms 816 KB Output is correct
10 Correct 2 ms 816 KB Output is correct
11 Correct 2 ms 816 KB Output is correct
12 Correct 2 ms 816 KB Output is correct
13 Correct 2 ms 816 KB Output is correct
14 Correct 2 ms 816 KB Output is correct
15 Correct 2 ms 816 KB Output is correct
16 Correct 2 ms 816 KB Output is correct
17 Correct 2 ms 816 KB Output is correct
18 Correct 2 ms 816 KB Output is correct
19 Correct 3 ms 816 KB Output is correct
20 Correct 2 ms 824 KB Output is correct
21 Correct 2 ms 828 KB Output is correct
22 Correct 2 ms 832 KB Output is correct
23 Correct 2 ms 836 KB Output is correct
24 Correct 2 ms 836 KB Output is correct
25 Correct 15 ms 1016 KB Output is correct
26 Correct 4 ms 1060 KB Output is correct
27 Correct 2 ms 1060 KB Output is correct
28 Correct 2 ms 1060 KB Output is correct
29 Correct 4 ms 1232 KB Output is correct
30 Correct 3 ms 1232 KB Output is correct
31 Correct 125 ms 1736 KB Output is correct
32 Correct 8 ms 1736 KB Output is correct
33 Correct 2 ms 1736 KB Output is correct
34 Correct 4 ms 1736 KB Output is correct
35 Correct 105 ms 2812 KB Output is correct
36 Correct 76 ms 3412 KB Output is correct
37 Correct 382 ms 3656 KB Output is correct
38 Correct 20 ms 4484 KB Output is correct
39 Correct 79 ms 4484 KB Output is correct
40 Correct 5 ms 4484 KB Output is correct
41 Correct 10 ms 4548 KB Output is correct
42 Correct 8 ms 4828 KB Output is correct
43 Correct 11 ms 5252 KB Output is correct
44 Correct 19 ms 5840 KB Output is correct
45 Correct 11 ms 5840 KB Output is correct
46 Correct 178 ms 5896 KB Output is correct
47 Correct 20 ms 5896 KB Output is correct
48 Correct 19 ms 5896 KB Output is correct
49 Correct 260 ms 6024 KB Output is correct
50 Correct 221 ms 6024 KB Output is correct