Pagini recente » Cod sursa (job #622888) | Cod sursa (job #585401) | Cod sursa (job #3199802) | Cod sursa (job #1331072) | Cod sursa (job #2749521)
#include <fstream>
#include <vector>
#include <utility>
#include <stack>
#define nmax 100005
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
int n, m, x, y;
vector<pii> G[nmax];
int sol[nmax * 5];
bool seen[nmax * 5];
bool used[nmax * 5];
void dfs(int k) {
while (G[k].size()) {
auto act = G[k].back();
G[k].pop_back();
if (used[act.second]) {
continue;
}
used[act.second] = true;
dfs(act.first);
}
fout << k << ' ';
}
int main()
{
fin >> n >> m;
for (int i=1;i<=m;++i) {
fin >> x >> y;
G[x].pb(mp(y, i));
G[y].pb(mp(x, i));
}
for (int i=1;i<=n;++i) {
if (G[i].size() % 2) {
fout << "-1\n";
return 0;
}
}
stack<int> st;
st.push(1);
int seenNodes = 0;
while (st.size()) {
auto act = st.top();
if (!seen[act]) {
seen[act] = true;
++seenNodes;
}
if (G[act].size()) {
auto node = G[act].back();
G[act].pop_back();
if (used[node.second]) {
continue;
}
used[node.second] = true;
st.push(node.first);
continue;
} else {
st.pop();
sol[++sol[0]] = act;
}
}
if (seenNodes < n) {
fout << "-1\n";
} else {
for (int i=1;i<=sol[0];++i) {
fout << sol[i] << ' ';
}
fout << '\n';
}
fin.close();
fout.close();
return 0;
}