Pagini recente » Cod sursa (job #544022) | Cod sursa (job #1295958) | Cod sursa (job #270469) | Cod sursa (job #1293854) | Cod sursa (job #3282309)
#include <fstream>
#include <vector>
#include <stack>
#include <bitset>
using namespace std;
const int NMAX = 100001,
MMAX = 500001;
struct muchie
{
int nod, nrm; /// nrm = numarul muchiei
};
int n, m;
vector<muchie> G[NMAX];
vector<int> L;
stack<int> S;
bitset<MMAX> elim;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
void citire()
{
int x, y;
f >> n >> m;
for(int i = 1; i <= m; i++)
{
f >> x >> y;
G[x].push_back({y, i});
G[y].push_back({x, i});
}
}
void euler()
{
int x;
muchie mm;
//
S.push(1);
while(!S.empty())
{
x = S.top();
if(!G[x].empty())
{
mm = G[x].back();
G[x].pop_back();
if(!elim[mm.nrm])
{
elim[mm.nrm] = 1;
S.push(mm.nod);
}
}
else
{
L.push_back(x);
S.pop();
}
}
}
int main()
{
citire();
for(int i = 1; i <= n; i++)
if(G[i].size() & 1)
{
g << "-1";
return 0;
}
//
euler();
L.pop_back();
for(const auto &x : L)
g << x << ' ';
f.close();
g.close();
return 0;
}