Pagini recente » Cod sursa (job #212674) | Cod sursa (job #318008) | Cod sursa (job #2700773) | Cod sursa (job #2192995) | Cod sursa (job #1230610)
#include<fstream>
#include<algorithm>
#include<vector>
#include<stack>
#define Nmax 100009
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
int n,m,x,y;
vector <int> G[Nmax], Cycle;
stack <int> st;
inline int IsEulerian()
{
for(int i=1;i<=n;++i)
if(G[i].size() &1) return 0;
return 1;
}
inline void GetCycle(int S)
{
for(st.push(S); !st.empty();)
{
int node=st.top();
if(G[node].size())
{
int last=G[node].back();
st.push(last);
G[node].pop_back();
G[last].erase(find(G[last].begin(),G[last].end(),node));
}
else st.pop() , Cycle.push_back(node);
}
}
int main()
{
f>>n>>m;
for(int i=1;i<=m;++i){
f>>x>>y;
G[x].push_back(y);
G[y].push_back(x);
}
if(IsEulerian()){
GetCycle(1);
vector<int> :: iterator it;
for(it=Cycle.begin();it!=Cycle.end();++it)
g<<*it<<' ';
g<<'\n';
}
else g<<-1<<'\n';
f.close();g.close();
return 0;
}