Pagini recente » Cod sursa (job #1326605) | Cod sursa (job #1505400) | Cod sursa (job #1402108) | Cod sursa (job #2082475) | Cod sursa (job #2596659)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
int n, m, e, cnt, sol;
vector<int> graph[10005];
int toLft[10005], toRgt[10005];
bitset<10005> check;
bool tryCouple(int node);
int main()
{
fin >> n >> m >> e;
while(e--){
int x, y;
fin >> x >> y;
graph[x].push_back(y);
}
do{
cnt = 0;
for(int i = 1; i <= n; ++i) check[i] = false;
for(int i = 1; i <= n; ++i) {
if(!toRgt[i]){
if(tryCouple(i)) ++cnt;
}
}
sol += cnt;
}while(cnt);
fout << sol << "\n";
for(int i = 1; i <= n; ++i){
if(toRgt[i]){
fout << i << " " << toRgt[i] << "\n";
}
}
return 0;
}
bool tryCouple(int node){
check[node] = true;
int avoid = toRgt[node];
for(auto next:graph[node]){
if(next != avoid && !toLft[next]){
toLft[next] = node;
toRgt[node] = next;
return true;
}
}
for(auto next:graph[node]){
if(next != avoid){
if(check[toLft[next]]) continue;
if(tryCouple(toLft[next])){
toLft[next] = node;
toRgt[node] = next;
return true;
}
}
}
return false;
}