Pagini recente » Cod sursa (job #612519) | Cod sursa (job #612524) | Cod sursa (job #888921) | Cod sursa (job #1474240) | Cod sursa (job #1652658)
#include <fstream>
#include <vector>
#include <string.h>
#define Ndim 10003
#define pb push_back
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
int N,M,E,L[Ndim],R[Ndim];
bool VIZ[Ndim];
vector <int> G[Ndim];
void read()
{
fin>>N>>M>>E;
int a,b,i;
for(i=1;i<=E;i++)
{
fin>>a>>b;
G[a].pb(b);
}
}
bool pairup(int nod)
{
if(VIZ[nod])
return 0;
VIZ[nod] = 1;
for(vector<int> :: iterator it = G[nod].begin();it!=G[nod].end();++it)
{
if(!R[*it])
{
L[nod] = *it;
R[*it] = nod;
return 1;
}
}
for(vector<int> :: iterator it = G[nod].begin();it!=G[nod].end();++it)
{
if(pairup(R[*it]))
{
L[nod] = *it;
R[*it] = nod;
return 1;
}
}
return 0;
}
void solve()
{
int i,change;
for(change = 1;change;)
{
change = 0;
memset(VIZ,0,sizeof VIZ);
for(i=1;i<=N;i++)
if(!L[i])
change|=pairup(i);
}
}
void print()
{
int sol = 0,i;
for(i=1;i<=N;i++)
sol += (L[i]>0);
fout<<sol<<'\n';
for(i=1;i<=N;i++)
if(L[i] > 0)
fout<<i<<' '<<L[i]<<'\n';
}
int main()
{
read();
solve();
print();
return 0;
}