/* https://oj.uz/submission/100855 (Benq) */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NU 30000
#define NV 2000
unsigned int X = 12345;
int rand_() {
return (X *= 3) >> 1;
}
int nu, nv;
int *eu[NV], eo[NV], el[NV], er[NV];
int uu[NV], vv[NU]; char visited[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;
}
void sort(int *ii, int l, int r) {
while (l < r) {
int i = l, j = l, k = r, i_ = ii[l + rand_() % (r - l)], tmp;
while (j < k)
if (ii[j] == i_)
j++;
else if (ii[j] < i_) {
tmp = ii[i], ii[i] = ii[j], ii[j] = tmp;
i++, j++;
} else {
k--;
tmp = ii[j], ii[j] = ii[k], ii[k] = tmp;
}
sort(ii, l, i);
l = k;
}
}
int dfs(int v) {
int o;
if (visited[v])
return 0;
visited[v] = 1;
for (o = el[v]; o < er[v]; o++) {
int u = eu[v][o], w = vv[u];
if (w == -1 || dfs(w)) {
uu[v] = u, vv[u] = v;
return 1;
}
}
return 0;
}
int check() {
int v;
for (v = 0; v < nv; v++)
if (uu[v] == -1) {
memset(visited, 0, nv * sizeof *visited);
if (!dfs(v))
return 0;
}
return 1;
}
int main() {
int m, h, l, r, u, v, ans;
scanf("%d%d%d", &nu, &nv, &m);
for (v = 0; v < nv; v++)
eu[v] = (int *) malloc(2 * sizeof *eu[v]);
for (h = 0; h < m; h++) {
scanf("%d%d", &u, &v), u--, v--;
append(v, u);
}
for (v = 0; v < nv; v++)
sort(eu[v], 0, eo[v]);
memset(uu, -1, nv * sizeof *uu), memset(vv, -1, nu * sizeof *vv);
ans = 0;
for (l = 0, r = -1; l < nu; l++) {
while (r < nu && !check()) {
r++;
for (v = 0; v < nv; v++)
if (er[v] < eo[v] && eu[v][er[v]] == r)
er[v]++;
}
ans += nu - r;
for (v = 0; v < nv; v++) {
if (el[v] < eo[v] && eu[v][el[v]] == l)
el[v]++;
if (uu[v] == l)
uu[v] = -1;
}
}
printf("%d\n", ans);
return 0;
}
Compilation message
marriage.c: In function 'append':
marriage.c:22:23: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
22 | if (o >= 2 && (o & o - 1) == 0)
| ~~^~~
marriage.c: In function 'main':
marriage.c:78:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
78 | scanf("%d%d%d", &nu, &nv, &m);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
marriage.c:82:3: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
82 | scanf("%d%d", &u, &v), u--, v--;
| ^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 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 |
Correct |
0 ms |
212 KB |
Output is correct |
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 |
1 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 |
Correct |
0 ms |
212 KB |
Output is correct |
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 |
6 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
2 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
31 |
Correct |
52 ms |
612 KB |
Output is correct |
32 |
Correct |
3 ms |
340 KB |
Output is correct |
33 |
Correct |
2 ms |
340 KB |
Output is correct |
34 |
Correct |
9 ms |
376 KB |
Output is correct |
35 |
Correct |
59 ms |
860 KB |
Output is correct |
36 |
Correct |
43 ms |
852 KB |
Output is correct |
37 |
Correct |
156 ms |
848 KB |
Output is correct |
38 |
Correct |
24 ms |
876 KB |
Output is correct |
39 |
Correct |
112 ms |
568 KB |
Output is correct |
40 |
Correct |
55 ms |
492 KB |
Output is correct |
41 |
Correct |
64 ms |
556 KB |
Output is correct |
42 |
Correct |
37 ms |
532 KB |
Output is correct |
43 |
Correct |
40 ms |
656 KB |
Output is correct |
44 |
Correct |
76 ms |
928 KB |
Output is correct |
45 |
Correct |
167 ms |
596 KB |
Output is correct |
46 |
Correct |
251 ms |
1088 KB |
Output is correct |
47 |
Correct |
145 ms |
972 KB |
Output is correct |
48 |
Correct |
192 ms |
976 KB |
Output is correct |
49 |
Correct |
284 ms |
1100 KB |
Output is correct |
50 |
Correct |
277 ms |
552 KB |
Output is correct |