Pagini recente » Cod sursa (job #1904660) | Cod sursa (job #2395859) | Cod sursa (job #1958474) | Cod sursa (job #2178601) | Cod sursa (job #1968604)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
const int Nmax=10010;
vector<int>G[Nmax];
int st[Nmax],dr[Nmax];
bitset<Nmax>viz;
bool cupleaza(int nod)
{
if(viz[nod]==1) return 0;
viz[nod]=1;
for(vector<int>::iterator it=G[nod].begin();it!=G[nod].end();it++)
{
if(st[*it]==0)
{
st[*it]=nod;
dr[nod]=*it;
return 1;
}
}
for(vector<int>::iterator it=G[nod].begin();it!=G[nod].end();it++)
{
if(cupleaza(st[*it])==1)
{
st[*it]=nod;
dr[nod]=*it;
return 1;
}
}
return 0;
}
int main()
{
int n,m,e,i,x,y,cuplaj=0;
fin>>n>>m>>e;
for(i=1;i<=e;i++)
{
fin>>x>>y;
G[x].push_back(y);
}
bool change=1;
while(change)
{
change=0;
viz.reset();
for(i=1;i<=n;i++)
{
if(dr[i]==0) change|=cupleaza(i);
}
}
for(i=1;i<=n;i++)
{
if(dr[i]) cuplaj++;
}
fout<<cuplaj<<"\n";
for(i=1;i<=n;i++)
{
if(dr[i]) fout<<i<<" "<<dr[i]<<"\n";
}
}