Pagini recente » Cod sursa (job #1362807) | Cod sursa (job #1405609) | Cod sursa (job #2333788) | Cod sursa (job #1648340) | Cod sursa (job #1362506)
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
using namespace std;
#ifdef HOME
const string inputFile = "input.txt";
const string outputFile = "output.txt";
#else
const string problemName = "ciclueuler";
const string inputFile = problemName + ".in";
const string outputFile = problemName + ".out";
#endif
const int NMAX = 100000 + 5;
int N, M;
vector<int> V[NMAX];
bitset<NMAX> viz;
vector<int> S, sol;
void dfs(int x) {
viz[x] = 1;
for(auto y : V[x])
if(!viz[y])
dfs(y);
}
int is_euler() {
int i;
dfs(1);
for(i = 1; i <= N; i++)
if(!viz[i] || V[i].size() % 2)
return 0;
return 1;
}
void erase(int x, int y) {
for(auto &z : V[x])
if(z == y) {
swap(V[x].back(), z);
V[x].pop_back();
return;
}
}
void get_euler() {
int x, y;
S.push_back(1);
while(!S.empty()) {
x = S.back();
if(V[x].empty()) {
S.pop_back();
sol.push_back(x);
} else {
y = V[x][0];
erase(x, y);
erase(y, x);
S.push_back(y);
}
}
}
int main() {
int i, x, y;
freopen(inputFile.c_str(), "r", stdin);
freopen(outputFile.c_str(), "w", stdout);
scanf("%d%d", &N, &M);
while(M--) {
scanf("%d%d", &x, &y);
V[x].push_back(y);
V[y].push_back(x);
}
if(is_euler()) {
get_euler();
for(i = 0; i < sol.size() - 1; i++)
printf("%d ", sol[i]);
} else
printf("-1\n");
return 0;
}