Pagini recente » Cod sursa (job #3001237) | Cod sursa (job #463494) | Cod sursa (job #2243735) | Cod sursa (job #1112491) | Cod sursa (job #738801)
Cod sursa(job #738801)
#include <bitset>
#include <vector>
#include <fstream>
#include <cstdlib>
using namespace std;
const int N_MAX=10011;
int L[N_MAX], R[N_MAX];
bitset<N_MAX> was;
vector<int> G[N_MAX];
inline bool PairUp(int x)
{
if(true == was.test(x))
return false;
was.set(x);
vector<int>::const_iterator it=G[x].begin(), iend=G[x].end();
for(; it < iend; ++it)
if(!R[*it] || PairUp(R[*it]))
{
L[x]=*it;
R[*it]=x;
return true;
}
return false;
}
int main()
{
bool ok;
int N, M, E, x, y, i, countMatch=0;
ifstream in("cuplaj.in");
ofstream out("cuplaj.out");
for(in>>N>>M>>E; E; --E)
{
in>>x>>y;
G[x].push_back(y);
}
do {
ok=false;
for(i=1; i <= N; ++i)
if(!L[i] && PairUp(i))
ok=true, ++countMatch;
was&=0;
}while(ok);
out<<countMatch<<'\n';
for(i=1; i <= N; ++i)
if(L[i])
out<<i<<' '<<L[i]<<'\n';
return EXIT_SUCCESS;
}