Cod sursa(job #2905637)

Utilizator 100pCiornei Stefan 100p Data 22 mai 2022 20:24:24
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.52 kb
#include <bits/stdc++.h>
#define ull unsigned long long
#define FILES freopen("cuplaj.in","r",stdin);\
              freopen("cuplaj.out","w",stdout);
#define CMAX 15485863
#define fastio std::ios_base::sync_with_stdio(NULL),cin.tie(NULL),cout.tie(NULL);
#define mp make_pair
#define INF 1e18
#define mod 666013
#define ll long long
#define SMAX 65000000
#define MAX 10000
#define pb push_back
#define add emplace_back
#define void inline void

using namespace std;
vector<int>v[MAX+5];
int n,m,ed,a,b,go[MAX+5],ans[MAX+5];
bool ok = 1, check[MAX+5], okk[MAX+5];
bool Good(int x)
{
    if(check[x]) return 0;
    check[x] = 1;
    for(auto i : v[x])
    {
        if(!go[i])
        {
            okk[x] = 1;
            go[i] = x;
            ans[x] = i;
            return 1;
        }
    }
    for(auto i : v[x])
    {
        if(Good(go[i]))
        {
            okk[x] = 1;
            go[i] = x;
            ans[x] = i;
            return 1;
        }
    }
    return 0;
}
int main()
{
    fastio
    FILES
    cin >> n >> m >> ed;
    for(int i = 1;i <= ed; ++i)
        cin >> a >> b, v[a].add(b);
    while(ok)
    {
        for(int i = 1;i <= n; ++i) check[i] = 0;
        ok = 0;
        for(int i = 1;i <= n; ++i){
            if(!okk[i])
            ok |= Good(i);
        }
    }
    int ANS = 0;
    for(int i = 1;i <= n; ++i)
        if(ans[i]) ANS++;
    cout << ANS << '\n';
    for(int i = 1;i <= n; ++i)
        if(ans[i]) cout << i << ' ' << ans[i] << '\n';
}