# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
341266 | juggernaut | Marriage questions (IZhO14_marriage) | C++14 | 440 ms | 3504 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
#define MAXN 100009
#define INF 1000000007
#define mp(x,y) make_pair(x,y)
#define all(v) v.begin(),v.end()
#define pb(x) push_back(x)
#define wr cout<<"----------------"<<endl;
#define ppb() pop_back()
#define tr(ii,c) for(__typeof((c).begin()) ii=(c).begin();ii!=(c).end();ii++)
#define ff first
#define ss second
#define my_little_dodge 46
#define debug(x) cerr<< #x <<" = "<< x<<endl;
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
template<class T>bool umin(T& a,T b){if(a>b){a=b;return 1;}return 0;}
template<class T>bool umax(T& a,T b){if(a<b){a=b;return 1;}return 0;}
int vis[MAXN],l,r=1,ver;
vector<int>adj[MAXN];
int match[MAXN];
bool dfs(int nd){
if(vis[nd]==ver)return 0;
vis[nd]=ver;
tr(it,adj[nd])
if(l<=*it and *it<=r)
if(match[*it]==-1 or dfs(match[*it])){
match[*it]=nd;
return 1;
}
return 0;
}
int main(){
//freopen("file.in", "r", stdin);
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=k;i++){
int a,b;
scanf("%d%d",&a,&b);
adj[b].pb(a);
}
for(int i=1;i<=m;i++)sort(all(adj[i]));
vector<int>need;
for(int i=1;i<=m;i++)
need.pb(i);
for(int i=1;i<=n;i++)
match[i]=-1;
ll ans=0;
for(l=1;max(r,l)<=n;l++){
while(!need.empty()){
int nd=need.back();ver++;
while(r<=n and !dfs(nd))
r++,ver++;
if(r>n)
break;
need.ppb();
}
ans+=n-r+1;
if(~match[l])
need.pb(match[l]),match[l]=-1;
}
printf("%lld\n",ans);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |