Pagini recente » Cod sursa (job #760564) | Cod sursa (job #1458068) | Cod sursa (job #2068353) | Cod sursa (job #1950180) | Cod sursa (job #625217)
Cod sursa(job #625217)
#include <fstream>
#include <iostream>
#include <vector>
#include <cstring>
#define MAXN 10101
#define NOT_EXPLORED 0x3f3f3f3f
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
using namespace std;
typedef unsigned int uint32;
typedef vector<uint32> Graph;
Graph graph[MAXN];
uint32 usedEdges[MAXN];
uint32 partLeft[MAXN];
uint32 partRight[MAXN];
int MaximumMatching
(
const Graph graph[],
const uint32 node
)
{
if (usedEdges[node])
return FALSE;
usedEdges[node] = 1;
for (Graph::const_iterator it=graph[node].begin(); it!=graph[node].end(); ++it)
{
if (partRight[*it] == NOT_EXPLORED)
{
partLeft[node] = *it;
partRight[*it] = node;
return TRUE;
}
}
for (Graph::const_iterator it=graph[node].begin(); it!=graph[node].end(); ++it)
{
if (MaximumMatching(graph, partRight[*it]))
{
partLeft[node] = *it;
partRight[*it] = node;
return TRUE;
}
}
return FALSE;
}
int main()
{
int n,m,e;
fstream fin("cuplaj.in", fstream::in);
fstream fout("cuplaj.out", fstream::out);
fin >> n >> m >> e;
//cout << n << " " << m << " " << e << endl;
for (int i=0; i<e; ++i)
{
int x,y;
fin >> x >> y;
//cout << x-1 << " " << y-1 << endl;
graph[x-1].push_back(y-1);
}
//cout << "\n";
memset(partLeft, 0x3f, MAXN*sizeof(uint32));
memset(partRight, 0x3f, MAXN*sizeof(uint32));
int ord;
do
{
ord = 0;
memset(usedEdges,0,MAXN*sizeof(uint32));
for (int i=0; i<n; ++i)
if (partLeft[i] == NOT_EXPLORED)
ord |= MaximumMatching(graph, i);
} while(ord);
int maxM=0;
for (int i=0; i<n; ++i)
{
if (partLeft[i] != NOT_EXPLORED)
maxM++;
}
fout << maxM << "\n";
for (int i=0; i<n; ++i)
if (partLeft[i] != NOT_EXPLORED)
fout << i+1 << " " << partLeft[i]+1 << "\n";
fin.close();
fout.close();
return 0;
}