Pagini recente » Cod sursa (job #1471171) | Cod sursa (job #2578214) | Cod sursa (job #2363473) | Cod sursa (job #1822274) | Cod sursa (job #2113031)
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
class bipart
{
public:
bipart(int n, int m)
{
this->n = n;
this->m = m;
edges.resize(n + 1);
}
void AddEdge(int x, int y)
{
edges[x].push_back(y);
}
void GetCuplaj(vector<pair<int, int> > &ret)
{
vector<int> to(n+1), from(n+1);
vector<bool> viz(n+1);
bool cuplat = true;
while(cuplat)
{
cuplat = false;
fill(viz.begin(), viz.end(), false);
for(int i = 1; i <= n; ++i)
if(to[i] == 0 && cupleaza(i, viz, to, from))
cuplat = true;
}
for(int i = 1; i <= n; ++i)
if(to[i] != 0)
ret.push_back({i, to[i]});
}
private:
bool cupleaza(int nod, vector<bool> &viz, vector<int> &to, vector<int> &from)
{
if(viz[nod])
return false;
viz[nod] = true;
for(auto v:edges[nod])
if(from[v] == 0)
{
to[nod] = v;
from[v] = nod;
return true;
}
for(auto v:edges[nod])
if(cupleaza(from[v], viz, to, from))
{
to[nod] = v;
from[v] = nod;
return true;
}
return false;
}
vector<vector<int> > edges;
int n, m;
};
int main()
{
ifstream in("cuplaj.in");
int n, m, e;
in >> n >> m >> e;
bipart graf(n, m);
int x, y;
while(e--)
{
in >> x >> y;
graf.AddEdge(x, y);
}
in.close();
vector<pair<int, int> > rasp;
graf.GetCuplaj(rasp);
ofstream out("cuplaj.out");
out << rasp.size() << "\n";
for(auto &x:rasp)
out << x.first << " " << x.second << "\n";
out.close();
return 0;
}