Pagini recente » Cod sursa (job #394965) | Cod sursa (job #1333855) | Cod sursa (job #1801254) | Cod sursa (job #974188) | Cod sursa (job #1498492)
#include <fstream>
using namespace std;
ifstream in("ciclueuler.in");
ofstream out("ciclueuler.out");
#define N 100005
#define M 500005
struct muchie
{
int x, y;
};
int n, m;
int lst[N], vf[2 * M], urm[2 * M], nr;
muchie muchii[M];
bool ok[M];
int stiva[N], st;
int ciclu[2 * N], nCE;
int gasesteVecin(int x, muchie e)
{
return (x == e.x) ? e.y : e.x;
}
int main()
{
in >> n >> m;
for(int i = 1; i <= m; i++)
{
int x, y;
in >> x >> y;
muchii[i].x = x;
muchii[i].y = y;
vf[++nr] = i;
urm[nr] = lst[x];
lst[x] = nr;
vf[++nr] = i;
urm[nr] = lst[y];
lst[y] = nr;
}
for(int i = 1; i <= n; i++)
{
stiva[++st] = i;
while(st)
{
int x = stiva[st];
bool blocat = true;
for(int i = lst[x]; i; i = urm[i])
{
int e = vf[i];
if(!ok[e])
{
int y = gasesteVecin(x, muchii[e]);
stiva[++st] = y;
ok[e] = 1;
blocat = false;
break;
}
}
if(blocat)
{
st--;
ciclu[++nCE] = x;
}
}
}
for(int i = 1; i <= m; i++)
if(!ok[i])
{
out << -1 << '\n';
return 0;
}
for(int i = 1; i <= m; i++)
out << ciclu[i] << ' ';
out << '\n';
return 0;
}