Pagini recente » Cod sursa (job #2945386) | Cod sursa (job #2005200) | Cod sursa (job #661633) | Cod sursa (job #2037625) | Cod sursa (job #1581140)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <stack>
using namespace std;
ifstream fin ("ciclueuler.in");
ofstream fout ("ciclueuler.out");
const int nmax = 100005;
vector <int> g[nmax];
inline bool is_euler(int n)
{
for(int i=1; i<=n; i++)
if(g[i].size()%2==1) return false;
return true;
}
void euler()
{
stack <int> st;
int i, dad, son;
st.push(1);
while(!st.empty())
{
dad=st.top();
if(g[dad].empty())
{
fout << dad << " ";
st.pop();
}
else
{
son=g[dad][0];
g[dad].erase(g[dad].begin());
g[son].erase(find(g[son].begin(), g[son].end(), dad));
st.push(son);
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
int n, m, x, y, i;
fin >> n >> m;
for(i=1; i<=m; i++)
{
fin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
if(!is_euler(n)) fout << -1;
else euler();
return 0;
}