Pagini recente » Cod sursa (job #3310073) | Borderou de evaluare (job #3327814) | Borderou de evaluare (job #3345021) | Cod sursa (job #3336035) | Cod sursa (job #3352276)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("cuplaj.in") ;
ofstream fout ("cuplaj.out") ;
const int nmx = 10005 ;
int n1 , n2 , m ;
vector < int > g[nmx] ;
int st[nmx] , dr[nmx] ;
vector < bool > sel ;
int drum ( int node )
{
sel[node] = 1 ;
for ( auto it : g[node] )
if ( dr[it] == 0 )
{
st[node] = it ;
dr[it] = node ;
return 1 ;
}
for ( auto it : g[node] )
if ( ! sel[dr[it]] )
{
if ( drum(dr[it] ) )
{
st[node] = it ;
dr[it] = node ;
return 1 ;
}
}
return 0 ;
}
int cuplaj ()
{
int c = 0 ;
int oldc = -1 ;
while ( c != oldc )
{
oldc = c ;
for ( int i = 1 ; i <= n1 ; i ++ )
sel[i] = 0 ;
for ( int i = 1 ; i <= n1 ; i ++ )
{
if ( st[i] == 0 && sel[i] == 0 )
c += drum(i) ;
}
}
return c ;
}
int main()
{
fin >> n1 >> n2 >> m ;
sel.resize(n1+2) ;
for ( int i = 1 ; i <= m ; i ++ )
{
int x , y ;
fin >> x >> y ;
g[x].push_back(y) ;
}
fout << cuplaj() ;
fout << '\n' ;
for ( int i = 1 ; i <= n1 ; i ++ )
{
if ( st[i] != 0 )
fout << i << " " << st[i] << '\n' ;
}
return 0;
}