Pagini recente » Cod sursa (job #955422) | Cod sursa (job #1883833) | Cod sursa (job #2356484) | Cod sursa (job #2176377) | Cod sursa (job #1365950)
#include <fstream>
#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
const int maxn = 100005;
vector <int> g[maxn];
int n, m;
inline void euler(int stnode) {
stack <int> st;
st.push(stnode);
vector <int> cycle;
while(!st.empty()) {
int node = st.top();
if(g[node].size()) {
int newnode = g[node].back();
st.push(newnode);
g[node].pop_back();
g[newnode].erase(find(g[newnode].begin(), g[newnode].end(), node));
}
else {
st.pop();
if(!st.empty())
cycle.push_back(node);
}
}
for(vector <int> :: iterator it = cycle.begin() ; it != cycle.end() ; ++ it)
fout << *it << ' ';
}
int main() {
fin >> n >> m;
for(int i = 1 ; i <= m ; ++ i) {
int x, y;
fin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
for(int i = 1 ; i <= n ; ++ i)
if(!g[i].size() || (g[i].size() % 2 == 1)) {
fout << "-1\n";
return 0;
}
euler(1);
}