Pagini recente » Cod sursa (job #552668) | Cod sursa (job #3203247) | Cod sursa (job #2406166) | Cod sursa (job #103058) | Cod sursa (job #2357486)
#include <bits/stdc++.h>
#define dim 10005
using namespace std;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
int Left[dim],Right[dim],cuplaj;
bool viz[dim];
vector <int> graph[dim];
int n,m;
void Read()
{
int edge;
f>>n>>m>>edge;
for(int i=1; i<=edge; ++i)
{
int x,y;
f>>x>>y;
graph[x].push_back(y);
}
}
bool Match(int node)
{
if(viz[node])return false;
viz[node]=1;
for(int i=0; i<graph[node].size(); ++i)
{
int son=graph[node][i];
if(!Right[son])
{
Right[son]=node;
Left[node]=son;
++cuplaj;
return true;
}
}
for(int i=0; i<graph[node].size(); ++i)
{
int son=graph[node][i];
if(Match(Right[son]))
{
Right[son]=node;
Left[node]=son;
return true;
}
}
return false;
}
void Cuplaj()
{
bool okay;
do
{
for(int i=1; i<=n; ++i)
viz[i]=0;
okay=false;
for(int i=1; i<=n; ++i)
if(!Left[i])
okay|=Match(i);
}
while(okay);
}
void Write()
{
g<<cuplaj<<'\n';
for(int i=1; i<=n; ++i)
if(Left[i])
g<<i<<" "<<Left[i]<<'\n';
}
int main()
{
Read();
Cuplaj();
Write();
return 0;
}