//cu matrice de adiacenta
#include<fstream>
#include<cstring>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
struct muchie {int x,y;};
int C[10001][10001];//matricea capacitatilor
int t1,t2,n;//cardinalele si nr total de noduri
int T[10001],X[10001];//vector TATA din BFS, coada din BFS
muchie S[10001];//solutia
int sk;//lungimea solutiei
int BFS() //returneaza 1 daca gaseste drum de crestere de la 0 la n
{
int s=1,d=1,j;
memset(T,0,sizeof(T));
memset(X,0,sizeof(X));
X[1]=0;T[0]=-1;
while(s<=d)
{
for(j=1;j<=n;j++)
if(T[j]==0 && C[X[s]][j]>0)
{
X[++d]=j;
T[j]=X[s];
if(j==n)
return 1;
}
s++;
}
return 0;
}
void cuplaj_maxim()
{
int j;
while(BFS())//cat timp mai gaseste drumuri de crestere
{
j=n;
while(j!=0)//inverseaza arcele
{ C[T[j]][j]=0;
C[j][T[j]]=1;
j=T[j];
}
}
}
int main()
{
int i,x,y,m;
fin>>t1>>t2>>m;
n=t1+t2;
for(i=1;i<=m;++i)
{
fin>>x>>y;
C[x][t1+y]=1;
}
for(i=1;i<=t1;i++) C[0][i]=1;
n++;
for(i=1;i<=t2;i++) C[t1+i][n]=1;
cuplaj_maxim();
for(i=1;i<=n;i++)
if(C[n][i]==1) S[++sk].y=i;
for(i=1;i<=sk;i++)
for(int j=1;j<=n;j++)
if(C[S[i].y][j]) S[i].x=j;
fout<<sk<<endl;
for(i=1;i<=sk;i++) fout<<S[i].x<<" "<<S[i].y-t1<<endl;
return 0;
}
// cu listele de adiacenta
#include<fstream>
#include<cstring>
#include <vector>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
struct muchie {int x,y;};
int t1,t2,n;//cardinalele si nr total de noduri
int T[10001],X[10001];//vector TATA din BFS, coada din BFS
muchie S[10001];//solutia
int sk;//lungimea solutiei
vector<int> V[10001];//listele de adiacenta
int BFS() //returneaza 1 daca gaseste drum de crestere de la 0 la n
{
int s=1,d=1,j;
memset(T,0,sizeof(T));
memset(X,0,sizeof(X));
X[1]=0;T[0]=-1;
while(s<=d)
{
for(int i=0;i<V[X[s]].size();i++)
{
j=V[X[s]][i];
if(T[j]==0)
{
X[++d]=j;
T[j]=X[s];
if(j==n)
return 1;
}
}
s++;
}
return 0;
}
void cuplaj_maxim()
{
int j;
while(BFS())//cat timp mai gaseste drumuri de crestere
{
j=n;
while(j!=0)//inverseaza arcele
{ for(int i=0;i<V[T[j]].size();i++)
if(V[T[j]][i]==j) V[T[j]].erase(V[T[j]].begin()+i);
V[j].push_back(T[j]);
j=T[j];
}
}
}
int main()
{
int i,x,y,m;
fin>>t1>>t2>>m;
n=t1+t2;
for(i=1;i<=t1;i++) V[0].push_back(i);
for(i=1;i<=m;++i)
{
fin>>x>>y;
V[x].push_back(y+t1);
}
n++;
for(i=1;i<=t2;i++) V[t1+i].push_back(n);
cuplaj_maxim();
for(i=0;i<V[n].size();i++)
S[++sk].y=V[n][i];
for(i=1;i<=sk;i++)
for(int j=0;j<V[S[i].y].size();j++)
S[i].x=V[S[i].y][j];
fout<<sk<<endl;
for(i=1;i<=sk;i++) fout<<S[i].x<<" "<<S[i].y-t1<<endl;
return 0;
}
|