Pagini recente » Cod sursa (job #2541432) | Cod sursa (job #831891) | Cod sursa (job #702494) | Cod sursa (job #1387766) | Cod sursa (job #2803186)
#include <bits/stdc++.h>
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
const int N = 1e5 + 1, M = 5e5;
int n, m, x, y, poz[N];
pair<int, int> muchie[M];
vector<int> incidenta[N], ans;
bitset<M> sters;
stack<int> s;
bool gradePare(){
for(int i = 1; i <= n; i++){
if(incidenta[i].size() % 2)
return 0;
}
return 1;
}
int cautMuchie(int x){
for(int i = poz[x]; i < incidenta[x].size(); i++){
if(!sters[incidenta[x][i]]){
poz[x] = i + 1;
return incidenta[x][i];
}
}
return -1;
}
int main(){
f >> n >> m;
for(int i = 0; i < m; i++){
f >> x >> y;
muchie[i] = {x, y};
incidenta[x].push_back(i);
incidenta[y].push_back(i);
}
f.close();
if(!gradePare()){
g << -1;
return 0;
}
s.push(1);
while(!s.empty()){
x = s.top();
int e = cautMuchie(x);
if(e == -1){ //
s.pop();
if(!s.empty()) // vrem sa nu scriem primul element si la sfarsit
ans.push_back(x);
}else{
sters[e] = 1;
y = muchie[e].first + muchie[e].second - x;
s.push(y);
}
}
if(ans.size() < m){ // graful are mai multe cicluri disjuncte
g << -1;
}else{
for(int x: ans)
g << x << ' ';
}
g.close();
}