Submission #345347

# Submission time Handle Problem Language Result Execution time Memory
345347 2021-01-07T07:51:14 Z maskoff Marriage questions (IZhO14_marriage) C++14
72 / 100
1500 ms 25412 KB
#include <bits/stdc++.h>

#define file ""

#define all(x) x.begin(), x.end()

#define sc second
#define fr first

#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

const ll inf = 1e18 + 5;
const ll mod = 1e9 + 7;

const int N = 5e5 + 5;

int dx[] = {+1, 0, -1, 0};
int dy[] = {0, +1, 0, -1};


int n, m, k, timer;
int used[N], mt[N], pr[N], s[N];
vector<int> g[N], all[N];

bool go(int v) {
	if (used[v] == timer) return false;
	used[v] = timer;

 	for (int i = s[v]; i < g[v].size(); i++) {
  	int to = g[v][i];
 		if (mt[to] == false) {
 		 	mt[to] = v;
 		 	pr[v] = to;
 		 	return true;
 		}
 	}
 	  
 	for (int i = s[v]; i < g[v].size(); i++) {
 		int to = g[v][i];
		if (go(mt[to])) {
		 	mt[to] = v;
		 	pr[v] = to;
		 	return true;
		}
	}

	return false;
}

bool check(int y, int p) {
	fill (pr + 1, pr + 1 + m, false);

	for (int j : all[p])
		g[j].pb(p);
	
	for (int i = 1; i <= m; i++) {
		timer++;
		go(i);
	}
	
	int ok = true;
	for (int i = 1; i <= m; i++) 
		if (pr[i] == 0) ok = false;
		else mt[pr[i]] = false;
	
	return ok;
}

void clear(int x) {
 	for (int j : all[x]) {
 		s[j]++;
 		assert(s[j] <= g[j].size());
 	}
}                   

int main() { 
	ios_base :: sync_with_stdio(false);               
	cin.tie(nullptr);
	srand(time(nullptr));
	cin >> n >> m >> k;
	for (int x, y, i = 1; i <= k; i++) {
		cin >> x >> y;
	 	all[x].pb(y);
	}
	ll res = 0;
	int j = 1;
	for (int i = 1; i <= n; i++) {
		int p = check(i, j);
		while (j <= n && !p) {
			j++;
			p = check(i, j);
	 	}
	 	if (p) res += n - j + 1;
	 	else break;
	 	clear(i);
	}
	cout << res;
  return 0;              
}

Compilation message

marriage.cpp: In function 'bool go(int)':
marriage.cpp:35:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   35 |   for (int i = s[v]; i < g[v].size(); i++) {
      |                      ~~^~~~~~~~~~~~~
marriage.cpp:44:24: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   44 |   for (int i = s[v]; i < g[v].size(); i++) {
      |                      ~~^~~~~~~~~~~~~
In file included from /usr/include/c++/9/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:33,
                 from marriage.cpp:1:
marriage.cpp: In function 'void clear(int)':
marriage.cpp:78:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   78 |    assert(s[j] <= g[j].size());
      |           ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 21 ms 23788 KB Output isn't correct
2 Correct 19 ms 23788 KB Output is correct
3 Correct 18 ms 23788 KB Output is correct
4 Correct 17 ms 23788 KB Output is correct
5 Correct 19 ms 23800 KB Output is correct
6 Correct 19 ms 23788 KB Output is correct
7 Incorrect 19 ms 23788 KB Output isn't correct
8 Correct 15 ms 23788 KB Output is correct
9 Correct 15 ms 23788 KB Output is correct
10 Correct 16 ms 23788 KB Output is correct
11 Correct 17 ms 23788 KB Output is correct
12 Correct 28 ms 23788 KB Output is correct
13 Incorrect 18 ms 23788 KB Output isn't correct
14 Correct 16 ms 23796 KB Output is correct
15 Incorrect 16 ms 23872 KB Output isn't correct
16 Correct 15 ms 23788 KB Output is correct
17 Correct 14 ms 23788 KB Output is correct
18 Correct 17 ms 23788 KB Output is correct
19 Incorrect 20 ms 23916 KB Output isn't correct
20 Incorrect 18 ms 23916 KB Output isn't correct
21 Correct 15 ms 23788 KB Output is correct
22 Correct 15 ms 23788 KB Output is correct
23 Correct 16 ms 23916 KB Output is correct
24 Correct 16 ms 23916 KB Output is correct
25 Incorrect 52 ms 24044 KB Output isn't correct
26 Incorrect 25 ms 23916 KB Output isn't correct
27 Correct 17 ms 23924 KB Output is correct
28 Correct 17 ms 23916 KB Output is correct
29 Correct 18 ms 23916 KB Output is correct
30 Correct 17 ms 23916 KB Output is correct
31 Execution timed out 1580 ms 24288 KB Time limit exceeded
32 Correct 404 ms 23972 KB Output is correct
33 Correct 23 ms 23916 KB Output is correct
34 Correct 37 ms 23916 KB Output is correct
35 Correct 264 ms 25160 KB Output is correct
36 Correct 175 ms 24968 KB Output is correct
37 Execution timed out 1589 ms 24316 KB Time limit exceeded
38 Correct 73 ms 25236 KB Output is correct
39 Correct 626 ms 24224 KB Output is correct
40 Correct 472 ms 24300 KB Output is correct
41 Correct 724 ms 24416 KB Output is correct
42 Correct 175 ms 24428 KB Output is correct
43 Correct 186 ms 24684 KB Output is correct
44 Correct 359 ms 25068 KB Output is correct
45 Execution timed out 1570 ms 25068 KB Time limit exceeded
46 Execution timed out 1580 ms 25224 KB Time limit exceeded
47 Correct 654 ms 25412 KB Output is correct
48 Correct 668 ms 25324 KB Output is correct
49 Execution timed out 1585 ms 25068 KB Time limit exceeded
50 Execution timed out 1594 ms 24364 KB Time limit exceeded