Pagini recente » Cod sursa (job #2765906) | infoarena 2.0 | Cod sursa (job #254288) | Cod sursa (job #2368894) | Cod sursa (job #3283443)
#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);
vector <int> sol;
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()){
stk.pop_back();
sol.push_back(top);
continue;
}
else
{
stk.push_back(v[top].back().first);
f[v[top].back().second]=true;
}
}
for(int i = 0 ; i< sol.size()-1;i++)
fout<<sol[i]<<' ';
}