Pagini recente » Cod sursa (job #1839413) | Cod sursa (job #592388) | Cod sursa (job #428150) | Cod sursa (job #2319408) | Cod sursa (job #2101862)
#include <iostream>
#include <fstream>
#include <stack>
#include <vector>
using namespace std;
ifstream in("ciclueuler.in");
ofstream out("ciclueuler.out");
int n,m,grad[100005],i,x,y,lung, ll[100005],z;
vector< pair <int,int> > a[10005];
bool v[100005];
stack<int> st;
void citire(){
in>>n>>m;
for(i=1;i<=m;++i){
in>>x>>y;
a[x].push_back({y, i});
a[y].push_back({x, i});
grad[x]++;
grad[y]++;
}
}
int main()
{
citire();
for(i=1;i<=n;i++){
if(grad[i]%2 || !grad[i]){
out<<-1;
return 0;
}
}
st.push(1);
while(!st.empty()){
x=st.top();
lung=a[x].size();
while(ll[x]<lung){
y=a[x][ll[x]].first;
z=a[x][ll[x]].second;
if(!v[z]){
v[z]=1;
st.push(y);
break;
}
ll[x]++;
}
if(ll[x] == lung)
{
out<<x<<" ";
st.pop();
}
}
return 0;
}