Pagini recente » Cod sursa (job #1243526) | Cod sursa (job #2349770) | Cod sursa (job #1220481) | Cod sursa (job #1871085) | Cod sursa (job #3042206)
#include<bits/stdc++.h>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
int n,m,e;
vector<int> con[10005];
int tori[10005];
int tole[10005];
bool vis[10005];
bool change=0;
bool cupleaza(int x)
{
if(vis[x])
return 0;
vis[x]=1;
for(auto adj:con[x])
{
if(tole[adj]==0)
{
tori[x]=adj;
tole[adj]=x;
change=1;
return 1;
}
}
for(auto adj:con[x])
{
if(cupleaza(tole[adj]))
{
tori[x]=adj;
tole[adj]=x;
change=1;
return 1;
}
}
return 0;
}
void cuplaj()
{
while(1)
{
change=0;
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++)
if(tori[i]==0)
cupleaza(i);
if(change==0)
break;
}
int cnt=0;
for(int i=1;i<=n;i++)
if(tori[i]!=0)
cnt++;
fout<<cnt<<"\n";
for(int i=1;i<=n;i++)
if(tori[i]!=0)
fout<<i<<" "<<tori[i]<<"\n";
}
signed main()
{
fin>>n>>m>>e;
int x,y;
for(int i=0;i<e;i++)
{
fin>>x>>y;
con[x].push_back(y);
}
cuplaj();
return 0;
}