Cod sursa(job #3293932)

Utilizator pacelaaaCiurea Pavel pacelaaa Data 13 aprilie 2025 17:03:33
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.43 kb
#include <bits/stdc++.h>

using namespace std;

ifstream fin ( "cuplaj.in" );
ofstream fout ( "cuplaj.out" );

#define cin fin
#define cout fout
#define FR( a, b ) for( int a = 0; a < (int) (b); a++ )
#define FOR( a, c, b ) for( int a = (c); a < (int) b; a ++ )
#define PB push_back

const int Nmax = 10001;

vector<int> adj[Nmax];
bool used[Nmax];
int match_l[Nmax], match_r[Nmax];


bool cuplaj( int nod ) {
  if( used[nod] )
    return false;
  used[nod] = true;
  for( int i = 0; i < adj[nod].size(); i ++  ) {
    int u = adj[nod][i];
    if( match_r[u] == 0 || cuplaj( match_r[u] ) ) {
      match_l[nod] = u;
      match_r[u] = nod;
      return true;
    }
  }
  return false;
}

int main()
{
    int n, m, no_edges, u, nod, ans;


    cin >> n >> m >> no_edges;
    FR( i, no_edges ) {
      cin >> u >> nod;
      adj[u].PB( nod );
    }
    /*FOR( i,1,n+1) {
      FR( j, adj[i].size() )
        cout << adj[i][j] << " ";
      cout << '\n';
    }*/

    bool change = true;
    while( change ) {
      change = false;
      FOR( i, 1, n + 1 )
        used[i] = false;

      FOR( i,1,n+1) {
        if( match_l[i] == 0 )
          change |= cuplaj(i);
      }
    }

    ans = 0;
    FOR( i,1,n+1)
      if( match_l[i] != 0 )
        ans++;
    cout << ans << '\n';
    FOR( i,1,n+1)
      if( match_l[i] != 0 )
        cout << i << " " << match_l[i] << '\n';
    return 0;
}