Pagini recente » Cod sursa (job #809543) | Cod sursa (job #653707) | Cod sursa (job #1837394) | Cod sursa (job #2284252) | Cod sursa (job #3152989)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
const int N = 10009;
int n, m, e, u, v, L[N], R[N];
vector<vector<int>> G;
bool viz[N];
bool DoMatch(int x)
{
if(viz[x])return false;
viz[x] = true;
for(auto y : G[x])
if(!R[y])
{
L[x] = y;
R[y] = x;
return true;
}
for(auto y : G[x])
if(DoMatch(R[y]))
{
L[x] = y;
R[y] = x;
return true;
}
return false;
}
int MaxMatching()
{
int max_matching = 0;
for(bool found_match = true; found_match;)
{
found_match = false;
fill(viz + 1, viz + n + 1, false);
for(int i = 1; i <= n; ++i)
if(!L[i] && DoMatch(i))
max_matching += found_match = true;
}
return max_matching;
}
int main()
{
cin >> n >> m >> e;
G = vector<vector<int>>(n + 1);
for(int i = 1; i <= e; ++i)
{
cin >> u >> v;
G[u].push_back(v);
}
cout << MaxMatching() << '\n';
for(int i = 1; i <= n; ++i)
if(L[i])
cout << i << ' ' << L[i] << '\n';
return 0;
}