Pagini recente » Cod sursa (job #75414) | Cod sursa (job #2527324) | Cod sursa (job #2821974) | Cod sursa (job #58959) | Cod sursa (job #2845800)
#include <fstream>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <stack>
#include <unordered_map>
#define NMAX 100003
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
int n, m;
unordered_map<int, int>hashMap[NMAX];
int main()
{
fin >> n >> m;
for (int i = 1; i <= m; i++)
{
int x, y;
fin >> x >> y;
hashMap[x].insert({ i,y });
hashMap[y].insert({ i,x });
}
stack<int>stiv;
stiv.push(1);
vector<int>sol;
while (!stiv.empty())
{
int nod = stiv.top();
if (hashMap[nod].size() == 0) {
sol.push_back(nod);
stiv.pop();
}
else {
auto itr = hashMap[nod].begin();
int nd = itr->second;
int cod = itr->first;
stiv.push(nd);
hashMap[nd].erase(cod);
hashMap[nod].erase(cod);
}
}
if (sol.size() != m + 1)
{
fout << "-1";
return 0;
}
for (int i = 0; i < sol.size() - 1; i++)
{
fout << sol[i] << " ";
}
return 0;
}