Pagini recente » Cod sursa (job #2982059) | Cod sursa (job #1787160) | Cod sursa (job #3175172) | Cod sursa (job #655319) | Cod sursa (job #803660)
Cod sursa(job #803660)
#include<cstdio>
#include<cassert>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
int n, m;
vector<pair<int, int> > graph[100005];
pair<int, int> edg[500005];
int num[100005], ind[100005], viz[500005];
void read(){
assert(freopen("ciclueuler.in", "r", stdin));
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; ++i){
int x, y;
scanf("%d%d", &x, &y);
++num[x];
++num[y];
edg[i] = make_pair(x, y);
graph[x].push_back(make_pair(i, y));
graph[y].push_back(make_pair(i, x));
}
}
vector<int> ans;
void dfs(int e, int x){
if(num[x] == 0){
// ans.push_back(x);
if(x == edg[e].first)
ans.push_back(edg[e].second);
else
ans.push_back(edg[e].first);
return ;
}
while(ind[x] < graph[x].size()){
if(!viz[graph[x][ind[x]].first]){
viz[graph[x][ind[x]].first] = 1;
int aux = ind[x];
--num[x];
--num[graph[x][ind[x]].second];
++ind[x];
dfs(graph[x][aux].first, graph[x][aux].second);
}
else
++ind[x];
}
if(x == edg[e].first)
ans.push_back(edg[e].second);
else
ans.push_back(edg[e].first);
}
void write(){
assert(freopen("ciclueuler.out", "w", stdout));
if(ans.size() - 1 != m){
printf("-1\n");
return;
}
for(int i = 0; i < ans.size() - 1; ++i)
printf("%d ", ans[i]);
}
int main(){
read();
int isk = 1;
for(int i = 1; i <= n; ++i)
if(num[i] % 2 != 0)
isk = 0;
if(isk)
dfs(0, 1);
write();
return 0;
}