Pagini recente » Cod sursa (job #1075925) | Cod sursa (job #131592) | Cod sursa (job #78819) | Cod sursa (job #1534570) | Cod sursa (job #959970)
Cod sursa(job #959970)
#include<fstream>
#include<cstring>
#include<vector>
#include<queue>
using namespace std ;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
#define maxn 10005
int N, M, E ;
vector< vector<int> > graf ;
int solution;
int cuplaj[ 2 * maxn + 1 ], tata[ 2 * maxn + 1 ] ;
int bfs(int nod)
{
queue<int> coada ;
coada.push(nod) ;
tata[nod] = -1 ;
while( coada.empty() == false )
{
int act = coada.front() ;
coada.pop() ;
if( act <= N )
{
for(size_t i = 0; i < graf[act].size(); ++i )
{
int vecin = graf[act][i] ;
if( tata[vecin] == 0 )
{
tata[vecin] = act ;
if( cuplaj[vecin] == 0 )
return vecin ;
coada.push( vecin ) ;
}
}
}
else
{
tata[ cuplaj[act] ] = act ;
coada.push( cuplaj[act] ) ;
}
}
return 0 ;
}
void cupleaza(int nod)
{
bool ok = true ;
while( tata[nod] > 0 )
{
if( ok == true )
{
cuplaj[nod] = tata[nod] ;
cuplaj[ tata[nod] ] = nod ;
}
ok = !ok ;
nod = tata[nod] ;
}
}
int main()
{
fin >> N >> M >> E ;
graf.resize( N + M + 1 ) ;
for(int i = 0; i < E; ++i )
{
int x, y ;
fin >> x >> y ;
graf[x].push_back( y + N ) ;
graf[ y + N ].push_back(x) ;
}
int nr = 0 ;
for(int i = 1; i <= N; ++i )
{
for(size_t j = 0; j < graf[i].size(); ++i )
{
int vecin = graf[i][j] ;
if( cuplaj[vecin] == 0 )
{
cuplaj[i] = vecin ;
cuplaj[vecin] = i ;
++nr ;
break ;
}
}
}
while( 1 )
{
bool ok = false ;
memset( tata, 0, sizeof(tata) ) ;
for(int i = 1; i <= N; ++i )
{
if( cuplaj[i] == 0 )
{
int act = bfs(i) ;
if( act )
{
ok = true ;
cupleaza(act) ;
++nr ;
}
}
}
if( ok == false )
break ;
}
fout << nr << "\n" ;
for(int i = 1; i <= N; ++i )
if( cuplaj[i] )
fout << i << " " << cuplaj[i] - N << "\n" ;
return 0 ;
}