Pagini recente » Cod sursa (job #1264165) | Cod sursa (job #2382116) | Cod sursa (job #3154151) | Cod sursa (job #1509807) | Cod sursa (job #1880392)
// Cuplaj O(sqrt(l)*r) - Lanturi alternative
// In stanga am l noduri In dreapta am r noduri
// In totatl m muchii
#include <fstream>
#include <vector>
#include <bitset>
#define Nmax 10009
using namespace std;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
int l,r,m,toleft[Nmax],toright[Nmax],x,y,sol;
vector < int > G[Nmax];
bitset < Nmax > viz;
bool Cupleaza(int node)
{
if(viz[node])
return 0;
viz[node]=1;
for(auto x : G[node])
if(!toleft[x])
{
toright[node]=x;
toleft[x]=node;
return 1;
}
for(auto x : G[node])
if(Cupleaza(toleft[x]))
{
toright[node]=x;
toleft[x]=node;
return 1;
}
return 0;
}
int main()
{
f>>l>>r>>m;
for(int i=1;i<=m;++i)
{
f>>x>>y;
G[x].push_back(y);
}
int gata=0;
while(!gata)
{
gata=1;
for(int i=1;i<=l;++i)viz[i]=0;
for(int i=1;i<=l;++i)
if(!toright[i] && Cupleaza(i))
{
++sol;
gata=0;
}
}
g<<sol<<'\n';
for(int i=1;i<=l;++i)
if(toright[i])g<<i<<' '<<toright[i]<<'\n';
f.close();g.close();
return 0;
}