#include<bits/stdc++.h>
#define pb push_back
using namespace std;
const int MAXN = 34004, MAXM = 268900;
int s, t, P[MAXN], qu[MAXN];
int ts, to[MAXM], flow[MAXM];
int head[MAXN], nxt[MAXM];
int ptr, mm;
inline void AddEdge(int v, int u, int w)
{
nxt[ts] = head[v]; head[v] = ts;
to[ts] = u; flow[ts] = w; ts ++;
nxt[ts] = head[u]; head[u] = ts;
to[ts] = v; flow[ts] = 0; ts ++;
}
inline bool BFS()
{
int l = 0, r = 0;
memset(P, -1, sizeof(P));
qu[r ++] = s;
while (r - l)
{
int v = qu[l ++];
while (~head[v] && mm < to[head[v]] && to[head[v]] <= ptr)
head[v] = nxt[head[v]];
for (int id = head[v], p = id; ~id; p = id, id = nxt[id])
{
if (mm < to[id] && to[id] <= ptr)
nxt[p] = nxt[id], id = p;
else if (flow[id] && P[to[id]] == -1)
{
P[to[id]] = id;
if (to[id] == t)
return (1);
qu[r ++] = to[id];
}
}
}
return (0);
}
inline int MaxFlow()
{
int Flow = 0;
while (BFS())
{
int v = t, _flow = INT_MAX;
while (v != s)
_flow = min(_flow, flow[P[v]]), v = to[P[v]^1];
Flow += _flow; v = t;
while (v != s)
{
flow[P[v]] -= _flow;
flow[P[v]^1] += _flow;
v = to[P[v]^1];
}
}
return Flow;
}
int n, m, k, source, sink;
int tff[2003];
vector < int > vec[MAXN];
inline void Add(int v)
{
for (int &u : vec[v])
AddEdge(u, v + m, 1);
AddEdge(v + m, sink, 1);
}
inline int Del(int v)
{
v += m;
if (flow[head[v]])
{
flow[head[v]] = 0;
//for (int e = head[v]; ~e; e = nxt[e])
//flow[e^1] = 0;
return (0);
}
flow[head[v]^1] = 0;
int e = head[v];
for (; !flow[e]; e = nxt[e]);
int u = to[e];
flow[e] = 0;
flow[tff[u]] = 0;
flow[tff[u]^1] = 1;
return (1);
}
int main()
{
memset(head, -1, sizeof(head));
memset(nxt, -1, sizeof(nxt));
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= k; i++)
{
int v, u;
scanf("%d%d", &v, &u);
vec[v].pb(u);
//AddEdge(u, v + m, 1);
}
source = 0; sink = n + m + 1;
s = source; t = sink;
for (int i = 1; i <= m; i++)
AddEdge(source, i, 1), tff[i] = ts - 1;
//for (int i = 1; i <= n; i++)
//AddEdge(m + i, sink, 0);
int r = 0, Flow = 0;
int tot = 0;
ptr = mm = m;
for (int l = 1; l <= n; l ++)
{
while (r < l)
r ++, Add(r);
Flow += MaxFlow();
while (r < n && Flow < m)
r ++, Add(r), Flow += MaxFlow();
if (Flow < m)
break;
tot += n - r + 1;
Flow -= Del(l);
ptr ++;
}
return !printf("%d\n", tot);
}
Compilation message
marriage.cpp: In function 'int main()':
marriage.cpp:92:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", &n, &m, &k);
~~~~~^~~~~~~~~~~~~~~~~~~~~~
marriage.cpp:96:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &v, &u);
~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
2424 KB |
Output is correct |
2 |
Correct |
4 ms |
2424 KB |
Output is correct |
3 |
Correct |
4 ms |
2424 KB |
Output is correct |
4 |
Correct |
4 ms |
2424 KB |
Output is correct |
5 |
Correct |
3 ms |
2424 KB |
Output is correct |
6 |
Correct |
4 ms |
2424 KB |
Output is correct |
7 |
Correct |
4 ms |
2424 KB |
Output is correct |
8 |
Correct |
4 ms |
2424 KB |
Output is correct |
9 |
Correct |
4 ms |
2424 KB |
Output is correct |
10 |
Correct |
3 ms |
2424 KB |
Output is correct |
11 |
Correct |
4 ms |
2424 KB |
Output is correct |
12 |
Correct |
4 ms |
2428 KB |
Output is correct |
13 |
Correct |
3 ms |
2424 KB |
Output is correct |
14 |
Correct |
4 ms |
2424 KB |
Output is correct |
15 |
Correct |
4 ms |
2424 KB |
Output is correct |
16 |
Correct |
4 ms |
2424 KB |
Output is correct |
17 |
Correct |
4 ms |
2424 KB |
Output is correct |
18 |
Correct |
4 ms |
2424 KB |
Output is correct |
19 |
Correct |
7 ms |
2552 KB |
Output is correct |
20 |
Correct |
5 ms |
2424 KB |
Output is correct |
21 |
Correct |
4 ms |
2424 KB |
Output is correct |
22 |
Correct |
4 ms |
2424 KB |
Output is correct |
23 |
Correct |
5 ms |
2552 KB |
Output is correct |
24 |
Correct |
4 ms |
2424 KB |
Output is correct |
25 |
Correct |
29 ms |
2696 KB |
Output is correct |
26 |
Correct |
15 ms |
2552 KB |
Output is correct |
27 |
Correct |
8 ms |
2424 KB |
Output is correct |
28 |
Correct |
9 ms |
2396 KB |
Output is correct |
29 |
Correct |
10 ms |
2680 KB |
Output is correct |
30 |
Correct |
10 ms |
2680 KB |
Output is correct |
31 |
Correct |
540 ms |
3660 KB |
Output is correct |
32 |
Correct |
32 ms |
2680 KB |
Output is correct |
33 |
Correct |
20 ms |
2424 KB |
Output is correct |
34 |
Correct |
23 ms |
2552 KB |
Output is correct |
35 |
Correct |
415 ms |
4608 KB |
Output is correct |
36 |
Correct |
290 ms |
4304 KB |
Output is correct |
37 |
Correct |
1314 ms |
3860 KB |
Output is correct |
38 |
Correct |
56 ms |
4856 KB |
Output is correct |
39 |
Correct |
377 ms |
2804 KB |
Output is correct |
40 |
Correct |
334 ms |
3044 KB |
Output is correct |
41 |
Correct |
373 ms |
3224 KB |
Output is correct |
42 |
Correct |
168 ms |
3576 KB |
Output is correct |
43 |
Correct |
176 ms |
3960 KB |
Output is correct |
44 |
Correct |
262 ms |
4672 KB |
Output is correct |
45 |
Correct |
782 ms |
4120 KB |
Output is correct |
46 |
Correct |
1076 ms |
5600 KB |
Output is correct |
47 |
Correct |
500 ms |
5540 KB |
Output is correct |
48 |
Correct |
462 ms |
5216 KB |
Output is correct |
49 |
Correct |
1128 ms |
5812 KB |
Output is correct |
50 |
Correct |
996 ms |
3268 KB |
Output is correct |