Cod sursa(job #3239288)

Utilizator nistor_dora_valentinaNistor Dora Valentina nistor_dora_valentina Data 4 august 2024 12:18:19
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.22 kb
#include <fstream>
#include <vector>
#include <bits/stdc++.h>

using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
int n, m, e, i, x, y, mt[100005], viz[100005], change, l[100005], cnt;
vector <int> g[100005];
vector <pair<int, int>> ans;
bool try_kuhn(int v)
{
    if(viz[v]==1)
        return false;
    viz[v]=true;
    for(auto i: g[v])
    {
        if(mt[i]==-1 || try_kuhn(mt[i]))
        {
            mt[i]=v;
            l[v]=i;
            return true;
        }
    }
    return false;
}
int main()
{
    fin>>n>>m>>e;
    for(i=1; i<=e; i++)
    {
        fin>>x>>y;
        g[x].push_back(y);
    }
    for(i=1; i<=m; i++)
        mt[i]=-1;
    for(i=1; i<=n; i++)
        l[i]=-1;
    change=1;
    while(change)
    {
        change=0;
        for(i=1; i<=n; i++)
            viz[i]=0;
        for(i=1; i<=n; i++)
            if(l[i]==-1)
            change|=try_kuhn(i);
    }
    for(i=1; i<=m; i++)
      if(mt[i]>0)
      cnt++;
    fout<<cnt<<'\n';
    for(i=1; i<=m; i++)
        if(mt[i]>0)
    ans.push_back({mt[i], i});
    sort(ans.begin(), ans.end());
    for(auto i: ans)
        fout<<i.first<<" "<<i.second<<'\n';
    return 0;
}