#include <bits/stdc++.h>
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("-O3")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define vec vector
#define sz(x) ( int)x.size()
#define m_p make_pair
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
using namespace std;
typedef long long ll;
typedef pair<ll, int> pli;
typedef pair<int,ll> pil;
typedef pair<int,int> pii;
typedef unsigned long long ull;
auto rng=bind(uniform_int_distribution<int>(1,1000),mt19937(time(0)));
template<class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);}
template<class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);}
const int N=3e4+1;
const int K=165;
const int M=2001;
vec<int> g[N+M];
int uk;
int mt[N+M];
int h[N+M],u[N+M],tt,n,m;
int tr;
vec<int>rem;
bool kuhn(int v){
if(u[v]==tt) return 0;
u[v]=tt;
for(auto &z : g[v]){
if(h[z] && (mt[z]==-1 || kuhn(mt[z]))){
mt[z]=v;mt[v]=z;
return 1;
}
}
return 0;
}
signed main(){
// ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int k;
scanf("%d%d%d",&n,&m,&k);
for(int i=0;i<k;i++){
int v,u;
scanf("%d%d",&v,&u);--v;--u;
g[v].pb(u+n);
g[u+n].pb(v);
}
fill(mt,mt+n+m,-1);
for(int i=0;i<m;i++) h[i+n]=1,rem.pb(i+n);
int r=-1;
int c=0;
ll ans=0;
for(int l=0;l<n;l++){
if(l){
h[l-1]=0;
if(mt[l-1]!=-1){
tt++;
mt[mt[l-1]]=-1;
c--;
rem.pb(mt[l-1]);
mt[l-1]=-1;
}
}
while(r<n && c!=m){
r++;tt++;h[r]=1;
while(c!=m && kuhn(rem.back())){
c++;rem.pop_back();
}
}
ans+=n-r;
}
printf("%lld",ans);
return 0;
}
/*
5
1 2
2 3
3 4
4 5
5 1
*/
Compilation message
marriage.cpp: In function 'int main()':
marriage.cpp:46:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
46 | scanf("%d%d%d",&n,&m,&k);
| ~~~~~^~~~~~~~~~~~~~~~~~~
marriage.cpp:49:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | scanf("%d%d",&v,&u);--v;--u;
| ~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
972 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
972 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
972 KB |
Output isn't correct |
4 |
Correct |
1 ms |
972 KB |
Output is correct |
5 |
Correct |
1 ms |
972 KB |
Output is correct |
6 |
Correct |
1 ms |
972 KB |
Output is correct |
7 |
Correct |
1 ms |
972 KB |
Output is correct |
8 |
Correct |
1 ms |
972 KB |
Output is correct |
9 |
Correct |
1 ms |
972 KB |
Output is correct |
10 |
Correct |
2 ms |
972 KB |
Output is correct |
11 |
Correct |
1 ms |
972 KB |
Output is correct |
12 |
Correct |
1 ms |
984 KB |
Output is correct |
13 |
Correct |
1 ms |
972 KB |
Output is correct |
14 |
Correct |
1 ms |
972 KB |
Output is correct |
15 |
Correct |
1 ms |
972 KB |
Output is correct |
16 |
Correct |
1 ms |
972 KB |
Output is correct |
17 |
Correct |
1 ms |
972 KB |
Output is correct |
18 |
Correct |
1 ms |
1060 KB |
Output is correct |
19 |
Correct |
2 ms |
1100 KB |
Output is correct |
20 |
Correct |
1 ms |
972 KB |
Output is correct |
21 |
Correct |
1 ms |
972 KB |
Output is correct |
22 |
Correct |
1 ms |
972 KB |
Output is correct |
23 |
Correct |
2 ms |
972 KB |
Output is correct |
24 |
Correct |
2 ms |
972 KB |
Output is correct |
25 |
Correct |
6 ms |
1228 KB |
Output is correct |
26 |
Incorrect |
2 ms |
1060 KB |
Output isn't correct |
27 |
Correct |
1 ms |
972 KB |
Output is correct |
28 |
Correct |
1 ms |
972 KB |
Output is correct |
29 |
Incorrect |
5 ms |
1100 KB |
Output isn't correct |
30 |
Incorrect |
3 ms |
1100 KB |
Output isn't correct |
31 |
Correct |
82 ms |
1728 KB |
Output is correct |
32 |
Incorrect |
2 ms |
1100 KB |
Output isn't correct |
33 |
Correct |
1 ms |
1100 KB |
Output is correct |
34 |
Correct |
2 ms |
1100 KB |
Output is correct |
35 |
Correct |
69 ms |
2160 KB |
Output is correct |
36 |
Correct |
52 ms |
2092 KB |
Output is correct |
37 |
Correct |
163 ms |
1844 KB |
Output is correct |
38 |
Incorrect |
29 ms |
2372 KB |
Output isn't correct |
39 |
Correct |
58 ms |
1356 KB |
Output is correct |
40 |
Correct |
5 ms |
1356 KB |
Output is correct |
41 |
Incorrect |
7 ms |
1576 KB |
Output isn't correct |
42 |
Incorrect |
9 ms |
1676 KB |
Output isn't correct |
43 |
Incorrect |
15 ms |
1864 KB |
Output isn't correct |
44 |
Incorrect |
25 ms |
2340 KB |
Output isn't correct |
45 |
Incorrect |
12 ms |
2124 KB |
Output isn't correct |
46 |
Incorrect |
33 ms |
2760 KB |
Output isn't correct |
47 |
Incorrect |
30 ms |
2804 KB |
Output isn't correct |
48 |
Incorrect |
26 ms |
2652 KB |
Output isn't correct |
49 |
Incorrect |
36 ms |
2948 KB |
Output isn't correct |
50 |
Correct |
215 ms |
1460 KB |
Output is correct |