Pagini recente » Cod sursa (job #1149927) | Cod sursa (job #839021) | Cod sursa (job #1847149) | Cod sursa (job #1676928) | Cod sursa (job #1938878)
#include <cstdio>
#include <vector>
#include <bitset>
using namespace std;
const int nmx = 10002;
int n,m,l,st[nmx],dr[nmx];
vector <int> g[nmx];
bitset <nmx> viz;
void citire()
{
scanf("%d %d %d", &n,&m, &l);
for(int i = 1; i <= l; ++i)
{
int nod1,nod2;
scanf("%d %d", &nod1, &nod2);
g[nod1].push_back(nod2);
}
}
bool uneste(int nod)
{
if(viz[nod])
return false;
viz[nod] = true;
for(vector<int>::iterator it = g[nod].begin(); it != g[nod].end(); ++it)
if(not st[*it])
{
dr[nod] = *it;
st[*it] = nod;
return true;
}
for(vector<int>::iterator it = g[nod].begin(); it != g[nod].end(); ++it)
if(uneste(st[*it]))
{
dr[nod] = *it;
st[*it] = nod;
return true;
}
return false;
}
void cuplare()
{
bool ok = 1;
while(ok)
{
ok = 0;
viz.reset();
for(int i = 1; i <= n; ++i)
if(not dr[i])
ok |= uneste(i);
}
}
void afish()
{
int total = 0;
for(int i = 1; i <= n; ++i)
if(dr[i])
++ total;
printf("%d\n", total);
for(int i = 1; i <= n; ++i)
if(dr[i])
printf("%d %d\n", i, dr[i]);
}
int main()
{
freopen("cuplaj.in", "r", stdin);
freopen("cuplaj.out", "w", stdout);
citire();
cuplare();
afish();
return 0;
}