Pagini recente » Cod sursa (job #96688) | Cod sursa (job #343318) | Monitorul de evaluare | Cod sursa (job #1417879) | Cod sursa (job #2669097)
#include <iostream>
#include <fstream>
#include <vector>
#define NMAX 100001
#define MMAX 500005
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
int n, m, sizeOf[NMAX], index = 0;
int from[MMAX], to[MMAX];
vector <int> nodes[NMAX];
vector <int> result;
void read(){
f >> n >> m;
int x, y;
for (int i = 1; i <= m; i++){
f >> x >> y;
nodes[x].push_back(i);
sizeOf[x]++;
sizeOf[y]++;
if (x != y){
nodes[y].push_back(i);
}
if (x > y)
swap(x, y);
from[i] = x;
to[i] = y;
}
}
int main()
{
read();
for (int i = 1; i <= n; ++i) {
if (sizeOf[i] & 1) {
g << "-1\n";
return 0;
}
}
vector<int> stk;
stk.push_back(1);
while(!stk.empty()){
int node = stk.back();
if (!nodes[node].empty()){
int from_ = from[nodes[node].back()];
int to_ = to[nodes[node].back()];
int edgePosition = nodes[node].back();
nodes[node].pop_back();
if (from_ != 0){
int next;
if (from_ == node)
next = to_;
else
next = from_;
from[edgePosition] = 0;
to[edgePosition] = 0;
stk.push_back(next);
}
}
else{
stk.pop_back();
result.push_back(node);
}
}
for (int i = result.size() - 1; i > 0; i--)
g << result[i] << " ";
return 0;
}