Cod sursa(job #2854819)

Utilizator LionMan101Achim-Panescu Silvian LionMan101 Data 21 februarie 2022 19:48:35
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.56 kb
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
const int INF=1e9+7;
const ll INFF=1e18+7;
#define nl '\n'
void __print(int x) { cout << x; }
void __print(long x) { cout << x; }
void __print(long long x) { cout << x; }
void __print(unsigned x) { cout << x; }
void __print(unsigned long x) { cout << x; }
void __print(unsigned long long x) { cout << x; }
void __print(float x) { cout << x; }
void __print(double x) { cout << x; }
void __print(long double x) { cout << x; }
void __print(char x) { cout << '\'' << x << '\''; }
void __print(const char* x) { cout << '\"' << x << '\"'; }
void __print(const string& x) { cout << '\"' << x << '\"'; }
void __print(bool x) { cout << (x ? "true" : "false"); }
template<typename T, typename V>
void __print(const pair<T, V>& x) { cout << '{'; __print(x.first); cout << ','; __print(x.second); cout << '}'; }
template<typename T>
void __print(const T& x) { int f = 0; cout << '{'; for (auto& i : x) cout << (f++ ? "," : ""), __print(i); cout << "}"; }
void _print() { cout << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) { __print(t); if (sizeof...(v)) cout << ", "; _print(v...); }
#ifndef ONLINE_JUDGE
#define debug(x...) cout << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif

const int maxn=10'001;

vector<vector<int>> a(maxn);
vector<int> l(maxn), r(maxn);
vector<bool> visited(maxn);
int n,m,e;

bool dfs(int x){
    if(visited[x]) return false;
    visited[x]=true;
    for(int& i:a[x]){
        if(!l[i] || dfs(l[i])){
            r[x]=i, l[i]=x;
            return true;
        }
    }
    return false;
}


void algoritmBlanao(){ //Hopcroft Karp
    bool ok=true;
    while(ok){
        ok=false;
        fill(visited.begin(),visited.end(),false);
        for(int i=1; i<=n; i++){
            if(!r[i] && dfs(i)) ok=true;
        }
    }
}

void solve()
{
    cin>>n>>m>>e;
    for(int i=0; i<e; i++){
        int x,y;
        cin>>x>>y;
        a[x].emplace_back(y);
    }
    algoritmBlanao();
    int nr=0;
    for(int i=1; i<=n; i++) if(r[i]) nr++;
    cout<<nr<<nl;
    for(int i=1; i<=n; i++) if(r[i]) cout<<i<<" "<<r[i]<<nl;
}

int main()
{
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);

    freopen("cuplaj.in", "r", stdin);
    freopen("cuplaj.out", "w", stdout);

    ios::sync_with_stdio(0);
    cin.tie(0);
    int t=1;
    // int t;
    // cin >> t;
    for(int tt=1; tt<=t; tt++){
        // cout<<"#Case "<<t<<nl;
        solve();
    }
    return 0;
}