Pagini recente » Cod sursa (job #420866) | Cod sursa (job #1554066) | Diferente pentru teorema-chineza-a-resturilor intre reviziile 89 si 34 | Cod sursa (job #1466044) | Cod sursa (job #1972893)
#include <fstream>
#include <vector>
#include <cstring>
using namespace std;
ifstream in("cuplaj.in");
ofstream out("cuplaj.out");
const int NMAX = 10000;
int st[NMAX+2], dr[NMAX+2], Ans = 0;
bool viz[NMAX+2];
vector <int> G[NMAX+2];
bool cuplaj( int nod ) {
if( viz[nod] ) return 0;
viz[nod] = 1;
for( auto x : G[nod] ) {
if( !dr[x] ) {
st[nod] = x;
dr[x] = nod;
++Ans;
return 1;
}
}
for( auto x : G[nod] ) {
if( dr[x] && cuplaj( dr[x] ) ) {
st[nod] = x;
dr[x] = nod;
return 1;
}
}
return 0;
}
int main()
{
int N, M, E;
in >> N >> M >> E;
for( int i = 1; i <= E; ++i ) {
int x,y;
in >> x >> y;
G[x].push_back( y );
}
bool ok = 1;
while( ok ) {
ok = 0;
memset( viz, 0, sizeof(viz) );
for( int i = 1; i <= N; ++i ) {
if( !st[i] && cuplaj(i) ) {
ok = 1;
}
}
}
out << Ans << '\n';
for( int i = 1; i <= N; ++i ) {
if( st[i] ) out << i << ' ' << st[i] << '\n';
}
return 0;
}