Pagini recente » Cod sursa (job #241331) | Profil StarGold2 | Cod sursa (job #362969) | Diferente pentru home intre reviziile 413 si 412 | Cod sursa (job #2030751)
#include <iostream>
#include <fstream>
#include <string.h>
#include <vector>
using namespace std;
int N, M, E;
int l [10001], r[10001];
bool vizitat [10001];
//bool term = false;
vector <int> G[10001];
int link (int x)
{
//cout<<"LINKUI NODUL "<<x<<"\n";
//if (term)
//std::terminate();
//if(x==3)
// term = true;
//if(x==3)
//std::terminate();
if(vizitat[x])
return 0;
vizitat[x] = 1;
//for(auto i = G[x].begin(); i < G[x].end(); ++i)
// cout<<r[*i]<<" ";
//cout<<"\n";
for(auto i = G[x].begin(); i < G[x].end(); ++i)
if(!r[*i])
{
//cout<<"caut nod liber in stanga... \n";
l[x] = *i;
r[*i] = x;
return 1;
}
for(auto i = G[x].begin(); i < G[x].end(); ++i) ///dc nu am gasit nod liber in dreapta incercam sa reconectam unul
if(link (r[*i]))
{
l[x] = *i;
r[*i] = x;
return 1;
}
return false;
}
int main()
{
ifstream in ("cuplaj.in");
ofstream out ("cuplaj.out");
int x, y, sol = 0;
in >> N >> M >> E;
for(int i = 0; i < E; ++i)
{
in >> x >> y;
G[x].push_back(y); //liste de adiacenta pt ca da
}
bool ok = false;
//cout<<"INCEP REZOLVAREA!\n";
while(!ok)
{
ok = true;
memset(vizitat, 0, 10001);
for (int i = 1; i <= N; ++i)
if(!l[i])
if(link(i))
ok = false;
}
//cout<<"AFISAREA:\n";
for(int i = 1; i <= N; ++i)
if (l[i])
++sol;
out<<sol<<"\n";
for(int i = 1; i <= N; ++i)
if(l[i])
out<<i<<" "<<l[i]<<"\n";
return 0;
}