Pagini recente » Cod sursa (job #307073) | Cod sursa (job #685186) | Cod sursa (job #36099) | Cod sursa (job #1011234) | Cod sursa (job #1401821)
#include <fstream>
#include <vector>
#include <cstring>
#define DIM 10005
using namespace std;
ifstream in("cuplaj.in");
ofstream out("cuplaj.out");
vector <int> node[DIM];
int l_match[DIM], r_match[DIM], vis[DIM], l, r, edges, sol;
bool cupleaza(int vertex){
int neigh;
if(vis[vertex]) return false;
vis[vertex]=1;
for(int i=0; i<node[vertex].size(); ++i){
neigh=node[vertex][i];
if(!r_match[neigh]){
l_match[vertex]=neigh;
r_match[neigh]=vertex;
return true;
}
}
for(int i=0; i<node[vertex].size(); ++i){
if(cupleaza(r_match[node[vertex][i]])){
neigh=node[vertex][i];
l_match[vertex]=neigh;
r_match[neigh]=vertex;
return true;
}
}
return false;
}
int main()
{
int a, b;
short temp=1;
in>>l>>r>>edges;
while(edges--){
in>>a>>b;
node[a].push_back(b);
}
while(temp){
memset(vis, 0, sizeof(vis));
temp=0;
for(int i=1; i<=l; ++i){
if(!l_match[i]&&cupleaza(i)){
sol++; temp=1;
}
}
}
out<<sol<<"\n";
for(int i=1; i<=l; ++i){
if(l_match[i])
out<<i<<" "<<l_match[i]<<"\n";
}
return 0;
}