Pagini recente » Cod sursa (job #880100) | Cod sursa (job #2284947) | Cod sursa (job #873756) | Cod sursa (job #960641) | Cod sursa (job #2716887)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#include <unordered_map>
using namespace std;
int N, M;
void euler(vector<vector<int>>& links, vector<int>& from, vector<int>& to, vector<bool>& visits, vector<int>& cycle, int v)
{
stack<int> nodeStack;
nodeStack.push(v);
while (!nodeStack.empty())
{
int node = nodeStack.top();
if(!links[node].empty())
{
int edge = links[node].back();
links[node].pop_back();
if (!visits[edge])
{
visits[edge] = true;
int destination;
if (from[edge] != node)
{
destination = from[edge];
}
else
{
destination = to[edge];
}
nodeStack.push(destination);
//euler(links, from, to, visits, cycle, destination);
}
}
else
{
nodeStack.pop();
cycle.push_back(node);
}
}
}
int main()
{
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
// Program
f >> N >> M;
vector<vector<int>> links(N + 1, vector<int>());
// links[x] - all the links that have x
// links[x][1] = p - p'th link contains x
vector<int> from(M);
vector<int> to(M);
for (int i = 0; i < M; i++)
{
int source, destination;
f >> source >> destination;
links[source].push_back(i);
links[destination].push_back(i);
from[i] = source;
to[i] = destination;
}
vector<int> cycle;
vector<bool> visits(M);
euler(links, from, to, visits, cycle, 1);
auto end = cycle.end() - 1;
for (auto it = cycle.begin(); it != end; it++)
{
g << *it << " ";
}
// Exit
f.close();
g.close();
return 0;
}