Pagini recente » Cod sursa (job #137551) | Cod sursa (job #2105547) | Cod sursa (job #2275700) | Cod sursa (job #1273702) | Cod sursa (job #784064)
Cod sursa(job #784064)
#include <iostream>
#include <fstream>
#include <list>
#include <stack>
#include <queue>
using namespace std;
ifstream in ("ciclueuler.in");
ofstream out ("ciclueuler.out");
const int MAXN = 100010;
list <int> Graf[MAXN], Sol;
stack <int> S;
int N, M, Deg[MAXN];
bool Viz[MAXN];
/*inline void BFS (int nod)
{
list <int> :: iterator it;
queue <int> Q;
Q.push (nod);
Viz[nod] = 1;
while (!Q.empty ()){
nod = Q.front ();
Q.pop ();
for (it = Graf[nod].begin (); it != Graf[nod].end (); ++ it)
if (!Viz[*it]){
Q.push (*it);
Viz[*it] = 1;
}
}
}*/
inline void DFS (int nod)
{
list <int> :: iterator it;
Viz[nod] = 1;
for (it = Graf[nod].begin (); it != Graf[nod].end (); ++ it)
if (!Viz[*it])
DFS (*it);
}
inline bool conex ()
{
int nod;
//BFS (1);
DFS (1);
for (nod = 2; nod <= N; nod ++)
if (!Viz[nod])
return 0;
return 1;
}
inline bool eulerian ()
{
int nod;
if (!conex ())
return 0;
for (nod = 1; nod <= N; nod ++)
if (Deg[nod] & 1)
return 0;
return 1;
}
inline void sterge (int S, int D)
{
list <int> :: iterator it;
-- Deg[S], -- Deg[D];
Graf[S].pop_front ();
for (it = Graf[D].begin (); it != Graf[D].end (); ++ it)
if (*it == S){
Graf[D].erase (it);
return;
}
}
inline void euler (int nod)
{
int nod2;
while (1){
if (Graf[nod].empty ())
return;
nod2 = Graf[nod].front ();
S.push (nod);
sterge (nod, nod2);
nod = nod2;
}
}
inline bool rez ()
{
int nod = eulerian ();
if (!nod)
return 0;
do{
euler (nod);
nod = S.top ();
S.pop ();
Sol.push_front (nod);
}while (!S.empty ());
return 1;
}
int main ()
{
list <int> :: iterator it;
int x, y;
in >> N >> M;
while (M --){
in >> x >> y;
Graf[x].push_back (y), ++ Deg[x];
Graf[y].push_back (x), ++ Deg[y];
}
if (!rez ()){
out << -1;
return 0;
}
for (it = Sol.begin (); it != Sol.end (); ++ it)
out << *it << " ";
return 0;
}