Pagini recente » Cod sursa (job #2803444) | Cod sursa (job #1976456) | Cod sursa (job #1370901) | Cod sursa (job #2126207) | Cod sursa (job #1752988)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
typedef struct vertex
{
int id;
bool validFlag;
bool marked;
bool dfsMarked;
bool isLeft;
struct vertex* matchedVertex;
vector<vertex*> adjencyList;
} Vertex;
Vertex** CreateVector(int n, bool isLeft);
bool DoDfs(int k, Vertex* startNode, Vertex** leftList, Vertex** rightList);
void ReadEdges(int e, Vertex** leftList, Vertex** rightList, istream &fin);
int main()
{
ifstream fin;
ofstream fout;
fout.open("cuplaj.out");
fin.open("cuplaj.in");
int n, m, e;
fin >> n >> m >> e;
Vertex** leftVertexList = CreateVector(n, true);
Vertex** rightVertexList = CreateVector(m, false);
ReadEdges(e, leftVertexList, rightVertexList, fin);
bool modify = true;
int k = 1;
while(modify)
{
modify = false;
for(int i = 1; i <= n; i++)
{
if(leftVertexList[i]->marked == false)
{
if(DoDfs(k, leftVertexList[i], leftVertexList, rightVertexList))
{
modify = true;
}
for(int i = 1; i <= n; i++)
leftVertexList[i]->dfsMarked = false;
for(int i = 1; i <= m; i++)
rightVertexList[i]->dfsMarked = false;
}
}
for(int i = 1; i <= n; i++)
leftVertexList[i]->validFlag = true;
for(int i = 1; i <= m; i++)
rightVertexList[i]->validFlag = true;
k+=2;
}
int matchNumber = 0;
for(int i = 1; i <= m; i++)
{
if(rightVertexList[i]->matchedVertex)
matchNumber++;
}
fout << matchNumber << "\n";
for(int i = 1; i <= m; i++)
{
if(rightVertexList[i]->matchedVertex)
fout << rightVertexList[i]->matchedVertex->id << " " << rightVertexList[i]->id << "\n";
}
fin.close();
fout.close();
return 0;
}
bool DoDfs(int k, Vertex* startNode, Vertex** leftList, Vertex** rightList)
{
if (k < 0)
return false;
if(k == 0 && startNode->marked == false)
{
startNode->marked = true;
startNode->validFlag = false;
return true;
}
startNode->dfsMarked = true;
for(int i = 0; i < startNode->adjencyList.size(); i++)
{
Vertex* y = startNode->adjencyList[i];
if(y->validFlag == true && y->dfsMarked == false)
{
if(startNode->isLeft || (!startNode->isLeft && y->marked))
{
if(DoDfs(k-1, y, leftList, rightList))
{
y->matchedVertex = startNode;
startNode->validFlag = false;
startNode->marked = true;
return true;
}
}
}
}
return false;
}
Vertex** CreateVector(int n, bool isLeft)
{
Vertex **list = new Vertex*[n + 1]();
for(int i = 1; i <= n; i++)
{
list[i] = new Vertex();
list[i]->id = i;
list[i]->validFlag = true;
list[i]->matchedVertex = NULL;
list[i]->isLeft = isLeft;
}
return list;
}
void ReadEdges(int e, Vertex** leftList, Vertex** rightList, istream &fin)
{
int x, y;
for(int i = 1; i <= e; i++)
{
fin >> x >> y;
leftList[x]->adjencyList.push_back(rightList[y]);
rightList[y]->adjencyList.push_back(leftList[x]);
}
}