Pagini recente » Cod sursa (job #2151951) | Cod sursa (job #1572596) | Cod sursa (job #960251) | Cod sursa (job #1139188) | Cod sursa (job #2209899)
#include <fstream>
#include <stack>
#include <vector>
#include <algorithm>
#define NMAX 100005
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
vector<int> G[NMAX];
int n, m;
void read_data(){
f >> n >> m;
for(int i = 1; i<=m; i++){
int x, y;
f >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
}
bool check_euler(){
for(int i = 1; i<=n; i++){
if(G[i].size() % 2 != 0){
return false;
}
}
return true;
}
vector<int> euler_cycl(){
vector<int> sol;
if(!check_euler()){
return {-1};
}
stack<int> st;
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.push_back(node);
st.pop();
}
}
return sol;
}
int main(){
read_data();
auto sol = euler_cycl();
for(const auto& elem:sol){
g << elem << ' ';
}
return 0;
}