Pagini recente » Cod sursa (job #1747688) | Cod sursa (job #2226913) | Cod sursa (job #2020280) | Rating Dediu Matei (Dediu_Matei_324CA) | Cod sursa (job #1757289)
#include <fstream>
#include <algorithm>
#include <vector>
#include <bitset>
using namespace std;
#define Nmax 10002
ifstream fin( "cuplaj.in" );
ofstream fout( "cuplaj.out" );
int N, M;
int Left_to_Right[Nmax], Right_to_Left[Nmax];
vector < int > G[Nmax];
bitset < Nmax > Used;
bool PairUP( int node ){
if( Used[node] )
return 0;
vector < int > :: iterator it;
Used[node] = 1;
for( it = G[node].begin(); it != G[node].end(); ++it )
if( !Right_to_Left[*it] ){
Left_to_Right[node] = *it;
Right_to_Left[*it] = node;
return 1;
}
for( it = G[node].begin(); it != G[node].end(); ++it )
if( PairUP( Right_to_Left[*it] ) ){
Left_to_Right[node] = *it;
Right_to_Left[*it] = node;
return 1;
}
return 0;
}
int main(){
int N, M, E;
fin >> N >> M >> E;
int x, y;
while( E-- ){
fin >> x >> y;
G[x].push_back(y);
}
int Changes = 1;
while( Changes ){
Changes = 0;
Used.reset();
for( int i = 1; i <= N; ++i )
if( !Left_to_Right[i] )
Changes += PairUP(i);
}
int Matches = 0;
for( int i = 1; i <= N; ++i )
if( Left_to_Right[i] )
Matches++;
fout << Matches << "\n";
for( int i = 1; i <= N; ++i )
if( Left_to_Right[i] )
fout << i << " " << Left_to_Right[i] << "\n";
return 0;
}