Pagini recente » Statistici Bianca Rotariu (BiancaRotariu) | Cod sursa (job #202992) | Istoria paginii utilizator/vali.lazar | Cod sursa (job #1796767) | Cod sursa (job #938049)
Cod sursa(job #938049)
#include <fstream>
using namespace std;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
#define inf 1<<30
#define LE 10066
#include <vector>
#include <map>
#define pb push_back
#define mp make_pair
#define sh short int
vector<sh> H[LE*2+666];
sh n,i,fmin,e,xx,yy,node,maxn;
sh viz[LE],father[LE],m,result,cap;
map<pair<sh,sh>,sh >flux;
bool okz=true;
int color;
void dfs(sh nod)
{
if (okz==true)
return;
if (nod==n)
{
okz=true;
return;
}
viz[nod]=color;
sh N=H[nod].size();
for(sh i=0; i<N; ++i)
{
if (nod>maxn&&H[nod][i]&&H[nod][i]<=maxn)
cap=0;
else
cap=1;
if(viz[H[nod][i]]!=color&&cap>flux[mp(nod,H[nod][i])])
{
father[H[nod][i]]=nod;
dfs(H[nod][i]);
}
}
}
int main()
{
f>>n>>m>>e;
maxn=n;
for(i=1; i<=e; ++i)
{
f>>xx>>yy;
H[xx].pb(yy+n);
H[yy+n].pb(xx);
flux[mp(xx,yy)]=0;
flux[mp(yy,xx)]=0;
}
for(i=1; i<=n+m; ++i)
{
if (i<=n)
{
H[0].pb(i);
flux[mp(0,i)]=0;
flux[mp(i,0)]=0;
}
else
{
H[i].pb(n+m+1);
flux[mp(i,n+m+1)]=0;
flux[mp(n+m+1,i)]=0;
}
}
n+=(m+1);
while (okz)
{
okz=false;
fmin=0;
++color;
dfs(0);
if (okz==false)
break;
node=n;
while(node)
{
flux[mp(father[node],node)]++;
flux[mp(node,father[node])]--;
node=father[node];
}
++result;
}
g<<result<<'\n';
for(i=1; i<=maxn; ++i)
{
int N=H[i].size();
for(int j=0; j<N; ++j)
if (i<H[i][j])
if (flux[mp(i,H[i][j])]>0)
g<<i<<" "<<H[i][j]-maxn<<'\n';
}
f.close();
g.close();
return 0;
}