Submission #345167

# Submission time Handle Problem Language Result Execution time Memory
345167 2021-01-07T05:42:14 Z maskoff Marriage questions (IZhO14_marriage) C++14
72 / 100
1500 ms 26476 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;
int used[N], mt[N], pr[N];
vector<int> g[N], all[N];

bool go(int v) {
 	if (used[v]) return false;
 	used[v] = true;
 	for (int to : g[v]) 
 		if (mt[to] == false) {
 		 	mt[to] = v;
 		 	pr[v] = to;
 		 	return true;
 		}    
	for (int to : g[v])
		if (go(mt[to])) {
		 	mt[to] = v;
		 	pr[v] = to;
		 	return true;
		}
	return false;
}

bool check(int y, int p) {
	fill (mt + 1, mt + 1 + n, false);
	fill (pr + 1, pr + 1 + m, false);
	for (int i = 1; i <= m; i++)
		g[i].clear();
	for (int i = y; i <= p; i++) {
	 	for (int j : all[i])
	 		g[j].pb(i);
	}
	for (int i = 1; i <= m; i++) {
		fill(used + 1, used + 1 + m, false);
		go(i);
	}
	for (int i = 1; i <= m; i++)
		if (pr[i] == 0)
			return false;
	return true;
}

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);
	}
	int res = 0;
	for (int i = 1; i <= n; i++) {
		int l = i;
		int r = n;
		int ans = -1;
		while (l <= r) {
		 	int mid = l + r >> 1;
		 	if (check(i, mid)) ans = mid, r = mid - 1;
		 	else l = mid + 1;
		}
		if (ans == -1) break;
		else res += (n - ans + 1);
	}
	cout << res;
  return 0;              
}

Compilation message

marriage.cpp: In function 'int main()':
marriage.cpp:82:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   82 |     int mid = l + r >> 1;
      |               ~~^~~
# Verdict Execution time Memory Grader output
1 Correct 20 ms 23788 KB Output is correct
2 Correct 18 ms 23788 KB Output is correct
3 Correct 17 ms 23788 KB Output is correct
4 Correct 19 ms 23788 KB Output is correct
5 Correct 17 ms 23936 KB Output is correct
6 Correct 21 ms 23788 KB Output is correct
7 Correct 18 ms 23788 KB Output is correct
8 Correct 17 ms 23788 KB Output is correct
9 Correct 17 ms 23788 KB Output is correct
10 Correct 25 ms 23848 KB Output is correct
11 Correct 20 ms 23788 KB Output is correct
12 Correct 19 ms 23788 KB Output is correct
13 Correct 18 ms 23788 KB Output is correct
14 Correct 20 ms 23976 KB Output is correct
15 Correct 21 ms 23788 KB Output is correct
16 Correct 20 ms 23788 KB Output is correct
17 Correct 21 ms 23788 KB Output is correct
18 Correct 24 ms 23788 KB Output is correct
19 Correct 28 ms 23916 KB Output is correct
20 Correct 22 ms 23936 KB Output is correct
21 Correct 21 ms 23828 KB Output is correct
22 Correct 24 ms 23788 KB Output is correct
23 Correct 21 ms 23892 KB Output is correct
24 Correct 19 ms 23916 KB Output is correct
25 Correct 251 ms 24128 KB Output is correct
26 Correct 89 ms 23964 KB Output is correct
27 Correct 17 ms 23916 KB Output is correct
28 Correct 38 ms 23916 KB Output is correct
29 Correct 48 ms 24044 KB Output is correct
30 Correct 64 ms 24044 KB Output is correct
31 Execution timed out 1577 ms 24812 KB Time limit exceeded
32 Correct 464 ms 24056 KB Output is correct
33 Correct 18 ms 23916 KB Output is correct
34 Correct 428 ms 23984 KB Output is correct
35 Execution timed out 1543 ms 25684 KB Time limit exceeded
36 Execution timed out 1598 ms 25608 KB Time limit exceeded
37 Execution timed out 1565 ms 24984 KB Time limit exceeded
38 Execution timed out 1573 ms 25932 KB Time limit exceeded
39 Execution timed out 1548 ms 24172 KB Time limit exceeded
40 Correct 88 ms 24300 KB Output is correct
41 Execution timed out 1562 ms 24556 KB Time limit exceeded
42 Correct 508 ms 24812 KB Output is correct
43 Correct 893 ms 25136 KB Output is correct
44 Execution timed out 1588 ms 25964 KB Time limit exceeded
45 Execution timed out 1585 ms 25068 KB Time limit exceeded
46 Execution timed out 1554 ms 26328 KB Time limit exceeded
47 Execution timed out 1542 ms 26408 KB Time limit exceeded
48 Execution timed out 1573 ms 26220 KB Time limit exceeded
49 Execution timed out 1577 ms 26476 KB Time limit exceeded
50 Execution timed out 1564 ms 24300 KB Time limit exceeded