Pagini recente » Cod sursa (job #483452) | Cod sursa (job #928512) | Cod sursa (job #3236576) | Cod sursa (job #1629287) | Cod sursa (job #2734337)
#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()
{
stack <int> stiva;
stiva.push(1);
while (!stiva.empty())
{
int x = stiva.top();
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);
gasit_muchie = true;
break;
}
if (!gasit_muchie)
{
c.push_back(x);
stiva.pop();
}
}
}
bool toate_pare(int n)
{
for (int i = 1; i <= n; i++)
{
if (a[i].size() % 2 != 0)
{
return false;
}
}
return true;
}
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();
if (toate_pare(n))
{
c.erase(c.begin() + m);
for (auto x: c)
{
out << x << " ";
}
}
else
{
out << -1;
}
out.close();
return 0;
}