Pagini recente » Cod sursa (job #139753) | Cod sursa (job #2288220) | Cod sursa (job #2329291) | Romanii medaliati la IOI | Cod sursa (job #2734335)
#include <fstream>
#include <vector>
#include <stack>
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], c;
int stiva[M];
/*
void euler(int x)
{
for (auto i: a[x])
{
if (e[i].folosit)
{
continue;
}
e[i].folosit = true;
int y = e[i].x + e[i].y - x;
euler(y);
}
c.push_back(x);
}
*/
void euler()
{
//stack <int> stiva;
//stiva.push(1);
int vf = 0;
stiva[++vf] = 1;
//while (!stiva.empty())
while (vf > 0)
{
//int x = stiva.top();
int x = stiva[vf];
bool gasit_muchie = false;
for (int j = a[x].size() - 1; j >= 0; j--)
{
int i = a[x][j];
a[x].pop_back();
if (e[i].folosit)
{
continue;
}
e[i].folosit = true;
int y = e[i].x + e[i].y - x;
//stiva.push(y);
stiva[++vf] = y;
gasit_muchie = true;
break;
}
if (!gasit_muchie)
{
c.push_back(x);
//stiva.pop();
vf--;
}
}
}
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);
a[e[i].y].push_back(i);
}
in.close();
//euler(1);
euler();
if (c.size() == m + 1 && c[m] == c[0])
{
c.erase(c.begin() + m);
for (auto x: c)
{
out << x << " ";
}
}
else
{
out << -1;
}
out.close();
return 0;
}