Pagini recente » Cod sursa (job #1383599) | Cod sursa (job #2970545) | Cod sursa (job #1128076) | Cod sursa (job #707950) | Cod sursa (job #3152992)
#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;
bitset<N> viz;
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;
}
void MaxMatching()
{
int max_matching = 0;
for(bool found_match = true; found_match;)
{
found_match = false;
viz.reset();
for(int i = 1; i <= n; ++i)
if(!L[i]) found_match |= DoMatch(i);
}
}
int main()
{
fin >> n >> m >> e;
G = vector<vector<int>>(n + 1);
for(int i = 1; i <= e; ++i)
{
cin >> u >> v;
G[u].push_back(v);
}
MaxMatching();
int max_matching = 0;
for(int i = 1; i <= n; ++i)
if(L[i])max_matching ++;
fout << max_matching << '\n';
for(int i = 1; i <= n; ++i)
if(L[i])
fout << i << ' ' << L[i] << '\n';
return 0;
}