Pagini recente » Cod sursa (job #2934518) | Cod sursa (job #1151052) | Cod sursa (job #2036181) | Cod sursa (job #1306880) | Cod sursa (job #1879698)
#include <bits/stdc++.h>
using namespace std;
constexpr int maxn = 1e4 + 10;
vector<int> vec[maxn] = {};
int a, b, m, st[maxn] = {}, dr[maxn] = {}, viz[maxn] = {};
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
bool pairup(const int x){
if(viz[x]) return false;
viz[x] = true;
for(auto y : vec[x])
if(!st[y]) return st[y] = x, dr[x] = y;
for(auto y : vec[x])
if(pairup(st[y]))
return st[y] = x, dr[x] = y;
return false; }
void mk_cuplaj(){
for(bool b = true; b; ){
fill(viz, viz + a + 5, 0);
b = false;
for(int i = 1; i <= a; ++i)
if(!viz[i] && !dr[i]) b = (pairup(i) || b); } }
int main(){
f >> a >> b >> m;
for(int i = 0, x, y; i < m; ++i)
f >> x >> y, vec[x].push_back(y);
mk_cuplaj();
g << a - count(dr+1, dr+a+1, 0) << '\n';
for(int i = 1; i <= a; ++i)
if(dr[i]) g << i << ' ' << dr[i] << '\n';
return 0; }