Pagini recente » Cod sursa (job #214010) | Cod sursa (job #1848198) | Cod sursa (job #2147977) | Cod sursa (job #1635016) | Cod sursa (job #1435164)
#include<fstream>
#include<algorithm>
#include<list>
#include<stack>
#define N 100010
using namespace std;
ofstream g("ciclueuler.out");
list<int> v[N];
list<int>::iterator it;
stack<int> st;
int n, m, i, x, y, ok;
void euler(int nd)
{
int vec;
st.push(nd);
while(!st.empty())
{
nd = st.top();
if(v[nd].size()==0)
{
g << nd << " ";
st.pop();
continue;
}
vec = v[nd].front();
v[nd].pop_front();
st.push(vec);
for(it = v[vec].begin(); it != v[vec].end(); it++)
if(*it == nd)
{
v[vec].erase(it);
break;
}
}
}
int main()
{
ifstream f("ciclueuler.in");
f >> n >> m;
for(i = 1; i <= m; i++)
{
f >> x >> y;
v[x].push_back(y);
v[y].push_back(x);
}
ok=1;
for(i = 1; i <= n; i++)
if(v[i].size() & 1)
{
ok=0;
break;
}
if(!ok) g << "-1\n";
else
euler(1);
f.close();
g.close();
return 0;
}