Pagini recente » Cod sursa (job #300452) | Cod sursa (job #2150137) | Cod sursa (job #114905) | Cod sursa (job #2606455) | Cod sursa (job #1425429)
#include <iostream>
#include <deque>
#include <vector>
#include <cstring>
#include <bitset>
#include <algorithm>
#define INF 1000010
#define uint unsigned int
#define ll long long
#define split 10000
using namespace std;
int N, M, E, x, y, i, j, sol, cuplat[20010], viz[20010], ok;
vector<int> V[20010];
int DFS(int X)
{
if(viz[X])
return 0;
viz[X] = 1;
for(vector<int>::iterator it = V[X].begin(); it!=V[X].end(); it++)
{
if(!cuplat[*it])
{
cuplat[*it] = X;
cuplat[X] = *it;
return 1;
}
}
for(vector<int>::iterator it = V[X].begin(); it!=V[X].end(); it++)
{
if(DFS(cuplat[*it]))
{
cuplat[*it] = X;
cuplat[X] = *it;
return 1;
}
}
return 0;
}
int main()
{
freopen("cuplaj.in","r",stdin);
freopen("cuplaj.out","w",stdout);
scanf("%d%d%d", &N, &M, &E);
while(E--)
{
scanf("%d%d", &x, &y);
V[x].push_back(split + y);
V[split+y].push_back(x);
}
ok = 1;
while(ok)
{
ok = 0;
memset(viz, 0, sizeof(viz));
for(i = 1; i <= N; i++)
if(!viz[i] && !cuplat[i])
if(DFS(i))
{
ok = 1;
sol = sol + 1;
}
}
printf("%d\n", sol);
for(i=1;i<=N;i++)
if(cuplat[i])
printf("%d %d\n", i, cuplat[i]-split);
return 0;
}