Pagini recente » Cod sursa (job #1114914) | Cod sursa (job #1750482) | Statistici Margarit Ioan (LostDelmu) | Cod sursa (job #1528129) | Cod sursa (job #1649216)
# include <cstdio>
# include <stack>
# include <vector>
using namespace std;
FILE *f = freopen("ciclueuler.in", "r", stdin);
FILE *g = freopen("ciclueuler.out", "w", stdout);
const int N_MAX = 100001;
const int M_MAX = 500001;
struct Muchie{
int index;
int capat;
Muchie (int& a, int& b)
{
this -> index = a;
this -> capat = b;
}
};
stack <int> stiva;
vector <Muchie> G[N_MAX];
vector <int> solutie;
int grad[N_MAX];
int vizitatM[M_MAX];
int n, m, M;
bool se_Poate;
void adauga(int& x, int& y)
{
grad[x] ++;
grad[y] ++;
M++;
G[x].push_back(Muchie(M, y));
G[y].push_back(Muchie(M, x));
}
void read()
{
scanf("%d %d", &n, &m);
for (int i=1; i<=m; i++)
{
int x, y;
scanf("%d %d", &x, &y);
adauga(x, y);
}
}
int sterge_vecin(int& nod)
{
while (!G[nod].empty() && vizitatM[G[nod].back().index])
G[nod].pop_back();
if (!G[nod].empty())
{
int v = G[nod].back().capat;
vizitatM[G[nod].back().index] = true;
grad[nod] --;
grad[v] --;
M--;
G[nod].pop_back();
return v;
}
else return 0;
}
void solve()
{
se_Poate = true;
for (int i=1; i<=n; i++)
if (grad[i] % 2 == 1)
se_Poate = false;
if (se_Poate)
{
int i;
for (i=1; grad[i] == 0 && i<=n; i++);
stiva.push(i);
while (!stiva.empty())
{
int nod = stiva.top();
if (grad[nod])
{
stiva.push(sterge_vecin(nod));
}
else
{
solutie.push_back(stiva.top());
stiva.pop();
}
}
}
if (M > 0) se_Poate = false;
}
void write()
{
if (se_Poate)
{
for (int i=0; i<solutie.size()-1; i++)
printf("%d ", solutie[i]);
printf("\n");
}
else printf("-1\n");
}
int main()
{
read();
solve();
write();
return 0;
}