Pagini recente » Cod sursa (job #2496928) | Cod sursa (job #2103610) | Cod sursa (job #132159) | Cod sursa (job #1867365) | Cod sursa (job #2192116)
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
#define VMAX 100002
using namespace std;
ifstream f("cuplaj.in");
ofstream o("cuplaj.out");
int n,m,e,total;
int dr[VMAX],st[VMAX];
vector <int> g[VMAX];
bitset <VMAX> viz;
void citire()
{
f >> n >> m >> e;
int x,y;
for(int i = 1; i <= e; ++i)
{
f >> x >> y;
g[x].push_back(y);
}
}
bool pair_up(int nod)
{
if(nod && viz[nod])
return false;
viz[nod] = true;
for(auto i: g[nod])
if(st[i] == 0)
{
dr[nod] = i;
st[i] = nod;
viz[nod] = false;
return true;
}
else if(pair_up(st[i]))
{
dr[nod] = i;
st[i] = nod;
viz[nod] = false;
return true;
}
return false;
}
void cuplare()
{
bool ok = true;
while(ok)
{
ok = false;
for(int i = 1; i <= n; ++i)
if(pair_up(i))
{
ok = true;
++ total;
}
}
}
void output()
{
o << total << '\n';
for(int i = 1; i <= n; ++i)
if(dr[i])
o << i << ' ' << dr[i] << '\n';
}
int main()
{
citire();
cuplare();
output();
return 0;
}