Pagini recente » Cod sursa (job #2891358) | Cod sursa (job #1239404) | Cod sursa (job #2067319) | Cod sursa (job #3241855) | Cod sursa (job #1907480)
#include <fstream>
#include <vector>
#include <cstring>
#define nmax 10005
using namespace std;
vector <unsigned int> a[nmax];
unsigned int l[nmax],r[nmax];
bool viz[nmax];
bool cuplare(unsigned int x)
{
if (viz[x]) return 0;
viz[x]=1;
unsigned int i,y;
for (i=0;i<a[x].size();i++)
{
y=a[x][i];
if ((!r[y])||cuplare(r[y]))
{
l[x]=y;
r[y]=x;
return 1;
}
}
return 0;
}
int main()
{
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
unsigned int i,x,n,m,e,y,cuplaj=0,ok=1;
f>>n>>m>>e;
for (i=1;i<=e;i++)
{
f>>x>>y;
a[x].push_back(y);
}
while (ok)
{
ok=0;
memset(viz,0,sizeof(viz));
for (i=1;i<=n;i++)
{
//fiecare nod din L
if (!l[i])
{
ok= ok | (cuplare(i));
}
}
}
for (i=1,cuplaj=1;i<=n;i++,cuplaj+=(l[i]>0));
g<<cuplaj<<'\n';
for (i=1;i<=n;i++)
{
if (l[i]) g<<i<<' '<<l[i]<<'\n';
}
f.close();
g.close();
return 0;
}