Pagini recente » Cod sursa (job #3032685) | Cod sursa (job #2026928) | Cod sursa (job #2285034) | Cod sursa (job #245581) | Cod sursa (job #1644078)
#include <fstream>
using namespace std;
ifstream in("ciclueuler.in");
ofstream out("ciclueuler.out");
#define N 100001
#define M 500001
int n, m;
int lst[N], vf[2 * M], urm[2 * M], mch[2 * M], nvf = 0;
int stiva[2 * M], st = 0;
int cicluEulerian[2 * M], nce = 0;
bool ok[M];
int main()
{
in >> n >> m;
for(int i = 1; i <= m; i++)
{
int x, y;
in >> x >> y;
vf[++nvf] = y;
urm[nvf] = lst[x];
mch[nvf] = i;
lst[x] = nvf;
vf[++nvf] = x;
urm[nvf] = lst[y];
mch[nvf] = i;
lst[y] = nvf;
}
stiva[++st] = 1;
while(st)
{
int x = stiva[st];
bool blocat = 1;
for(int i = lst[x]; i; i = urm[i])
if(!ok[mch[i]])
{
stiva[++st] = vf[i];
ok[mch[i]] = 1;
blocat = 0;
break;
}
if(blocat)
{
st--;
cicluEulerian[++nce] = x;
}
}
for(int i = 1; i <= nvf; i++)
if(!ok[mch[i]])
{
out << "-1\n";
return 0;
}
if(cicluEulerian[1] != cicluEulerian[nce])
{
out << "-1\n";
return 0;
}
for(int i = 1; i < nce; i++)
out << cicluEulerian[i] << ' ';
out << '\n';
}