Pagini recente » Cod sursa (job #1944800) | Cod sursa (job #393180) | Cod sursa (job #2531855) | Cod sursa (job #1426192) | Cod sursa (job #1482022)
#include <bits/stdc++.h>
using namespace std;
typedef int var;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
#define MAXN 100005
vector<pair<int, int>> G[MAXN];
bool Viz[MAXN];
int Parent[MAXN];
bool Del[1000000];
void dfs(int node) {
Viz[node] = 1;
for(auto &e : G[node]) {
if(!Viz[e.first]) {
Parent[e.first] = node;
Del[e.second] = 1;
dfs(e.first);
}
}
}
void ciclu(int node) {
while(node != 0) {
fout<<node<<" ";
while(!G[node].empty() && Del[G[node].back().second])
G[node].pop_back();
if(G[node].empty()) {
node = Parent[node];
} else {
Del[G[node].back().second] = 1;
node = G[node].back().first;
G[node].pop_back();
}
}
}
int main() {
int m, n, a, b;
fin>>n>>m;
while(m--) {
fin>>a>>b;
G[a].push_back({b, m});
G[b].push_back({a, m});
}
dfs(1);
for(int i=1; i<=n; i++) {
if(!Viz[i] || G[i].size() % 2) {
fout<<"-1";
return 0;
}
}
ciclu(1);
fout.seekp(fout.tellp() - 2);
fout<<" \n";
return 0;
}