Pagini recente » Cod sursa (job #3362421) | Cod sursa (job #3362356)
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1, M = 5e5 + 1;
int n, m, x, y;
vector<pair<int, int>> mc[N];
stack<int> st;
vector<int> ans;
bool vis[M];
int main()
{
freopen("ciclueuler.in", "r", stdin);
freopen("ciclueuler.out", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= m; ++i)
{
cin >> x >> y;
mc[x].push_back({y, i});
mc[y].push_back({x, i});
}
for (int i = 1; i <= n; ++i)
{
if (mc[i].size() % 2)
{
cout << -1;
return 0;
}
}
st.push(1);
for (int i = 1; i <= m; ++i)
{
if (st.empty())
{
cout << -1;
return 0;
}
int nod = st.top();
while (!mc[nod].empty())
{
int nxt = mc[nod].back().first, idx = mc[nod].back().second;
mc[nod].pop_back();
if (vis[idx])
continue;
vis[idx] = 1;
st.push(nxt);
nod = nxt;
}
st.pop();
ans.push_back(nod);
}
if (ans.size() != m)
{
cout << -1;
return 0;
}
for (auto i : ans)
cout << i << ' ';
return 0;
}