Pagini recente » Cod sursa (job #2866779) | Cod sursa (job #1040345) | Cod sursa (job #1830083) | Cod sursa (job #1997853) | Cod sursa (job #2661760)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
const int nmax = 20005, inf = 1e9;
int n, m, e, match[nmax], cuplaj, dist[nmax];
vector <int> G[nmax];
bool bfs(){
queue <int> coada;
for (int i = 1; i <= n; ++i){
dist[i] = inf;
if (match[i] == 0){
coada.push(i);
dist[i] = 0;
}
}
dist[0] = inf;
while (!coada.empty()){
int nod = coada.front();
coada.pop();
if (nod != 0){
for (auto it : G[nod]){
if (dist[match[it]] == inf){
dist[match[it]] = 1 + dist[nod];
coada.push(match[it]);
}
}
}
}
return dist[0] != inf;
}
bool dfs(int nod){
if (nod){
for (auto it : G[nod]){
if (1 + dist[nod] == dist[match[it]] && dfs(match[it])){
match[nod] = it;
match[it] = nod;
return true;
}
}
return false;
}
return true;
}
int main(){
fin >> n >> m >> e;
for (int i = 1; i <= e; ++i){
int x, y;
fin >> x >> y;
G[x].push_back(y + n);
G[y + n].push_back(x);
}
while (bfs()){
for (int i = 1; i <= n; ++i){
if (match[i] == 0 && dfs(i)){
++cuplaj;
}
}
}
fout << cuplaj << "\n";
for (int i = 1; i <= n; ++i){
if (match[i])
fout << i << " " << match[i] - n << "\n";
}
fin.close();
fout.close();
return 0;
}