Pagini recente » Cod sursa (job #14206) | Cod sursa (job #1598562) | Cod sursa (job #2934538) | Cod sursa (job #2617867) | Cod sursa (job #2722760)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
const int MAXN = 1e5 + 1,MAXM = 5e5 + 1;
using namespace std;
ifstream in("ciclueuler.in");
ofstream out("ciclueuler.out");
stack<int>graf[MAXN];
bool viz[MAXM];
int n,m,from[MAXM],to[MAXM];
int ans[MAXM];
int main()
{
in.tie(0);
out.tie(0);
ios::sync_with_stdio(false);
in>>n>>m;
for(int i = 1; i <= m; i++){
int x,y;
in>>x>>y;
graf[x].push(i);
graf[y].push(i);
from[i] = x;
to[i] = y;
}
for(int i = 1; i <= n; i++){
if(graf[i].size() & 1){
out<<-1;
return 0;
}
}
stack<int>stiva;
stiva.push(1);
int index = 0;
while(stiva.size()){
int nod = stiva.top();
if(graf[nod].size() > 0){
int label = graf[nod].top();
graf[nod].pop();
/// am label-ul muchiilor
if(!viz[label]){
int vecin = from[label] ^ to[label] ^ nod;
viz[label] = true;
stiva.push(vecin);
}
}else{
ans[index++] = nod;
stiva.pop();
}
}
// for(int i = 1; i <= m; i++){
// if(!viz[i]){
// out<<-1;
// return 0;
// }
for(int i = 0; i < index - 1; i++)
out<<ans[i]<<' ';
return 0;
}