Pagini recente » Cod sursa (job #1841547) | Cod sursa (job #2581461) | Cod sursa (job #2343396) | Cod sursa (job #2628479) | Cod sursa (job #2734308)
#include <fstream>
#include <vector>
using namespace std;
const int M = 500001;
const int N = 100001;
struct muchie
{
int x, y;
bool folosit;
};
muchie e[M];
vector <int> a[N];
int c[M+1], ne, ind[2*M], urm[2*M], lst[N], nr;
void euler(int x)
{
//for (auto i: a[x])
for (int p = lst[x]; p != 0; p = urm[p])
{
int i = ind[p];
if (e[i].folosit)
{
continue;
}
e[i].folosit = true;
int y = e[i].x + e[i].y - x;
euler(y);
}
//c.push_back(x);
c[ne++] = x;
}
void adauga(int x, int i)
{
ind[++nr] = i;
urm[nr] = lst[x];
lst[x] = nr;
}
int main()
{
ifstream in("ciclueuler.in");
ofstream out("ciclueuler.out");
int n, m;
in >> n >> m;
for (int i = 0; i < m; i++)
{
in >> e[i].x >> e[i].y;
//a[e[i].x].push_back(i);
adauga(e[i].x, i);
//a[e[i].y].push_back(i);
adauga(e[i].y, i);
}
in.close();
euler(1);
//if (c.size() == m + 1)
if (ne == m + 1 && c[0] == c[ne-1])
{
//c.erase(c.begin() + m);
ne--;
//for (auto x: c)
for (int i = 0; i < ne; i++)
{
int x = c[i];
out << x << " ";
}
}
else
{
out << -1;
}
out.close();
return 0;
}