Pagini recente » Cod sursa (job #954786) | Cod sursa (job #626883) | Cod sursa (job #1714033) | Cod sursa (job #763647) | Cod sursa (job #2959182)
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
//ifstream in("maxflow.in");
//ofstream out("maxflow.out");
ifstream in("cuplaj.in");
ofstream out("cuplaj.out");
vector<int> adjList[10005];
vector<int> u(10005);
int l[10005], r[10005];
int pairup(int n){
if (u[n])
return 0;
u[n] = 1;
for(auto el : adjList[n]){
if(!r[el])
{
l[n] = el;
r[el] = n;
return 1;
}
}
for(auto el: adjList[n]){
if(pairup(r[el])){
l[n] = el;
r[el] = n;
return 1;
}
}
return 0;
}
int main(){
int n,m,e;
in>>n>>m>>e;
for(int i = 0; i<e; i++)
{
int x,y;
in>>x>>y;
adjList[x].push_back(y);
}
for(int i = 1; i;) {
i = 0;
fill(u.begin(), u.end(), 0);
for(int j =1; j<=n;j++)
if(!l[j])
i += pairup(j);
}
int mxFlow = 0;
for(int i = 1; i<=n ;i++)
mxFlow += (l[i] > 0);
out<<mxFlow<<endl;
for(int i=1 ; i<=n; i++)
if(l[i] > 0)
out<<i<<" "<<l[i]<<endl;
return 0;
}