Pagini recente » Cod sursa (job #2095758) | Cod sursa (job #2237280) | Cod sursa (job #1596950) | Cod sursa (job #799039) | Cod sursa (job #387043)
Cod sursa(job #387043)
# include <fstream>
using namespace std;
struct nod {
int info;
nod *next;};
nod *g[10003];
int L[10003], R[10003], u[10003], cuplaj, n, m;
void read ()
{
int e;
nod *t;
ifstream fin ("cuplaj.in");
fin>>n>>m>>e;
for (int i=1;i<=e;i++)
{
int i, j;
fin>>i>>j;
t=new nod;
t->info=j;
t->next=g[i];
g[i]=t;
}
}
int pereche (int x)
{
int i;
if (u[x])
return 0;
u[x]=1;
for (nod *p=g[x]; p ;p=p->next)
{
i=p->info;
if (R[i]==0 || pereche(R[i]))
{
L[x]=i;R[i]=x;
return 1;
}
}
return 0;
}
void cupleaza ()
{
int gata=0;
while (!gata)
{
gata=1;
for (int i=1;i<=n;i++)
u[i]=0;
for (int i=1;i<=n;i++)
if (L[i]==0)
if (pereche(i))
{
cuplaj++;
gata=0;
}
}
}
void write ()
{
ofstream fout ("cuplaj.out");
fout<<cuplaj<<endl;
for (int i=1;i<=n;i++)
if (L[i])
fout<<i<<" "<<L[i]<<endl;
}
int main ()
{
read ();
cupleaza ();
write ();
return 0;
}