Pagini recente » Cod sursa (job #3266200) | Cod sursa (job #2984806) | Cod sursa (job #1395756) | Cod sursa (job #2580308) | Cod sursa (job #2807486)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
const int nmax = 1e4 + 5;
int n, m, e, l[nmax], r[nmax];
bool vis[nmax];
vector <int> v[nmax];
void read(){
fin >> n >> m >> e;
for(int i = 1; i <= e; i++){
int x, y;
fin >> x >> y;
v[x].push_back(y);
}
}
bool mk_pair(int x){
if(vis[x])
return false;
vis[x] = true;
for(auto y:v[x])
if(!l[y]){
l[y] = x;
r[x] = y;
return true;
}
for(auto y:v[x])
if(mk_pair(l[y])){
l[y] = x;
r[x] = y;
return true;
}
return false;
}
void solve(){
bool ok = false;
int cnt = 0;
do {
ok = false;
memset(vis, 0, sizeof(vis));
for(int i = 1; i <= n; i++)
if(!r[i] && mk_pair(i)){
ok = true;
cnt++;
}
} while(ok);
fout << cnt << "\n";
for(int i = 1; i <= n; i++)
if(r[i])
fout << i << " " << r[i] << "\n";
}
int main()
{
read();
solve();
return 0;
}