Pagini recente » Cod sursa (job #2191594) | Cod sursa (job #684245) | Cod sursa (job #3146482) | Cod sursa (job #214369) | Cod sursa (job #2231708)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
const int MAXN = 100005, MAXM = 500005;
vector<pair<int, int> > graf[MAXN];
vector<int> ans;
int grad[MAXN];
bool vis[MAXM];
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
int n, m;
stack<int> euler;
int main()
{
fin >> n >> m;
int x, y;
for(int i = 1; i <= m; ++i){
fin >> x >> y;
grad[x]++;
grad[y]++;
graf[x].push_back({y, i});
graf[y].push_back({x, i});
}
bool ok = 1;
for(int i = 1; i <= n && ok; ++i)
if(grad[i] % 2 || grad[i] == 0)
ok = 0;
if(ok){
euler.push(1);
while(!euler.empty()){
int nod = euler.top();
if(graf[nod].size()){
int next = graf[nod].back().first, edge = graf[nod].back().second;
graf[nod].pop_back();
if(!vis[edge]){
vis[edge] = 1;
euler.push(next);
}
}
else{
ans.push_back(nod);
euler.pop();
}
}
ans.pop_back();
for(int i = 0; i < int(ans.size()); ++i)
fout << ans[i] << " ";
}
else
fout << -1;
return 0;
}