Pagini recente » Cod sursa (job #1445444) | Cod sursa (job #1137613) | Cod sursa (job #1859887) | Cod sursa (job #2799932) | Cod sursa (job #754695)
Cod sursa(job #754695)
#include <bitset>
#include <vector>
#include <fstream>
#include <cstdlib>
using namespace std;
const int N_MAX=10011;
bitset<N_MAX> was;
vector<int> G[N_MAX];
int L[N_MAX], R[N_MAX];
inline bool PairUp(int x)
{
if(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))
++countMatch, ok=true;
was&=0;
}while(ok);
out<<countMatch<<"\n";
for(i=1; i <= N; ++i)
if(L[i])
out<<i<<' '<<L[i]<<'\n';
return EXIT_SUCCESS;
}