Pagini recente » Cod sursa (job #1733535) | Cod sursa (job #235867) | Cod sursa (job #2900360) | Cod sursa (job #2050364) | Cod sursa (job #3283441)
#include <fstream>
#include <vector>
#include <algorithm>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
const int nmax = 100000;
int n,m;
int gr[nmax + 5];
vector <pair<int,int>> v[nmax + 5];
bool f[nmax* 5 + 5];
vector <int> stk;
int main(){
fin>>n>>m;
stk.reserve(n);
for(int i=1;i<=m;i++)
{
int x,y;
fin>>x>>y;
v[x].push_back({y,i});
v[y].push_back({x,i});
gr[x]++;
gr[y]++;
}
for(int i=1;i<=n;i++)
if(gr[i]%2==1)
{
fout<<-1;
return 0;
}
stk.push_back(1);
int nrmch = 0;
fout<<1<<' ';
while(!stk.empty())
{
int top = stk.back();
while(v[top].empty()==false && f[v[top].back().second])
v[top].pop_back();
if(v[top].empty()){
//if(stk.back()!=1)
// fout<<stk.back()<<' ';
stk.pop_back();
continue;
}
else
{
++nrmch;
stk.push_back(v[top].back().first);
if(nrmch!=m+1)
fout<<v[top].back().first<<' ';
f[v[top].back().second]=true;
}
}
}