Pagini recente » Cod sursa (job #2635926) | Cod sursa (job #1448586) | Cod sursa (job #227708) | Cod sursa (job #2553990) | Cod sursa (job #2926449)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
/**
in grafuri bipartite, cuplajul maxim este egal cu acoperirea minima cu noduri (minimum vertex cover).
Din acest rezultat deducem ca acoperirea minima cu noduri si multimea maxima de puncte independente (maximum independent set)
se pot rezolva in timp polinomial in grafuri bipartite
*/
int n, m, e, ans, st[10005], dr[10005];
bool viz[10005];
vector <int> g[10005];
bool Cupleaza(int nod){
if(viz[nod]) return false;
viz[nod] = 1;
for(auto it : g[nod])
if(!dr[it]){
st[nod] = it;
dr[it] = nod;
return true;
}
for(auto it : g[nod])
if(Cupleaza(dr[it])){
st[nod] = it;
dr[it] = nod;
return true;
}
return false;
}
void Solve(){
int gata = 0;
while(!gata){
gata = 1;
for(int i = 1; i <= n; i++)
viz[i] = 0;
for(int i = 1; i <= n; i++)
if(!st[i] && Cupleaza(i)){
ans++;
gata = 0;
}
}
fout << ans << "\n";
for(int i = 1; i <= n; i++)
if(st[i])
fout << i << " " << st[i] << "\n";
fout.close();
}
int main()
{
int x, y;
fin >> n >> m >> e;
for(int i = 1; i <= e; i++){
fin >> x >> y;
g[x].push_back(y);
}
Solve();
return 0;
}