Pagini recente » Cod sursa (job #2391070) | Cod sursa (job #1282599) | Cod sursa (job #1851896) | Cod sursa (job #938791) | Cod sursa (job #2802809)
#include <fstream>
#include <bitset>
#include <stack>
#include <vector>
using namespace std;
const int N = 1e5;
const int M = 5e5;
pair<int, int> e[M];
bitset <M> sters;
vector <int> a[N+1];
stack<int> stiva;
int poz[N+1], n, m;
bool este_euler()
{
return true;
}
int caut_muchie(int x)
{
for (int i = poz[x]; i < a[x].size(); i++)
{
if (!sters[a[x][i]])
{
poz[x] = i + 1;
return a[x][i];
}
}
return -1;
}
int main()
{
ifstream in("ciclueuler.in");
ofstream out("ciclueuler.out");
in >> n >> m;
for (int i = 0; i < m; i++)
{
int x, y;
in >> x >> y;
e[i] = {x, y};
a[x].push_back(i);
a[y].push_back(i);
}
in.close();
if (!este_euler())
{
out << -1;
out.close();
return 0;
}
stiva.push(1);
while (!stiva.empty())
{
int x = stiva.top();
int muchie = caut_muchie(x);
if (muchie == -1)
{
stiva.pop();
if (!stiva.empty())
{
out << x << " ";
}
}
else
{
int y = e[muchie].first + e[muchie].second - x;
stiva.push(y);
sters[muchie] = 1;
}
}
return 0;
}