Pagini recente » Cod sursa (job #2710832) | Cod sursa (job #334761) | Cod sursa (job #772195) | Cod sursa (job #1347873) | Cod sursa (job #2854635)
#include <fstream>
#include<vector>
using namespace std;
const int NMAX = 10001;
ifstream f("cuplaj.in");
ofstream g("cuplaj.out");
vector<int> G[NMAX];
int N, M, E,
L[NMAX], R[NMAX],
maxMatch;
bool viz[NMAX];
void citire()
{
int x, y;
f >> N >> M >> E;
while(E--)
{
f >> x >> y;
G[x].push_back(y);
}
}
bool DFS(int x)
{
if(viz[x]) return 0;
viz[x] = 1;
for(auto &y : G[x])
if(!R[y] || DFS(R[y]))
{
L[x] = y;
R[y] = x;
return 1;
}
return 0;
}
void HK() ///Hopcroft-Karp
{
bool wasChanged = 1;
int i;
while(wasChanged)
{
wasChanged = 0;
for(i = 1; i <= N; ++i)
viz[i] = 0;
for(i = 1; i <= N; ++i)
{
if(!L[i] && DFS(i))
{
wasChanged = 1;
maxMatch++;
}
}
}
}
int main()
{
citire();
HK();
g << maxMatch << '\n';
for(int i = 1; i <= N; i++)
{
if(L[i]) g << i << ' ' << L[i] << '\n';
}
f.close();
g.close();
return 0;
}