Pagini recente » Cod sursa (job #2084516) | Cod sursa (job #1530620) | Cod sursa (job #479906) | Cod sursa (job #1402595) | Cod sursa (job #1153054)
#include <fstream>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
int N, M;
vector<int> V[100001];
stack<int> ST;
bool instack[100001];
int is_euler(int nod)
{
ST.push(nod);
instack[nod] = true;
while (!ST.empty())
{
int now = ST.top();
ST.pop();
for (vector<int>::iterator it = V[now].begin(); it != V[now].end(); ++it)
{
if (!instack[*it])
{
ST.push(*it);
instack[*it] = true;
}
}
}
int ok = 1;
for (int i = 1; i <= N; ++i)
{
if (V[i].size() % 2 == 1 || !instack[i])
{
ok = 0;
break;
}
}
return ok;
}
void solve_euler(int nod)
{
ST.push(nod);
while (!ST.empty())
{
int now = ST.top();
if (V[now].size() > 0)
{
int neighbour = V[now][0];
ST.push(neighbour);
V[now].erase(V[now].begin());
V[neighbour].erase(find(V[neighbour].begin(), V[neighbour].end(), now));
}
else
{
fout << now << ' ';
ST.pop();
}
}
}
int main()
{
fin >> N >> M;
for (int i = 1, nod1, nod2; i <= M; ++i)
{
fin >> nod1 >> nod2;
V[nod1].push_back(nod2);
V[nod2].push_back(nod1);
}
if (!is_euler(1))
{
fout << -1 << '\n';
fin.close();
fout.close();
return 0;
}
solve_euler(1);
fin.close();
fout.close();
return 0;
}