Pagini recente » Cod sursa (job #344393) | Cod sursa (job #1676785) | Cod sursa (job #2515598) | Cod sursa (job #2467539) | Cod sursa (job #2514231)
#include <algorithm>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <cstring>
#include <bitset>
using namespace std;
//#include <iostream>
#include <fstream>
//ifstream cin ("input.in"); ofstream cout ("output.out");
ifstream cin ("ciclueuler.in"); ofstream cout ("ciclueuler.out");
static const int NMAX = 1e5+5;
static const int MMAX = 5e5+5;
int sol[MMAX];
bitset <MMAX> f;
vector <pair<int, int>> graph[NMAX];
int k = 0;
void getEuler ( int vertex ) {
stack <int> nodes;
nodes.push(vertex);
while ( nodes.size() ) {
int curVertex = nodes.top();
if ( !graph[curVertex].size() ) {
sol[++k] = curVertex;
nodes.pop();
continue;
}
int edge = graph[curVertex].back().first;
int node = graph[curVertex].back().second;
graph[curVertex].pop_back();
if ( !f[edge] ) {
nodes.push(node);
f[edge] = 1;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int n, m;
cin>>n>>m;
for ( int i = 1; i <= m; ++i ) {
int x, y;
cin>>x>>y;
graph[x].push_back({i, y});
graph[y].push_back({i, x});
}
for ( int i = 1; i <= n; ++i ) {
if ( !graph[i].size() || graph[i].size()&1 ) {
cout<<-1<<'\n';
return 0;
}
}
getEuler(1);
for ( int i = 1; i < k; ++i ) {
cout<<sol[i]<<" ";
}
}