Pagini recente » Cod sursa (job #1447619) | Cod sursa (job #1126117) | Cod sursa (job #1525907) | Cod sursa (job #2363503) | Cod sursa (job #2815583)
#include <iostream>
#include <fstream>
#include <algorithm>
#include <math.h>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
class Graph
{
public:
struct Edge
{
int _nrOrd, _startingNode, _destinationNode;
Edge(int nrOrd, int startingNode, int destinationNode)
{
_nrOrd = nrOrd;
_startingNode = startingNode;
_destinationNode = destinationNode;
}
};
private:
bool _oriented;
bool _weighted;
bool _capacity;
int _nrNodes;
int _nrEdges;
vector < vector <Edge> > _neighborList;
//gets an Euler cycle if there is one
void EulerCycle(vector<bool>& visitedEdges, vector<int>& solution, int& edgeCounter, int sourceNode);
public:
Graph(int nrNodes = 0, int nrEdges = 0, bool oriented = false, bool weighted = false, bool capacity = false);
~Graph() {}
#pragma region Setters&Getters
void setOrientation(bool orientation)
{
_oriented = orientation;
}
void setNrNodes(int nrNodes)
{
_nrNodes = nrNodes;
}
void setNrEdges(int nrEdges)
{
_nrEdges = nrEdges;
}
bool getOrientation()
{
return _oriented;
}
int getNrNodes()
{
return _nrNodes;
}
int getNrEdges()
{
return _nrEdges;
}
#pragma endregion
//reads and builds the graph
void buildNeighborList(istream& input);
//returns a vector of all the edges
vector<Edge> getEdges();
//adds an edge to the graph
void addEdge(Edge newEdge);
//Function Wrappers
//returns the Euler cycle, if there is one
vector<int> buildEulerCycle();
};
Graph::Graph(int nrNodes, int nrEdges, bool oriented, bool weighted, bool capacity)
{
_nrNodes = nrNodes;
_nrEdges = nrEdges;
_oriented = oriented;
_weighted = weighted;
_capacity = capacity;
vector <Edge> emptyVector;
for (int i = 0; i < nrNodes; i++)
{
_neighborList.push_back(emptyVector);
}
}
void Graph::EulerCycle(vector<bool>& visitedEdges, vector<int>& solution, int& edgeCounter, int sourceNode)
{
while(_neighborList[sourceNode].empty() == false)
{
Edge edge = _neighborList[sourceNode].back();
int destinationNode = edge._destinationNode;
int edgeNrOrd = edge._nrOrd;
_neighborList[sourceNode].pop_back();
if (visitedEdges[edgeNrOrd] == false)
{
visitedEdges[edgeNrOrd] = true;
EulerCycle(visitedEdges, solution, edgeCounter, destinationNode);
}
}
solution.push_back(sourceNode);
edgeCounter++;
}
void Graph::buildNeighborList(istream& input)
{
int node1, node2, distance, capacity;
for (int i = 0; i < _nrEdges; i++)
{
input >> node1 >> node2;
node1--;
node2--;
if (_weighted == true)
{
fin >> distance;
}
else
{
distance = 0;
}
if (_capacity == true)
{
fin >> capacity;
}
else
{
capacity = 0;
}
Edge newEdge(i, node1, node2);
_neighborList[node1].push_back(newEdge);
if (_oriented == false)
{
Edge newEdge(i, node2, node1);
_neighborList[node2].push_back(newEdge);
}
}
}
vector<Graph::Edge> Graph::getEdges()
{
vector<Edge> edges;
if (_oriented == true)
{
for (int node = 0; node < _nrNodes; node++)
{
for (auto &edge : _neighborList[node])
{
edges.push_back(edge);
}
}
}
else
{
vector<bool> visited(_nrNodes, false);
for (int node = 0; node < _nrNodes; node++)
{
for (auto &edge : _neighborList[node])
{
if (visited[edge._destinationNode] == false)
{
edges.push_back(edge);
}
}
visited[node] = true;
}
}
return edges;
}
void Graph::addEdge(Edge newEdge)
{
_neighborList[newEdge._startingNode].push_back(newEdge);
if (_oriented == false)
{
//Edge newEdge2(newEdge._destinationNode, newEdge._startingNode, newEdge._distance, newEdge._capacity);
//_neighborList[newEdge._destinationNode].push_back(newEdge2);
}
_nrEdges++;
}
vector<int> Graph::buildEulerCycle()
{
vector<bool> visitedEdges(_nrEdges, false);
vector<int> solution;
int edgeCounter = 0;
for (int i = 0; i < _nrNodes; i++)
{
if (_neighborList[i].size() % 2 == 1)
{
solution.push_back(-1);
return solution;
}
}
EulerCycle(visitedEdges, solution, edgeCounter, 0);
if (edgeCounter != _nrEdges + 1)
{
solution[0] = -1;
return solution;
}
else
{
return solution;
}
}
int main()
{
vector<int> solution;
int nrNodes, nrEdges;
fin >> nrNodes >> nrEdges;
Graph graph(nrNodes, nrEdges, false, false, false);
graph.buildNeighborList(fin);
solution = graph.buildEulerCycle();
if (solution[0] != -1)
{
for (int i = 0; i < solution.size() - 1; i++)
{
fout << solution[i] + 1 << " ";
}
}
else
{
fout << -1;
}
fin.close();
fout.close();
return 0;
}