Pagini recente » Cod sursa (job #114667) | Cod sursa (job #5516) | Cod sursa (job #1082873) | Cod sursa (job #2321281) | Cod sursa (job #2585857)
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 10005;
vector<int> graf[MAXN];
int cuplajst[MAXN], cuplajdr[MAXN];
bool viz[MAXN];
bool cuplaj(int nod){
if(viz[nod]) return 0;
viz[nod] = 1;
for(auto x: graf[nod]){
if(cuplajst[x] == 0){
cuplajdr[nod] = x;
cuplajst[x] = nod;
return 1;
}
}
for(auto x: graf[nod]){
if(cuplaj(cuplajst[x])){
cuplajdr[nod] = x;
cuplajst[x] = nod;
return 1;
}
}
return 0;
}
int main()
{
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
int n, m, e;
fin >> n >> m >> e;
for(int i = 1; i <= e; ++i){
int x, y;
fin >> x >> y;
graf[x].push_back(y);
}
bool done = 0;
while(!done){
memset(viz, 0, n + 1);
done = 1;
for(int i = 1; i <= n; ++i)
if(cuplajdr[i] == 0 && cuplaj(i)) done = 0;
}
int ans = 0;
for(int i = 1; i <= n; ++i)
if(cuplajdr[i] > 0) ans++;
fout << ans << "\n";
for(int i = 1; i <= n; ++i){
if(cuplajdr[i] > 0)
fout << i << " " << cuplajdr[i] << "\n";
}
return 0;
}