Submission #341237

# Submission time Handle Problem Language Result Execution time Memory
341237 2020-12-29T09:27:15 Z juggernaut Marriage questions (IZhO14_marriage) C++14
100 / 100
472 ms 1260 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) continue;
		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 of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 |  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]
   46 | 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]
   47 |  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]
   50 |   scanf("%d %d", &a, &b);
      |   ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 492 KB Output is correct
2 Correct 1 ms 492 KB Output is correct
3 Correct 1 ms 492 KB Output is correct
4 Correct 1 ms 492 KB Output is correct
5 Correct 1 ms 492 KB Output is correct
6 Correct 1 ms 492 KB Output is correct
7 Correct 1 ms 492 KB Output is correct
8 Correct 1 ms 512 KB Output is correct
9 Correct 1 ms 492 KB Output is correct
10 Correct 1 ms 512 KB Output is correct
11 Correct 1 ms 492 KB Output is correct
12 Correct 1 ms 492 KB Output is correct
13 Correct 1 ms 492 KB Output is correct
14 Correct 1 ms 492 KB Output is correct
15 Correct 1 ms 492 KB Output is correct
16 Correct 1 ms 512 KB Output is correct
17 Correct 1 ms 492 KB Output is correct
18 Correct 1 ms 492 KB Output is correct
19 Correct 3 ms 512 KB Output is correct
20 Correct 1 ms 492 KB Output is correct
21 Correct 1 ms 492 KB Output is correct
22 Correct 1 ms 492 KB Output is correct
23 Correct 1 ms 492 KB Output is correct
24 Correct 1 ms 492 KB Output is correct
25 Correct 19 ms 620 KB Output is correct
26 Correct 4 ms 492 KB Output is correct
27 Correct 1 ms 492 KB Output is correct
28 Correct 1 ms 492 KB Output is correct
29 Correct 3 ms 492 KB Output is correct
30 Correct 3 ms 492 KB Output is correct
31 Correct 156 ms 748 KB Output is correct
32 Correct 10 ms 492 KB Output is correct
33 Correct 1 ms 492 KB Output is correct
34 Correct 3 ms 492 KB Output is correct
35 Correct 114 ms 1132 KB Output is correct
36 Correct 82 ms 1004 KB Output is correct
37 Correct 472 ms 876 KB Output is correct
38 Correct 24 ms 1004 KB Output is correct
39 Correct 75 ms 748 KB Output is correct
40 Correct 4 ms 620 KB Output is correct
41 Correct 9 ms 620 KB Output is correct
42 Correct 8 ms 620 KB Output is correct
43 Correct 12 ms 748 KB Output is correct
44 Correct 26 ms 1004 KB Output is correct
45 Correct 13 ms 748 KB Output is correct
46 Correct 228 ms 1132 KB Output is correct
47 Correct 23 ms 1004 KB Output is correct
48 Correct 22 ms 1004 KB Output is correct
49 Correct 347 ms 1260 KB Output is correct
50 Correct 187 ms 620 KB Output is correct