Cod sursa(job #2686152)

Utilizator SochuDarabaneanu Liviu Eugen Sochu Data 18 decembrie 2020 16:43:25
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.3 kb
#include <bits/stdc++.h>
//#pragma GCC optimize ("03")
#define FastIO ios_base::sync_with_stdio(false) , cin.tie(0) , cout.tie(0)
#define FILES freopen("cuplaj.in" , "r" , stdin) , freopen("cuplaj.out" , "w" , stdout)
#define ll long long
#define ull unsigned long long
#define ld long double
#define eb emplace_back
#define pb push_back
#define qwerty1 first
#define qwerty2 second
#define qwerty3 -> first
#define qwerty4 -> second
#define umap unordered_map
#define uset unordered_set
#define pii pair < int , int >
#define pq priority_queue
#define dbg(x) cerr << #x << ": " << x << '\n'

namespace FastRead
{
    char buff[5000];int lg = 0 , p = 0;
    char nc()
    {
        if(lg == p){lg = fread(buff , 1 , 5000 , stdin);p = 0;if(!lg) return EOF;}
        return buff[p++];
    }
    template<class T>void read(T&x)
    {
        T sgn = 1; char c;while(!isdigit(c = nc()))if(c == '-')sgn = -1;
        x = c - '0';while(isdigit(c = nc()))x = x * 10 + c - '0';x *= sgn;
    }
}

using namespace FastRead;
using namespace std;

const int N = 1e4 + 10;
const int M = 1e9 + 7;
const ld PI = acos(-1);
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());


int n , m , e , x , y;
int r[N] , l[N];
bool mark[N];
vector < int > g[N];

bool pairing(int x)
{
    if(mark[x])
        return 0;

    mark[x] = 1;

    for(auto i : g[x])
        if(!r[i])
        {
            r[i] = x;
            l[x] = i;
            return 1;
        }

    for(auto i : g[x])
        if(pairing(r[i]))
        {
            l[x] = i;
            r[i] = x;
            return 1;
        }

    return 0;
}

signed main()
{
	#ifndef ONLINE_JUDGE
		FastIO , FILES;
	#endif

    int i;

    cin >> n >> m >> e;

    for(i = 1 ; i <= e ; i++)
    {
        cin >> x >> y;
        g[x].pb(y);
    }

    bool change = 1;

    while(change)
    {
        change = 0;

        for(i = 1 ; i <= n ; i++)
            mark[i] = 0;

        for(i = 1 ; i <= n ; i++)
            if(!l[i])
                change |= pairing(i);
    }

    int cnt = 0;

    for(i = 1 ; i <= n ; i++)
        cnt += (l[i] > 0);

    cout << cnt << '\n';

    for(i = 1 ; i <= n ; i++)
        if(l[i] > 0)
            cout << i << ' ' << l[i] << '\n';

    return 0;
}