#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 p) {
for (int j : all[p])
g[j].pb(p);
for (int i = 1; i <= m; i++) {
timer++;
go(i);
}
bool ok = true;
for (int i = 1; i <= m; i++) {
if (pr[i] == 0) ok = false;
else mt[pr[i]] = 0;
pr[i] = false;
}
return ok;
}
bool clear(int x) {
for (int j : all[x])
s[j]++;
for (int i = 1; i <= m; i++) {
timer++;
go(i);
}
bool ok = true;
for (int i = 1; i <= m; i++) {
if (pr[i] == 0) ok = false;
else mt[pr[i]] = 0;
pr[i] = false;
}
return ok;
}
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;
bool p = check(1);
for (int i = 1; i <= n; i++) {
while (j <= n && !p) {
j++;
if (j == n + 1) break;
p = check(j);
}
if (p) res += n - j + 1;
else break;
p = 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++) {
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
23788 KB |
Output is correct |
2 |
Correct |
17 ms |
23788 KB |
Output is correct |
3 |
Correct |
17 ms |
23788 KB |
Output is correct |
4 |
Correct |
18 ms |
23788 KB |
Output is correct |
5 |
Correct |
16 ms |
23788 KB |
Output is correct |
6 |
Correct |
18 ms |
23788 KB |
Output is correct |
7 |
Correct |
17 ms |
23908 KB |
Output is correct |
8 |
Correct |
16 ms |
23800 KB |
Output is correct |
9 |
Correct |
18 ms |
23788 KB |
Output is correct |
10 |
Correct |
16 ms |
23788 KB |
Output is correct |
11 |
Correct |
16 ms |
23824 KB |
Output is correct |
12 |
Correct |
16 ms |
23788 KB |
Output is correct |
13 |
Correct |
24 ms |
24044 KB |
Output is correct |
14 |
Correct |
18 ms |
23716 KB |
Output is correct |
15 |
Correct |
21 ms |
23792 KB |
Output is correct |
16 |
Correct |
133 ms |
23788 KB |
Output is correct |
17 |
Correct |
136 ms |
23820 KB |
Output is correct |
18 |
Correct |
16 ms |
23916 KB |
Output is correct |
19 |
Correct |
20 ms |
23948 KB |
Output is correct |
20 |
Correct |
20 ms |
23920 KB |
Output is correct |
21 |
Correct |
17 ms |
23916 KB |
Output is correct |
22 |
Correct |
16 ms |
23788 KB |
Output is correct |
23 |
Correct |
17 ms |
23916 KB |
Output is correct |
24 |
Correct |
127 ms |
23916 KB |
Output is correct |
25 |
Correct |
297 ms |
24052 KB |
Output is correct |
26 |
Correct |
25 ms |
24044 KB |
Output is correct |
27 |
Correct |
18 ms |
23916 KB |
Output is correct |
28 |
Correct |
21 ms |
23916 KB |
Output is correct |
29 |
Correct |
21 ms |
23928 KB |
Output is correct |
30 |
Correct |
19 ms |
23916 KB |
Output is correct |
31 |
Execution timed out |
1586 ms |
24348 KB |
Time limit exceeded |
32 |
Correct |
412 ms |
23984 KB |
Output is correct |
33 |
Correct |
22 ms |
23916 KB |
Output is correct |
34 |
Correct |
36 ms |
24044 KB |
Output is correct |
35 |
Correct |
195 ms |
25012 KB |
Output is correct |
36 |
Correct |
250 ms |
25088 KB |
Output is correct |
37 |
Execution timed out |
1595 ms |
24304 KB |
Time limit exceeded |
38 |
Correct |
75 ms |
25196 KB |
Output is correct |
39 |
Correct |
562 ms |
24208 KB |
Output is correct |
40 |
Correct |
444 ms |
24308 KB |
Output is correct |
41 |
Correct |
721 ms |
24428 KB |
Output is correct |
42 |
Correct |
180 ms |
24472 KB |
Output is correct |
43 |
Correct |
187 ms |
24556 KB |
Output is correct |
44 |
Correct |
346 ms |
25068 KB |
Output is correct |
45 |
Execution timed out |
1593 ms |
24812 KB |
Time limit exceeded |
46 |
Execution timed out |
1583 ms |
25612 KB |
Time limit exceeded |
47 |
Correct |
641 ms |
25904 KB |
Output is correct |
48 |
Correct |
644 ms |
25868 KB |
Output is correct |
49 |
Execution timed out |
1554 ms |
25588 KB |
Time limit exceeded |
50 |
Execution timed out |
1591 ms |
24172 KB |
Time limit exceeded |