Pagini recente » Cod sursa (job #2704431) | Cod sursa (job #121958) | Cod sursa (job #1898269) | Cod sursa (job #1239528) | Cod sursa (job #581845)
Cod sursa(job #581845)
#include <fstream>
using namespace std;
const int N=20003;
int cap[N][N],flux[N][N],T[N],v[N],n,m;
bool use[N];
ifstream in("cuplaj.in");
ofstream out("cuplaj.out");
bool bfs(int X,int Y)
{
int st=0,dr=0,x,y;
v[++dr]=X;
for (x=0;x<=Y;x++)
{
use[x]=false;
T[x]=0;
}
use[X]=true;
while (st<dr)
{
x=v[++st];
for (y=1;y<=Y;y++)
if (!use[y] && flux[x][y]<cap[x][y])
{
v[++dr]=y;
T[y]=x;
use[y]=true;
}
}
return use[Y];
}
int max_flow(int X,int Y)
{
int M,i,j,flow=0;
while (bfs(X,Y))
for (i=1;i<Y;i++)
if (flux[i][Y]<cap[i][Y])
{
M=cap[i][Y]-flux[i][Y];
for (j=i;j!=X;j=T[j])
M=min(M,cap[T[j]][j]-flux[T[j]][j]);
if (!M)
continue;
T[Y]=i;
for (j=Y;j!=X;j=T[j])
{
flux[T[j]][j]+=M;
flux[j][T[j]]-=M;
}
flow+=M;
}
return flow;
}
int main()
{
int i,j,k,x,y;
in>>n>>m>>k;m+=n+1;
while (k--)
{
in>>x>>y;
cap[x][y]=1;
}
for (i=1;i<=n;i++)
cap[0][i]=1;
for (i=n+1;i<m;i++)
cap[i][m]=1;
out<<max_flow(0,m)<<"\n";
for (i=1;i<=n;i++)
for (j=n+1;j<m;j++)
if (flux[i][j]==1)
out<<i<<" "<<j<<"\n";
return 0;
}