Pagini recente » Cod sursa (job #18044) | Cod sursa (job #1345048) | Cod sursa (job #1226838) | Cod sursa (job #686708) | Cod sursa (job #2077368)
#include <bits/stdc++.h>
using namespace std;
const int Nmax = 100000 + 5;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
#define pii pair<int, int>
#define x first
#define pb push_back
#define y second
vector<pii>v[Nmax];
stack<int>st;
bool viz[Nmax], fol[5 * Nmax];
int n, m, g[Nmax];
void dfs(int nod)
{
viz[nod] = 1;
for(auto i : v[nod])
if(!viz[i.x])dfs(i.x);
}
int main()
{
fin >> n >> m;
for(int i =1, a, b; i <= m; ++i)
{
fin >> a >> b;
v[a].pb({b, i});
v[b].pb({a, i});
g[a]++; g[b]++;
}
dfs(1);
for(int i =1 ; i <= n; ++i)
if(g[i] % 2 == 1 || !viz[i])
{
fout << -1;
return 0;
}
st.push(1); int first = 0;
while(!st.empty())
{
int nod = st.top();
if(g[nod] == 0)
{
if(!first)first = 1;
else fout << nod << ' ';
st.pop();
continue;
}
while(fol[v[nod].back().y])
v[nod].pop_back();
int fiu = v[nod].back().x;
g[nod]--;g[fiu]--;
fol[v[nod].back().y] = 1;
v[nod].pop_back();
st.push(fiu);
}
return 0;
}