Pagini recente » Cod sursa (job #474487) | Cod sursa (job #2550802) | Cod sursa (job #1545396) | Cod sursa (job #1565924) | Cod sursa (job #2821052)
#include <fstream>
#include <vector>
#include <utility>
using namespace std;
std::ifstream f("ciclueuler.in");
std::ofstream g("ciclueuler.out");
int N, M, nodStart;
vector<vector<int>> Adiacenta;
bool vizitat[100009];
vector<int> Solutie;
void Parcurgere()
{
Solutie.reserve(M);
int nodCurent = nodStart;
for (;;)
{
Solutie.push_back(nodCurent);
int nodUrmator = Adiacenta[nodCurent].size() > 0 ? Adiacenta[nodCurent][0] : 0;
if (nodUrmator)
{
Adiacenta[nodCurent].erase(Adiacenta[nodCurent].begin());
for (auto it = Adiacenta[nodUrmator].begin(); it != Adiacenta[nodUrmator].end(); ++it)
if (*it == nodCurent)
{
Adiacenta[nodUrmator].erase(it);
break;
}
nodCurent = nodUrmator;
}
else
break;
}
Solutie.pop_back();
}
int main()
{
f >> N >> M;
Adiacenta.resize(N + 1);
for (int x, y, i = 0; i < M; ++i)
{
f >> x >> y;
nodStart = x;
Adiacenta[x].push_back(y);
Adiacenta[y].push_back(x);
}
Parcurgere();
if (Solutie.size() != M)
g << -1;
else
for (auto nod : Solutie)
g << nod << ' ';
return 0;
}