Pagini recente » Cod sursa (job #462374) | Cod sursa (job #1917071) | Cod sursa (job #352186) | Cod sursa (job #762741) | Cod sursa (job #2803182)
#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, grad[N], 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(grad[i] % 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);
grad[x]++;
grad[y]++;
}
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 << ' ';
}
}