Pagini recente » Cod sursa (job #2362216) | Cod sursa (job #78468) | Cod sursa (job #367508) | Cod sursa (job #1164056) | Cod sursa (job #2832147)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
const int N = 500010;
vector<pair<int, int>> adj[N];
int n, m, st, dr;
int f[N], v[N];
int main()
{
fin >> n >> m;
for (int i = 1; i <= m; i++)
{
int x, y;
fin >> x >> y;
adj[x].push_back(make_pair(y, i));
adj[y].push_back(make_pair(x, i));
}
for (int i = 1; i <= n; i++)
{
if (adj[i].size() % 2 == 1)
{
fout << -1;
return 0;
}
}
deque<int> d = {};
d.push_front(1);
while (!d.empty())
{
int nod = d.front();
if (adj[nod].size() != 0)
{
int x = adj[nod][adj[nod].size() - 1].second;
if (f[x] == 0)
{
f[x] = 1;
d.push_front(adj[nod][adj[nod].size() - 1].first);
}
adj[nod].pop_back();
}
else
{
v[++st] = nod;
d.pop_front();
}
}
for (int i = 1; i <= st; i++)
fout << v[i] << " ";
}