Pagini recente » Cod sursa (job #106380) | Istoria paginii runda/simulare_oji_clasa_a_9-a_1/clasament | Cod sursa (job #742766) | Cod sursa (job #360164) | Cod sursa (job #2444843)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("ciclueuler.in");
ofstream fout ("ciclueuler.out");
vector <int> a[100005], ans;
stack <int> s;
int main()
{
ios::sync_with_stdio(false);
fin.tie(0);
int n, m;
fin >> n >> m;
for(int i = 1; i <= m; ++i) {
int x, y;
fin >> x >> y;
a[x].push_back(y);
a[y].push_back(x);
}
for(int i = 1; i <= n; ++i) {
if(a[i].size() % 2 || !a[i].size()) {
fout << "-1";
return 0;
}
}
s.push(1);
while(!s.empty()) {
int x = s.top();
if(!a[x].empty()) {
int v = *a[x].begin();
vector <int>::iterator it = find(a[v].begin(), a[v].end(), x);
a[v].erase(it);
a[x].erase(a[x].begin());
s.push(v);
}
else {
s.pop();
ans.push_back(x);
}
}
ans.pop_back();
for(auto v : ans) fout << v << " ";
return 0;
}