Pagini recente » Cod sursa (job #1222867) | Cod sursa (job #1666833) | Cod sursa (job #2134299) | Cod sursa (job #1121825) | Cod sursa (job #3039673)
#include <algorithm>
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#include <queue>
#include <iomanip>
using namespace std;
string filename = "ciclueuler";
#ifdef LOCAL
ifstream fin("input.in");
ofstream fout("output.out");
#else
ifstream fin(filename + ".in");
ofstream fout(filename + ".out");
#endif
const int NMAX = 1e5;
const int EMAX = 5e5;
vector <int> adj[NMAX + 1];
struct Edge{
int u, v;
}edges[EMAX + 1];
bool viz[EMAX + 1];
signed main(){
int n, m;
fin >> n >> m;
for(int i = 1; i <= m; i++){
int a, b;
fin >> a >> b;
adj[a].push_back(i);
adj[b].push_back(i);
edges[i] = {a, b};
}
for(int i = 1; i <= n; i++){
if((int)adj[i].size() % 2 == 1){
fout << "-1\n";
return 0;
}
}
stack <int> S;
S.push(1);
vector <int> ans;
while(!S.empty()){
int node = S.top();
if(!adj[node].empty()){
int e = adj[node].back();
adj[node].pop_back();
if(!viz[e]){
viz[e] = true;
int To = edges[e].u ^ edges[e].v ^ node;
S.push(To);
}
}else{
S.pop();
ans.push_back(node);
}
}
ans.pop_back();
for(int node : ans){
fout << node << ' ';
}
fout << '\n';
return 0;
}