Cod sursa(job #1247237)
Utilizator | Data | 22 octombrie 2014 14:35:20 | |
---|---|---|---|
Problema | Cuplaj maxim in graf bipartit | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 1.98 kb |
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
vector <int> g[10001];
int st[10001], dr[10001], n1, n2;
bool used[10001];
bool cuplaj (int x)
{
int i;
if (used[x]==true) return false;
used[x]=true;
for (i=0; i<g[x].size(); i++)
{
if (dr[g[x][i]]==0)
{
st[x]=g[x][i];
dr[g[x][i]]=x;
return true;
}
int y=dr[g[x][i]];
if (cuplaj(y))
{
st[x]=g[x][i];
dr[g[x][i]]=x;
return true;
}
}
return false;
}
int main()
{
int m, i, x, y, nr=0;
bool ok=true;
freopen("cuplaj.in","r",stdin);
freopen("cuplaj.out","w",stdout);
scanf("%d%d%d",&n1,&n2,&m);
for (i=1; i<=m; i++)
{
scanf("%d%d",&x,&y);
g[x].push_back(y);
}
while (ok==true)
{
memset(used,0,sizeof(used)); ok=false;
for (i=1; i<=n1; i++)
{
if (st[i]==0)
{
if (cuplaj(i))
{
ok=true;
}
}
}
}
for (i=1; i<=n1; i++)
{
if (st[i]!=0) nr++;
}
printf("%d\n",nr);
for (i=1; i<=n1; i++)
{
if (st[i]!=0)
{
printf("%d %d\n",i,st[i]);
}
}
fclose(stdin);
fclose(stdout);
return 0;
}