Pagini recente » Cod sursa (job #2825903) | Cod sursa (job #220591) | Cod sursa (job #406365) | Cod sursa (job #1647496) | Cod sursa (job #2576283)
#include <bits/stdc++.h>
#define pb push_back
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
const int MAXN = 100005, MAXM = 500005;
struct Edge
{
int x, y;
}edge[MAXM];
vector<int> graph[MAXN], res;
int gr[MAXN], n, m;
bool vis[MAXM];
stack<int> st;
void read()
{
fin >> n >> m;
for (int i = 0; i < m; ++i) {
int x, y;
fin >> x >> y;
graph[x].pb(i);
graph[y].pb(i);
edge[i] = {x, y};
++gr[x], ++gr[y];
}
}
bool check()
{
for (int i = 1; i <= n; ++i)
if (gr[i] & 1)
return false;
return true;
}
void solve()
{
st.push(1);
while (!st.empty()) {
int node = st.top();
if (graph[node].size() == 0) {
st.pop();
if (!st.empty())
fout << node << ' ';
continue;
}
int last = graph[node].back(), from = edge[last].x, to = edge[last].y;
graph[node].pop_back();
if (vis[last] == true)
continue;
vis[last] = true;
if (to == node)
to = from;
st.push(to);
}
}
int main()
{
read();
if (check() == false)
fout << "-1\n";
else
solve();
return 0;
}