Pagini recente » Cod sursa (job #856240) | Cod sursa (job #3255610) | Cod sursa (job #346635) | Cod sursa (job #2490729) | Cod sursa (job #1467711)
#include <fstream>
#include <list>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
bool e_conex(const vector<list<int> >& graf){
vector<bool> e_viz(graf.size(), false);
stack<int> de_viz;
e_viz[0] = true;
de_viz.push(0);
while(!de_viz.empty()){
const int cur = de_viz.top();
de_viz.pop();
for(const auto next : graf[cur]){
if(!e_viz[next]){
e_viz[next] = true;
de_viz.push(next); } } }
return all_of(begin(e_viz), end(e_viz), [](const bool b){return b; }); }
bool e_eulerian(const vector<list<int> >& graf){
if(!e_conex(graf)){
return false; }
return all_of(begin(graf), end(graf),
[](const list<int>& v){return v.size()%2==0; }); }
int main(){
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
int n, m;
f >> n >> m;
vector<list<int> > graf(n);
for(int i = 0, a, b; i < m; ++i){
f >> a >> b;
--a, --b;
graf[a].push_back(b);
graf[b].push_back(a); }
if(!e_eulerian(graf)){
g << "-1";
return 0; }
stack<int> de_viz;
int cur = 0;
do{
int v = cur;
while(!graf[v].empty()){
const int w = graf[v].front();
de_viz.push(v);
graf[v].pop_front();
graf[w].erase(find(begin(graf[w]), end(graf[w]), v));
v = w; }
cur = de_viz.top();
de_viz.pop();
g << (cur+1) << ' ';
} while(!de_viz.empty());
return 0; }