Pagini recente » Cod sursa (job #682886) | Cod sursa (job #449377) | Cod sursa (job #653394) | Cod sursa (job #674075) | Cod sursa (job #1677060)
#include <iostream>
#include <vector>
#include <fstream>
#define nmax 10001
using namespace std;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
vector <int> G[nmax];
vector <bool> Use(nmax);
int L[nmax],R[nmax];
int cuplaj(int nod){
if(Use[nod]){
return 0;
}
Use[nod]=1;
for(int i=0;i<G[nod].size();i++){
int nodul=G[nod][i];
if(!R[nodul]){
R[nodul]=nod;
L[nod]=nodul;
return 1;
}
}
for(int i=0;i<G[nod].size();i++){
int nodul=G[nod][i];
if(cuplaj(R[nodul])){
R[nodul]=nod;
L[nod]=nodul;
return 1;
}
}
return 0;
}
int main()
{
int n,m,k;
f>>n>>m>>k;
for(int i=0;i<k;i++){
int x,y;
f>>x>>y;
G[x].push_back(y);
}
int ok=0,nr=0;
do{
ok=0;
Use=vector<bool> (nmax,false);
for(int i=1;i<=n;i++){
if(L[i]==0){
if(cuplaj(i)){
ok=1;
nr++;
}
}
}
}while(ok);
g<<nr<<"\n";
for(int i=1;i<=n;i++){
if(L[i]){
g<<i<<" "<<L[i]<<"\n";
}
}
return 0;
}