Pagini recente » Cod sursa (job #1417555) | Cod sursa (job #373051) | Cod sursa (job #2609062) | Cod sursa (job #917443) | Cod sursa (job #2328561)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("cuplaj.in");
ofstream fout("cuplaj.out");
const int VAL = 10005;
/// Complexity : O(sqrt(Nodes) * Edges)
int N, M, E, i, j;
int A, B, ANS;
int Left[VAL], Right[VAL];
vector <int> G[VAL], SOL;
bool gas, tried[VAL];
bool PairUp(int nod)
{
if (tried[nod]==true)
return false;
tried[nod]=true;
vector <int> :: iterator it;
for (it=G[nod].begin(); it!=G[nod].end(); it++)
{
if (Left[*it]==0)
{
Left[*it]=nod;
Right[nod]=*it;
return true;
}
}
for (it=G[nod].begin(); it!=G[nod].end(); it++)
{
if (PairUp(Left[*it])==true)
{
Left[*it]=nod;
Right[nod]=*it;
return true;
}
}
return false;
}
int main()
{
fin >> N >> M >> E;
for (i=1; i<=E; i++)
{
fin >> A >> B;
G[A].push_back(B);
}
while (1)
{
gas=false;
for (i=1; i<=N; i++)
tried[i]=false;
for (i=1; i<=N; i++)
if (Right[i]==0 && PairUp(i)==true)
gas=true;
if (gas==false)
break;
}
for (i=1; i<=N; i++)
if (Right[i]>0)
SOL.push_back(i);
fout << SOL.size() << '\n';
for (i=0; i<SOL.size(); i++)
fout << SOL[i] << " " << Right[SOL[i]] << '\n';
fin.close();
fout.close();
return 0;
}