Pagini recente » Cod sursa (job #1605061) | Cod sursa (job #637299) | Cod sursa (job #414547) | Cod sursa (job #265089) | Cod sursa (job #240856)
Cod sursa(job #240856)
#include <fstream>
#include <iostream>
#include <queue>
using namespace std;
struct nod
{
nod *urm, *coresp;
int nd,ct,rz;
} *li[20005],*t[20005];
int viz[20001],flx,n,m,s,d;
ofstream g("cuplaj.out");
void adauga(int x,int y)
{
nod *q=new nod;
nod *t=new nod;
q->ct=1;
t->ct=0;
q->nd=y;
t->nd=x;
q->coresp=t;
t->coresp=q;
q->urm=li[x];
li[x]=q;
t->urm=li[y];
li[y]=t;
}
void citire()
{
ifstream f("cuplaj.in");
int e;
f>>n>>m>>e;
s=0;
d=n+m+1;
for(int i=0;i<e;i++)
{
int a,b;
f>>a>>b;
adauga(a,b+n);
}
for(int i=1;i<=n;i++)
adauga(s,i);
for(int i=n+1;i<=n+m;i++)
adauga(i,d);
}
int flux()
{
memset(viz,0,sizeof(viz));
t[s]=0;
viz[s]=1;
queue<int> q;
q.push(s);
while(!q.empty())
{
int vf=q.front();
q.pop();
for(nod *v=li[vf];v;v=v->urm)
if(!viz[v->nd] && v->ct >0)
{
viz[v->nd]=1;
q.push(v->nd);
t[v->nd]=v;
if(v->nd == d)
{
for(nod *v2=t[d];v2;v2=t[v2->coresp->nd])
{
v2->ct--;
v2->coresp->ct++;
}
return 1;
}
}
}
return 0;
/*
queue<int> q;
q.push(s);
t[s]=-1;
viz[s]=1;
while(1)
{
int ok=0;
while(!q.empty())
{
int vf=q.front();
q.pop();
for(int i = 0;i <= n+m+1;i ++)
if(c[vf][i] != 0 && viz[i] == 0)
{
viz[i] = 1;
t[i] = vf;
q.push(i);
if(i == d)
{
ok=1;
break;
}
}
if(ok)
break;
}
if(ok == 0)
return;
while(!q.empty())
q.pop();
memset(viz,0,sizeof(viz));
int drum=65000;
for(int i = d; t[i] != -1; i = t[i])
if(c[t[i]][i] < drum)
drum = c[t[i]][i];
if(drum == 65000)
return;
viz[s] = 1;
q.push(s);
for(int i = d; t[i] != -1; i = t[i])
{
c[t[i]][i] -= drum;
c[i][t[i]] += drum;
}
memset(t,0,sizeof(t));
t[s] = -1;
flx += drum;
}
*/
}
void cuplaje()
{
for(int i=1;i<=n;i++)
for(nod *v=li[i];v;v=v->urm)
if(v->coresp->ct==1 && v->nd>n)
{
g<<i<<" "<<v->nd - n <<endl;
break;
}
}
int main()
{
citire();
while (flux())
flx++;
g<<flx<<endl;
cuplaje();
return 0;
}