Cod sursa(job #2746758)

Utilizator Sho10Andrei Alexandru Sho10 Data 28 aprilie 2021 13:57:09
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.59 kb
#include <bits/stdc++.h> //Andrei Alexandru a.k.a Sho
#define ll long long
#define double long double
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#define aint(a) (a).begin(), (a).end()
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define pi pair
#define rc(s) return cout<<s,0
#define endl '\n'
#define mod 1000000007
#define PI 3.14159265359
#define INF 1000000005
#define LINF 1000000000000000005ll
#define CODE_START  ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
ll n,m,e,cuplu[200005],viz[200005];
vector<ll>g[200005];
ll dfs(ll node){
if(viz[node]){
    return 0;
}
viz[node]=1;
for(auto it : g[node]){
    if(cuplu[it]==0){
        cuplu[it]=node;
        cuplu[node]=it;
        return 1;
    }
}
for(auto it : g[node]){
    if(cuplu[it]!=0){
        if(dfs(cuplu[it])){
            cuplu[node]=it;
            cuplu[it]=node;
            return 1;
        }
    }
}
return 0;
}
int32_t main(){
CODE_START;
ifstream cin("cuplaj.in");
ofstream cout("cuplaj.out");
cin>>n>>m>>e;
for(ll i=1;i<=e;i++)
{
    ll x,y;
    cin>>x>>y;
    g[x].pb(y+n);
    g[y+n].pb(x);
}
while(true){
    for(ll i=1;i<=n;i++)
    {
        viz[i]=0;
    }
    ll check=0;
    for(ll i=1;i<=n;i++)
    {
        if(cuplu[i]==0){
            check|=dfs(i);
        }
    }
    if(check==0){
        break;
    }
}
ll ans=0;
for(ll i=1;i<=n;i++)
{
    if(cuplu[i]){
        ans++;
    }
}
cout<<ans<<endl;
for(ll i=1;i<=n;i++)
{
    if(cuplu[i]){
    cout<<i<<' '<<cuplu[i]-n<<endl;
}
}
}