Pagini recente » Cod sursa (job #2572473) | Cod sursa (job #3288920) | Cod sursa (job #1953771) | Cod sursa (job #842853) | Cod sursa (job #3200963)
#include <fstream>
#include <set>
#include <vector>
using namespace std;
ifstream cin ("ciclueuler.in");
ofstream cout ("ciclueuler.out");
int n,m;
int u[100005];
int d[100005];
multiset <int> g[100005];
void scoate(int x,multiset<int> &v)
{
multiset<int>::iterator itr = v.find(x);
v.erase(itr);
}
vector<int> ciclu;
void dfs(int n)
{
u[n] = true;
for(auto x:g[n])
{
if(u[x])
continue;
dfs(x);
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for(int i=1;i<=m;i++)
{
int x,y;
cin >> x >> y;
d[x]++;
d[y]++;
g[x].insert(y);
g[y].insert(x);
}
dfs(1);
for(int i=1;i<=n;i++)
{
if(u[i]==false || d[i]%2!=0)
{
cout << "-1";
return 0;
}
}
vector<int> st;
st.push_back(1);
while(!st.empty())
{
int vf = st.back();
if(g[vf].empty())
{
st.pop_back();
ciclu.push_back(vf);
continue;
}
int u = *g[vf].begin();
scoate(u,g[vf]);
scoate(vf,g[u]);
st.push_back(u);
}
ciclu.pop_back();
for(auto x:ciclu)
cout << x << ' ';
return 0;
}