Pagini recente » Cod sursa (job #1194559) | Cod sursa (job #813747) | Cod sursa (job #2863809) | Cod sursa (job #1143283) | Cod sursa (job #1866636)
#include <fstream>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
class InputReader {
public:
InputReader() {}
InputReader(const char *file_name) {
input_file = fopen(file_name, "r");
cursor = 0;
fread(buffer, SIZE, 1, input_file);
}
inline InputReader &operator >>(int &n) {
while(buffer[cursor] < '0' || buffer[cursor] > '9') {
advance();
}
n = 0;
while('0' <= buffer[cursor] && buffer[cursor] <= '9') {
n = n * 10 + buffer[cursor] - '0';
advance();
}
return *this;
}
private:
FILE *input_file;
static const int SIZE = 1 << 17;
int cursor;
char buffer[SIZE];
inline void advance() {
++ cursor;
if(cursor == SIZE) {
cursor = 0;
fread(buffer, SIZE, 1, input_file);
}
}
};
const int NMAX = 100000 + 5;
const int MMAX = 5 * NMAX;
struct Edge {
int a, b;
bool vis;
int other(int node) {
if (node == a)
return b;
else
return a;
}
} edges[MMAX];
int N, M;
vector <int> graph[NMAX];
int ii[NMAX];
stack <int> stk;
vector <int> sol;
int main()
{
InputReader cin("ciclueuler.in");
ofstream cout("ciclueuler.out");
cin >> N >> M;
for (int i = 1; i <= M; ++ i) {
cin >> edges[i].a >> edges[i].b;
graph[edges[i].a].push_back(i);
graph[edges[i].b].push_back(i);
}
bool bad = false;
for (int i = 1; i <= N; ++ i)
if (graph[i].size() % 2 == 1) {
bad = true;
break;
}
if (bad) {
cout << "-1\n";
return 0;
}
stk.push(1);
while (!stk.empty()) {
int node = stk.top();
bool found = false;
for (; ii[node] < graph[node].size(); ++ ii[node])
if (!edges[graph[node][ii[node]]].vis) {
edges[graph[node][ii[node]]].vis = true;
stk.push(edges[graph[node][ii[node]]].other(node));
found = true;
break;
}
if (!found) {
stk.pop();
sol.push_back(node);
}
}
reverse(sol.begin(), sol.end());
sol.pop_back();
for (int i = 0; i < sol.size(); ++ i)
cout << sol[i] << " \n"[i + 1 == sol.size()];
return 0;
}