#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NU 30000
#define NV 2000
int min(int a, int b) { return a < b ? a : b; }
int max(int a, int b) { return a > b ? a : b; }
int nu, nv;
int *eu[NV], eo[NV];
void append(int v, int u) {
int o = eo[v]++;
if (o >= 2 && (o & o - 1) == 0)
eu[v] = (int *) realloc(eu[v], o * 2 * sizeof *eu[v]);
eu[v][o] = u;
}
int l, u_;
int uu[NV], vv[NU], pp[NU]; char visited[NV];
void dfs(int v) {
int o, o_;
visited[v] = 1;
for (o = 0, o_ = 0; o < eo[v]; o++) {
int u = eu[v][o], w = vv[u];
if (u <= l)
continue;
eu[v][o_++] = u;
if (w == -1 || !visited[w]) {
pp[u] = v;
if (w == -1)
u_ = min(u_, u);
else
dfs(w);
}
}
eo[v] = o_;
}
int main() {
int m, u, v, v_, r, ans;
scanf("%d%d%d", &nu, &nv, &m);
for (v = 0; v < nv; v++)
eu[v] = (int *) malloc(2 * sizeof *eu[v]);
while (m--) {
scanf("%d%d", &u, &v), u--, v--;
append(v, u);
}
memset(uu, -1, nv * sizeof *uu), memset(vv, -1, nu * sizeof *vv);
for (v = 0; v < nv; v++) {
memset(visited, 0, nv * sizeof *visited);
l = -1, u_ = nu, dfs(v);
u = u_;
while (1) {
v_ = pp[u], u_ = uu[v_];
uu[v_] = u, vv[u] = v_;
u = u_;
if (v_ == v)
break;
}
}
r = -1;
for (v = 0; v < nv; v++)
r = max(r, uu[v]);
ans = 0;
for (u = 0; u < nu; u++) {
ans += nu - r;
if ((v = vv[u]) != -1) {
memset(visited, 0, nv * sizeof *visited);
l = u, u_ = nu, dfs(v);
if (u_ == nu)
break;
r = max(r, u_);
u = u_;
while (1) {
v_ = pp[u], u_ = uu[v_];
uu[v_] = u, vv[u] = v_;
u = u_;
if (v_ == v)
break;
}
vv[u] = -1;
}
}
printf("%d\n", ans);
return 0;
}
Compilation message
marriage.c: In function 'append':
marriage.c:18:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
18 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
marriage.c: In function 'main':
marriage.c:51:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
51 | scanf("%d%d%d", &nu, &nv, &m);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
marriage.c:55:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
55 | scanf("%d%d", &u, &v), u--, v--;
| ^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Execution timed out |
1567 ms |
212 KB |
Time limit exceeded |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Execution timed out |
1568 ms |
212 KB |
Time limit exceeded |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
17 ms |
340 KB |
Output is correct |
26 |
Correct |
4 ms |
212 KB |
Output is correct |
27 |
Execution timed out |
1572 ms |
212 KB |
Time limit exceeded |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
4 ms |
364 KB |
Output is correct |
30 |
Correct |
3 ms |
340 KB |
Output is correct |
31 |
Correct |
175 ms |
624 KB |
Output is correct |
32 |
Correct |
13 ms |
380 KB |
Output is correct |
33 |
Execution timed out |
1576 ms |
212 KB |
Time limit exceeded |
34 |
Correct |
2 ms |
340 KB |
Output is correct |
35 |
Correct |
46 ms |
776 KB |
Output is correct |
36 |
Correct |
29 ms |
852 KB |
Output is correct |
37 |
Correct |
476 ms |
680 KB |
Output is correct |
38 |
Correct |
28 ms |
900 KB |
Output is correct |
39 |
Correct |
28 ms |
468 KB |
Output is correct |
40 |
Execution timed out |
1592 ms |
468 KB |
Time limit exceeded |
41 |
Correct |
12 ms |
592 KB |
Output is correct |
42 |
Correct |
7 ms |
596 KB |
Output is correct |
43 |
Correct |
13 ms |
756 KB |
Output is correct |
44 |
Correct |
22 ms |
996 KB |
Output is correct |
45 |
Correct |
7 ms |
688 KB |
Output is correct |
46 |
Execution timed out |
1591 ms |
1172 KB |
Time limit exceeded |
47 |
Correct |
30 ms |
1236 KB |
Output is correct |
48 |
Correct |
23 ms |
1212 KB |
Output is correct |
49 |
Execution timed out |
1578 ms |
1324 KB |
Time limit exceeded |
50 |
Correct |
27 ms |
596 KB |
Output is correct |