Cod sursa(job #2748909)

Utilizator bigmixerVictor Purice bigmixer Data 3 mai 2021 23:04:50
Problema Cuplaj maxim in graf bipartit Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.59 kb
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define all(a) (a).begin(), (a).end()
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#define fr first
#define sc second
#define int long long
#define rc(s) return cout<<s,0
#define rcc(s) cout<<s,exit(0)

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int nmax = 10005;

int n, m, e, l[nmax], r[nmax], cuplaj;
vector<int>nod[nmax];
bitset<nmax>viz;

bool dfs(int s){
    if(viz[s]) return false;
    viz[s] = 1;
    for(auto it : nod[s]){
        if(!r[it]){
            r[it] = s;
            l[s] = it;
            return true;
        }
    }
    for(auto it : nod[s]){
        if(dfs(r[it])){
            l[s] = it;
            r[it] = s;
            return true;
        }
    }
    return false;
}

int32_t main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
    freopen("cuplaj.in", "r", stdin);
    freopen("cuplaj.out", "w", stdout);
    cin >> n >> m >> e;
    for(int i=1;i<=e;i++){
        int x, y;
        cin >> x >> y;
        nod[x].push_back(y);
    }
    for(int change = 1; change;){
        change = 0;
        for(int i=1;i<=n;i++) viz[i] = 0;
        for(int i=1;i<=n;i++){
            if(!l[i]) change |= dfs(i);
        }
        for(int i=1;i<=n;i++) cout << l[i] << ' ';
        cout << '\n';
    }
    for(int i=1;i<=n;i++){
        if(l[i]) cuplaj++;
    }
    cout << cuplaj << '\n';
    for(int i=1;i<=n;i++){
        if(l[i]) cout << i << ' ' << l[i] << '\n';
    }
}