Pagini recente » Cod sursa (job #1924331) | Profil je_gigi003 | Rating Vlad Stanciu (vladstanciu) | Cod sursa (job #1885979) | Cod sursa (job #781336)
Cod sursa(job #781336)
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
#define Max 10001
vector<int>g[Max];
int n,m,st[Max],dr[Max];
bool was[Max];
bool dfs(int x){
int y;
if( was[x] )return 0; // nu putem cupla aici
was[x] = 1;
for(int i=0;i<g[x].size();i++)
{
y = g[x][i];
if( st[y] == 0 || dfs(st[y]) ) // daca nu-i cuplat sau
{ // ii putem gasi o alta pereche
st[y] = x;
dr[x] = y;
return 1; // am cuplat
}
}
return 0;
}
void cuplaj(){
int nr = 0, step = 1;
while(step)
{
step = 0;
memset(was,0,sizeof(was));
for(int i=1;i<=n;i++)
if( !dr[i] && dfs(i) ) // daca nu are un corespodent
{ // si putem gasi unul
nr++; // am mai cuplat o pereche
step = 1; // mai repetam odata ciclul
}
}
printf("%d\n",nr);
for(int i=1;i<=n;i++)
if(dr[i]) printf("%d %d\n",i,dr[i]);
}
int main(){
int e,x,y;
freopen("cuplaj.in","r",stdin);
freopen("cuplaj.out","w",stdout);
scanf("%d %d %d",&n,&m,&e);
while(e--)
{
scanf("%d %d",&x,&y);
g[x].push_back(y);
}
cuplaj();
return 0;
}