#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;
visited[v] = 1;
for (o = eo[v]; o--; ) {
int u = eu[v][o], w = vv[u];
if (u <= l)
continue;
if (w == -1 || !visited[w]) {
pp[u] = v;
if (w == -1)
u_ = min(u_, u);
else
dfs(w);
}
}
}
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:49:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | scanf("%d%d%d", &nu, &nv, &m);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
marriage.c:53:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
53 | scanf("%d%d", &u, &v), u--, v--;
| ^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 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 |
0 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 |
1582 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 |
0 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 |
2 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Execution timed out |
1586 ms |
212 KB |
Time limit exceeded |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
30 ms |
416 KB |
Output is correct |
26 |
Correct |
5 ms |
212 KB |
Output is correct |
27 |
Execution timed out |
1591 ms |
212 KB |
Time limit exceeded |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
4 ms |
340 KB |
Output is correct |
30 |
Correct |
3 ms |
340 KB |
Output is correct |
31 |
Correct |
230 ms |
632 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 |
41 ms |
844 KB |
Output is correct |
36 |
Correct |
29 ms |
852 KB |
Output is correct |
37 |
Correct |
678 ms |
708 KB |
Output is correct |
38 |
Correct |
26 ms |
860 KB |
Output is correct |
39 |
Correct |
28 ms |
512 KB |
Output is correct |
40 |
Execution timed out |
1593 ms |
424 KB |
Time limit exceeded |
41 |
Correct |
13 ms |
596 KB |
Output is correct |
42 |
Correct |
7 ms |
512 KB |
Output is correct |
43 |
Correct |
14 ms |
724 KB |
Output is correct |
44 |
Correct |
22 ms |
980 KB |
Output is correct |
45 |
Correct |
9 ms |
724 KB |
Output is correct |
46 |
Execution timed out |
1582 ms |
1172 KB |
Time limit exceeded |
47 |
Correct |
27 ms |
1108 KB |
Output is correct |
48 |
Correct |
23 ms |
1124 KB |
Output is correct |
49 |
Execution timed out |
1570 ms |
1192 KB |
Time limit exceeded |
50 |
Correct |
28 ms |
596 KB |
Output is correct |