Pagini recente » Diferente pentru blog/carti intre reviziile 58 si 59 | Istoria paginii utilizator/redshadow | Monitorul de evaluare | Concursuri Virtuale | Cod sursa (job #1004076)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
const int nmax= 10000, mmax= 10000;
vector <int> g[nmax+mmax+1];
int p[nmax+mmax+1];
bool up[nmax+1];
bool pair_up( int x ) {
if ( up[x]==1 ) {
return 0;
}
up[x]= 1;
for ( vector <int>::iterator it= g[x].begin(); it!=g[x].end(); ++it ) {
if ( p[*it]==0 ) {
p[x]= *it; p[*it]= x;
return 1;
}
}
for ( vector <int>::iterator it= g[x].begin(); it!=g[x].end(); ++it ) {
if ( pair_up(p[*it])==1 ) {
p[x]= *it; p[*it]= x;
return 1;
}
}
return 0;
}
int main( ) {
int n, m, e;
fin>>n>>m>>e;
for ( int i= 1; i<=e; ++i ) {
int x, y;
fin>>x>>y;
g[x].push_back(y+n);
g[y+n].push_back(x);
}
int mm= 0, mods= 1;
while ( mods>0 ) {
mods= 0;
for ( int i= 1; i<=n; ++i ) {
up[i]= 0;
}
for ( int i= 1; i<=n; ++i ) {
if ( p[i]==0 ) {
mods+= pair_up(i);
}
}
mm+= mods;
}
fout<<mm<<"\n";
for ( int i= 1; i<=n; ++i ) {
if ( p[i]!=0 ) {
fout<<i<<" "<<p[i]-n<<"\n";
}
}
return 0;
}