Pagini recente » Cod sursa (job #2398722) | Cod sursa (job #1537320) | Cod sursa (job #2645699) | Cod sursa (job #1935709) | Cod sursa (job #2585851)
#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 notdone = 1;
while(notdone){
memset(viz, 0, n + 1);
notdone = 0;
for(int i = 1; i <= n; ++i)
if(!cuplajdr[i]) notdone |= cuplaj(i);
}
int ans = 0;
for(int i = 1; i <= n; ++i)
if(cuplajdr[i]) ans++;
fout << ans << "\n";
for(int i = 1; i <= n; ++i){
if(cuplajdr[i])
fout << i << " " << cuplajdr[i] << "\n";
}
return 0;
}