Pagini recente » Cod sursa (job #290999) | Cod sursa (job #2999511) | Cod sursa (job #950186) | Cod sursa (job #2285421) | Cod sursa (job #2203993)
#include <fstream>
#include <vector>
#include <algorithm>
#include <stack>
#define pb push_back
#define NMAX 100005
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
int n, m;
stack<int> st;
vector<int> G[NMAX];
vector<int> sol;
void read_data(){
f >> n >> m;
for(int i = 1; i<=m; i++){
int x, y;
f >> x >> y;
G[x].pb(y);
G[y].pb(x);
}
}
bool is_euler(){
for(int i = 1; i<=n; i++){
if(G[i].size() % 2 != 0){
return false;
}
}
return true;
}
void solve_cycle(){
st.push(1);
while(!st.empty()){
int node = st.top();
if(!G[node].empty()){
int new_node = G[node].back();
G[node].pop_back();
G[new_node].erase(find(G[new_node].begin(), G[new_node].end(), node));
st.push(new_node);
}else{
sol.pb(node);
st.pop();
}
}
}
int main(){
read_data();
if(is_euler() == false){
g << -1;
return 0;
}
else{
solve_cycle();
for(int i = 0; i<sol.size(); i++){
g << sol[i] << ' ';
}
}
return 0;
}