#include<bits/stdc++.h>
#define pb push_back
using namespace std;
const int MAXN = 34004;
struct Edge {int to, flow;};
struct Dinic
{
int s, t, P[MAXN], D[MAXN], qu[MAXN];
vector < int > Adj[MAXN];
vector < Edge > E;
inline void Add(int v, int u, int w)
{
Adj[v].pb(E.size());
E.pb({u, w});
Adj[u].pb(E.size());
E.pb({v, 0});
}
inline bool BFS()
{
int l = 0, r = 0;
memset(D, -1, sizeof(D));
memset(P, 0, sizeof(P));
qu[r ++] = s; D[s] = 0;
while (r - l)
{
int v = qu[l ++];
for (int &id : Adj[v])
if (E[id].flow && D[E[id].to] == -1)
{
D[E[id].to] = D[v] + 1;
if (E[id].to == t)
return (1);
qu[r ++] = E[id].to;
}
}
return (0);
}
int DFS(int v, int flow)
{
if (v == t)
return (flow);
int Flow = 0;
for (int i = P[v]; i < (int)Adj[v].size(); i++)
{
int id = Adj[v][i];
if (E[id].flow && D[E[id].to] == D[v] + 1)
{
int _flow = DFS(E[id].to, min(flow, E[id].flow));
if (!_flow)
swap(Adj[v][i], Adj[v][P[v]]), P[v] ++;
E[id].flow -= _flow;
E[id^1].flow += _flow;
Flow += _flow; flow -= _flow;
if (!flow) return (Flow);
}
else
swap(Adj[v][i], Adj[v][P[v]]), P[v] ++;
}
return (Flow);
}
inline int MaxFlow()
{
int Flow = 0;
while (BFS())
Flow += DFS(s, INT_MAX);
return Flow;
}
};
int n, m, k;
Dinic G;
inline void Add(int v)
{
v += m;
for (int &id : G.Adj[v])
if (G.E[id].to == G.t)
{G.E[id].flow = 1; return ;}
assert(0);
}
inline int Del(int v)
{
v += m;
for (int &id : G.Adj[v])
if (G.E[id].to == G.t)
{
if (G.E[id^1].flow == 1)
{
int e = -1;
for (int &idd : G.Adj[v])
if (G.E[idd].flow)
{e = idd; break;}
assert(e != -1);
int u = G.E[e].to;
G.E[e].flow -= 1;
G.E[e^1].flow += 1;
G.E[id^1].flow = 0;
for (int &idd : G.Adj[u])
if (G.E[idd].to == G.s)
{
G.E[idd].flow = 0;
G.E[idd^1].flow = 1;
return (1);
}
assert(0);
}
else
{G.E[id].flow = 0; return (0);}
}
assert(0);
}
int main()
{
scanf("%d%d%d", &n, &m, &k);
int source = 0, sink = n + m + 1;
G.s = source; G.t = sink;
for (int i = 1; i <= m; i++)
G.Add(source, i, 1);
for (int i = 1; i <= n; i++)
G.Add(m + i, sink, 0);
for (int i = 1; i <= k; i++)
{
int v, u;
scanf("%d%d", &v, &u);
G.Add(u, v + m, 1);
}
int r = 0, Flow = 0;
int tot = 0;
for (int l = 1; l <= n; l ++)
{
while (r < l)
r ++, Add(r);
Flow += G.MaxFlow();
while (r < n && Flow < m)
r ++, Add(r), Flow += G.MaxFlow();
if (Flow < m)
break;
tot += n - r + 1;
Flow -= Del(l);
}
return !printf("%d\n", tot);
}
Compilation message
marriage.cpp: In function 'int main()':
marriage.cpp:113: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:123: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 |
3 ms |
1400 KB |
Output is correct |
2 |
Correct |
3 ms |
1400 KB |
Output is correct |
3 |
Correct |
3 ms |
1400 KB |
Output is correct |
4 |
Correct |
3 ms |
1400 KB |
Output is correct |
5 |
Correct |
3 ms |
1400 KB |
Output is correct |
6 |
Correct |
3 ms |
1404 KB |
Output is correct |
7 |
Correct |
3 ms |
1400 KB |
Output is correct |
8 |
Correct |
3 ms |
1400 KB |
Output is correct |
9 |
Correct |
3 ms |
1400 KB |
Output is correct |
10 |
Correct |
3 ms |
1400 KB |
Output is correct |
11 |
Correct |
3 ms |
1400 KB |
Output is correct |
12 |
Correct |
3 ms |
1400 KB |
Output is correct |
13 |
Correct |
3 ms |
1400 KB |
Output is correct |
14 |
Correct |
3 ms |
1400 KB |
Output is correct |
15 |
Correct |
4 ms |
1400 KB |
Output is correct |
16 |
Correct |
3 ms |
1400 KB |
Output is correct |
17 |
Correct |
3 ms |
1448 KB |
Output is correct |
18 |
Correct |
3 ms |
1404 KB |
Output is correct |
19 |
Correct |
10 ms |
1528 KB |
Output is correct |
20 |
Correct |
7 ms |
1400 KB |
Output is correct |
21 |
Correct |
5 ms |
1400 KB |
Output is correct |
22 |
Correct |
5 ms |
1400 KB |
Output is correct |
23 |
Correct |
6 ms |
1400 KB |
Output is correct |
24 |
Correct |
6 ms |
1400 KB |
Output is correct |
25 |
Correct |
112 ms |
1908 KB |
Output is correct |
26 |
Correct |
35 ms |
1528 KB |
Output is correct |
27 |
Correct |
12 ms |
1400 KB |
Output is correct |
28 |
Correct |
13 ms |
1400 KB |
Output is correct |
29 |
Correct |
29 ms |
1780 KB |
Output is correct |
30 |
Correct |
27 ms |
1780 KB |
Output is correct |
31 |
Correct |
1091 ms |
3180 KB |
Output is correct |
32 |
Correct |
116 ms |
1668 KB |
Output is correct |
33 |
Correct |
28 ms |
1512 KB |
Output is correct |
34 |
Correct |
42 ms |
1528 KB |
Output is correct |
35 |
Correct |
799 ms |
4844 KB |
Output is correct |
36 |
Correct |
591 ms |
4580 KB |
Output is correct |
37 |
Execution timed out |
1571 ms |
3332 KB |
Time limit exceeded |
38 |
Correct |
1063 ms |
5092 KB |
Output is correct |
39 |
Correct |
615 ms |
2292 KB |
Output is correct |
40 |
Execution timed out |
1577 ms |
2288 KB |
Time limit exceeded |
41 |
Execution timed out |
1565 ms |
2796 KB |
Time limit exceeded |
42 |
Correct |
1485 ms |
3180 KB |
Output is correct |
43 |
Execution timed out |
1579 ms |
4584 KB |
Time limit exceeded |
44 |
Execution timed out |
1565 ms |
5220 KB |
Time limit exceeded |
45 |
Execution timed out |
1591 ms |
4072 KB |
Time limit exceeded |
46 |
Execution timed out |
1551 ms |
6376 KB |
Time limit exceeded |
47 |
Execution timed out |
1577 ms |
8164 KB |
Time limit exceeded |
48 |
Execution timed out |
1571 ms |
6116 KB |
Time limit exceeded |
49 |
Execution timed out |
1564 ms |
8296 KB |
Time limit exceeded |
50 |
Execution timed out |
1572 ms |
3440 KB |
Time limit exceeded |