Pagini recente » Cod sursa (job #182754) | Cod sursa (job #3214889)
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream cin ("ciclueuler.in");
ofstream cout ("ciclueuler.out");
const int N = 1e5;
int grad[N + 1];
bool viz[N * 5 + 1];
struct muchie
{
int nod, nr_muchie;
};
vector <muchie> g[N + 1];
int n, m, x, y;
vector <int> sol;
stack <int> s;
int main()
{
cin >> n >> m;
for (int i = 1; i <= m; ++i)
{
cin >> x >> y;
g[x].push_back({y, i});
g[y].push_back({x, i});
grad[x]++;
grad[y]++;
}
for (int i = 1; i <= n; ++i)
if (grad[i] & 1)
{
cout << "-1\n";
return 0;
}
for (int i = 1; i <= n; ++i)
if (!g[i].empty())
{
s.push(i);
break;
}
while (!s.empty())
{
int node = s.top();
if (!g[node].empty())
{
int nod = g[node].back().nod;
int m = g[node].back().nr_muchie;
g[node].pop_back();
if (!viz[m])
viz[m] = 1, s.push(nod);
}
else
sol.push_back(node), s.pop();
}
sol.erase (sol.end() - 1);
for (auto it : sol)
cout << it << ' ';
return 0;
}