#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[M];
vec<int>gr[N];
int uk;
int vl[N];
int mt[N];
int wh[N];
int u[N],tt,n,m;
int tl,tr;
bool kuhn(int v){
if(u[v]==tt) return 0;
u[v]=tt;
for(auto &z : g[v]){
if(z<=tr && mt[z]==-1){
mt[z]=v;
vl[uk++]=v;
return 1;
}
}
for(auto &z : g[v]){
if(z<=tr && kuhn(mt[z])){
mt[z]=v;
vl[uk++]=v;
return 1;
}
}
return 0;
}
int check(int l,int r){
tl=l;tr=r;
for(int i=0;i<uk;i++){
mt[vl[i]]=-1;
}
uk=0;
for(int i=0;i<m;i++){
tt++;
if(!kuhn(i)) return 0;
}
return 1;
}
bool kuhn2(int v){
if(u[v]==tt) return 0;
u[v]=tt;
for(auto &z : gr[v]){
if(mt[z]==-1){
mt[z]=v;
return 1;
}
}
for(auto &z : gr[v]){
if(kuhn(mt[z])){
mt[z]=v;
return 1;
}
}
return 0;
}
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int k;
cin>>n>>m>>k;
for(int i=0;i<k;i++){
int v,u;
cin>>v>>u;--v;--u;
gr[v].pb(u);
}
for(int i=0;i<N;i++){
mt[i]=-1;
wh[i]=-1;
}
int l=n;
int h=0;
while(h!=m && l>0){
l--;
tt++;
h+=kuhn2(l);
}
if(h!=m){
cout<<0;
return 0;
}
fill(mt,mt+n,-1);
for(int i=n-1;i>l;i--){
for(auto &z : gr[i]) g[z].pb(i);
}
int r=n-1;
ll ans=0;
for(int i=l;i>=0;i--){
// cout<<i<<endl;
for(auto &z : gr[i]) g[z].pb(i);
while(check(i,r-1)) r--;
// cout<<i<<' '<<r<<endl;
ans+=(n-r);
}
cout<<ans;
return 0;
}
/*
5
1 2
2 3
3 4
4 5
5 1
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
1228 KB |
Output isn't correct |
2 |
Incorrect |
1 ms |
1228 KB |
Output isn't correct |
3 |
Incorrect |
1 ms |
1228 KB |
Output isn't correct |
4 |
Incorrect |
1 ms |
1228 KB |
Output isn't correct |
5 |
Correct |
1 ms |
1228 KB |
Output is correct |
6 |
Correct |
1 ms |
1228 KB |
Output is correct |
7 |
Incorrect |
1 ms |
1228 KB |
Output isn't correct |
8 |
Correct |
1 ms |
1228 KB |
Output is correct |
9 |
Correct |
1 ms |
1228 KB |
Output is correct |
10 |
Correct |
1 ms |
1228 KB |
Output is correct |
11 |
Correct |
1 ms |
1228 KB |
Output is correct |
12 |
Correct |
1 ms |
1228 KB |
Output is correct |
13 |
Incorrect |
1 ms |
1228 KB |
Output isn't correct |
14 |
Incorrect |
1 ms |
1228 KB |
Output isn't correct |
15 |
Incorrect |
1 ms |
1228 KB |
Output isn't correct |
16 |
Incorrect |
1 ms |
1228 KB |
Output isn't correct |
17 |
Incorrect |
1 ms |
1228 KB |
Output isn't correct |
18 |
Incorrect |
1 ms |
1228 KB |
Output isn't correct |
19 |
Incorrect |
2 ms |
1356 KB |
Output isn't correct |
20 |
Incorrect |
1 ms |
1228 KB |
Output isn't correct |
21 |
Correct |
1 ms |
1228 KB |
Output is correct |
22 |
Incorrect |
1 ms |
1228 KB |
Output isn't correct |
23 |
Incorrect |
1 ms |
1228 KB |
Output isn't correct |
24 |
Incorrect |
1 ms |
1228 KB |
Output isn't correct |
25 |
Incorrect |
9 ms |
1492 KB |
Output isn't correct |
26 |
Incorrect |
2 ms |
1356 KB |
Output isn't correct |
27 |
Correct |
1 ms |
1228 KB |
Output is correct |
28 |
Incorrect |
1 ms |
1228 KB |
Output isn't correct |
29 |
Incorrect |
3 ms |
1356 KB |
Output isn't correct |
30 |
Incorrect |
3 ms |
1356 KB |
Output isn't correct |
31 |
Incorrect |
42 ms |
1804 KB |
Output isn't correct |
32 |
Incorrect |
2 ms |
1356 KB |
Output isn't correct |
33 |
Correct |
1 ms |
1228 KB |
Output is correct |
34 |
Incorrect |
1 ms |
1228 KB |
Output isn't correct |
35 |
Incorrect |
33 ms |
2320 KB |
Output isn't correct |
36 |
Incorrect |
17 ms |
2244 KB |
Output isn't correct |
37 |
Runtime error |
15 ms |
3148 KB |
Execution killed with signal 11 |
38 |
Incorrect |
151 ms |
2580 KB |
Output isn't correct |
39 |
Runtime error |
4 ms |
2636 KB |
Execution killed with signal 11 |
40 |
Runtime error |
5 ms |
2892 KB |
Execution killed with signal 11 |
41 |
Runtime error |
7 ms |
3008 KB |
Execution killed with signal 11 |
42 |
Runtime error |
10 ms |
3400 KB |
Execution killed with signal 11 |
43 |
Runtime error |
15 ms |
3388 KB |
Execution killed with signal 11 |
44 |
Runtime error |
23 ms |
3728 KB |
Execution killed with signal 11 |
45 |
Runtime error |
11 ms |
3696 KB |
Execution killed with signal 11 |
46 |
Runtime error |
27 ms |
4496 KB |
Execution killed with signal 11 |
47 |
Runtime error |
27 ms |
4164 KB |
Execution killed with signal 11 |
48 |
Runtime error |
25 ms |
4176 KB |
Execution killed with signal 11 |
49 |
Runtime error |
29 ms |
4676 KB |
Execution killed with signal 11 |
50 |
Runtime error |
3 ms |
2636 KB |
Execution killed with signal 11 |