Pagini recente » Cod sursa (job #2920056) | Cod sursa (job #1506691) | Cod sursa (job #301894) | Cod sursa (job #1584912) | Cod sursa (job #3281037)
#include <fstream>
#include <bitset>
#include <vector>
#include <stack>
using namespace std;
const int NMAX = 1e5 + 1, MMAX = 5e5 + 1;
struct muchie
{
int nod, nrm;
};
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
int n, m;
vector<muchie> G[NMAX];
vector<int> L;
bitset<MMAX> elim;
stack<int> S;
void citire()
{
int x, y;
fin >> n >> m;
for(int i = 1; i <= m; ++i)
{
fin >> 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()
{
bool ok = 1;
citire();
for(int i = 1; i <= n; ++i)
if(G[i].size() & 1)
{
ok = 0;
break;
}
if(ok)
{
euler();
L.pop_back();
for(const auto& x : L) fout << x << ' ';
}
else fout << "-1";
return 0;
}