Cod sursa(job #2960578)

Utilizator razvan1403razvan razvan1403 Data 4 ianuarie 2023 18:19:27
Problema Cuplaj maxim in graf bipartit Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.01 kb
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define l long
#define d double
#define in int
#define si(x) scanf('%d', &x)
#define sl(x) scanf('%lld', &x)
#define ss(s) scanf('%s', s) 
#define pi(x) printf('%d', x)
#define pl(x) printf('%lld', x)
#define ps(s) printf('%s', s) 
#define pb push_back 
#define mp make_pair 
#define F first 
#define S second 
#define all(x) x.begin(), x.end() 
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x)) 
typedef pair<int, int> pii; 
typedef pair<ll, ll> pl; 
typedef vector<int> vi; 
typedef vector<ll> vl; 
#define modulo 1000000007
#define FOR(i,a,b) for(int i=a;i<=b;i++)
typedef vector<pii> vpii;

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

int n,m,k;
int x,y;
int legaturi_valide;
vector<vector<int>> legaturi;
vector<int> vizitat;
vector<int> Left;
vector<int> Right;

int dfs(int start){
  if(vizitat[start] == 1)
    return 0;
  vizitat[start] = 1;
  for(int i=0;i<legaturi[start].size();i++){
    int nod_curent = legaturi[start][i];
    if(Right[nod_curent] == 0 || dfs(Right[nod_curent])){
      Left[start] = nod_curent;
      Right[nod_curent] = start;
      return 1;
    }
  }
  return 0;
}

void solve(){
  fin>>n>>m>>k;
  legaturi = vector<vector<int>>(k + 1);
  Left = vector<int> (n+1, 0);
  Right = vector<int> (m+1, 0);
  for(int i=1;i<=k;i++){
    fin>>x>>y;
    legaturi[x].push_back(y);
  }
  do{
    legaturi_valide = 0;
    vizitat = vector<int> (k+1, 0);
    for(int i=1;i<=n;i++){
      if(Left[i] == 0)
        legaturi_valide = legaturi_valide + dfs(i);
    }
  }while(legaturi_valide != 0);
  for(int i=1;i<=n;i++)
    if(Left[i] != 0)
      legaturi_valide += 1;
  fout<<legaturi_valide<<endl;
  for(int i=1;i<=n;i++)
    if(Left[i] != 0)
      fout<<i<<" "<<Left[i]<<endl;
  fin.close();
  fout.close();
}

int main() 
{
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t = 1;

  while(t--){
    solve();
  }

  return 0;
}