Pagini recente » Cod sursa (job #1193192) | Cod sursa (job #434991) | Cod sursa (job #2449204) | Cod sursa (job #2391342) | Cod sursa (job #2905370)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
vector<int> G[100001];
vector<int> ans;
bool folosit[500001];
pair<int, int> muchie[500001];
int n, m;
void citire()
{
fin >> n >> m;
for (int i = 1; i <= n; ++i)
G[i].reserve(20);
for (int i = 0; i < m; i++)
{
int x, y;
fin >> x >> y;
G[x].push_back(i);
G[y].push_back(i);
muchie[i].first = x;
muchie[i].second = y;
}
}
void afisare()
{
for (int i = 0; i < ans.size()-1; i++)
fout << ans[i] << ' ';
}
void euler()
{
vector<int> st;
st.reserve(100001);
st.push_back(1);
while (!st.empty())
{
int node = st.back();
if(!G[node].empty())
{
int e = G[node].back();
G[node].pop_back();
if (!folosit[e])
{
folosit[e] = true;
int rez = muchie[e].first ^ muchie[e].second ^ node;
st.push_back(rez);
}
}
else
{
st.pop_back();
ans.push_back(node);
}
}
}
int main()
{
citire();
ans.reserve(m+1);
for (int i = 1; i <= n; i++)
if (G[i].size() & 1)
{
fout << "-1\n";
return 0;
}
euler();
afisare();
fin.close();
fout.close();
}