Mai intai trebuie sa te autentifici.
Cod sursa(job #1498485)
| Utilizator | Data | 8 octombrie 2015 17:34:21 | |
|---|---|---|---|
| Problema | Ciclu Eulerian | Scor | 50 |
| Compilator | cpp | Status | done |
| Runda | Arhiva educationala | Marime | 1.64 kb |
#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];
bool vizitat[N];
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;
}
vizitat[1] = true;
for(int i = 1; i <= n; i++)
{
if(vizitat[i] == false)
{
out << "-1\n";
return 0;
}
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;
vizitat[y] = true;
ok[e] = 1;
blocat = false;
break;
}
}
if(blocat)
{
st--;
ciclu[++nCE] = x;
}
}
}
for(int i = 1; i <= m; i++)
out << ciclu[i] << ' ';
out << '\n';
return 0;
}
