Pagini recente » Cod sursa (job #1270382) | Cod sursa (job #17689) | Cod sursa (job #361859) | Cod sursa (job #710487) | Cod sursa (job #2527411)
#include <iostream>
#include <vector>
#include <fstream>
#include <assert.h>
const int MAXN = 100000 + 1;
using namespace std;
ifstream in("ciclueuler.in");
ofstream out("ciclueuler.out");
bool viz[500005];
int grad[MAXN];
int stiva[5*MAXN];
vector<pair<int,int> >graf[500005];
int n,m;
void dfs(int nod){
for(auto pereche : graf[nod]){
int curent = pereche.first;
int label = pereche.second;
if(!viz[label]){
viz[label] = true;
dfs(curent);
out<<nod<<" ";
}
}
}
void dfs_stiva(int start){
int varf = 0;
stiva[++varf] = start;
while(varf > 0){
bool are_vecini = false;
int nod = stiva[varf];
for(int i = 0; i < graf[nod].size(); i++){
int curent = graf[nod][i].first;
int label = graf[nod][i].second;
if(!viz[label]){
are_vecini = true;
viz[label] = true;
stiva[++varf] = curent;
break;
}
}
if(!are_vecini){
varf--;
out<<nod<<" ";
}
}
}
int main()
{
in>>n>>m;
int muchii = 0;
for(int i = 1; i <= m; i++){
int x,y;
in>>x>>y;
graf[x].push_back({y,++muchii});
graf[y].push_back({x,muchii});
grad[x]++;
grad[y]++;
}
bool ok = true;
for(int i = 1; i <= n && ok; i++)
if(grad[i] % 2)
ok = false;
if(!ok){
out<<-1;
return 0;
}
dfs_stiva(1);
return 0;
}