Pagini recente » Cod sursa (job #399314) | Cod sursa (job #2877194) | Cod sursa (job #1324887) | Cod sursa (job #3141420) | Cod sursa (job #1647793)
#include <fstream>
#include <list>
#include <stack>
#include <queue>
#define NMAX 100010
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
int n, m, grades[NMAX];
bool vis[NMAX];
list <int> a[NMAX], f;
stack <int> S;
void read();
int check();
void BFS();
void euler(int v);
void deletee(int c, int next);
void solve();
int main()
{
read();
if(check() == 0)
{
fout << -1 << '\n';
return 0;
}
else
{
solve();
while(!f.empty())
{
fout << f.front()<< ' ';
f.pop_front();
}
fout << '\n';
}
return 0;
}
void read()
{
int i, x, y;
fin >> n >> m;
for(i = 1; i <= m; i++)
{
fin >> x >> y;
++grades[x];
a[x].push_back(y);
++grades[y];
if(x != y)
a[y].push_back(x);
}
}
void BFS()
{
queue <int> Q;
int v;
Q.push(1);
vis[1] = 1;
while(!Q.empty())
{
v = Q.front();
Q.pop();
for(std::list<int>::iterator i = a[v].begin(); i != a[v].end(); i++)
if(vis[*i] != 1)
{
Q.push(*i);
vis[*i] = 1;
}
}
}
int check()
{
int i;
BFS();
for(i = 1; i <= n; i++)
if(vis[i] == 0)
return 0;
for(i = 1; i <= n; i++)
if(grades[i] % 2 == 1)
return 0;
return 1;
}
void euler(int v)
{
int c = v, next;
while(!a[c].empty())
{
next = a[c].front();
S.push(c);
deletee(c, next);
c = next;
}
}
void deletee(int c, int next)
{
a[c].pop_front();
for(std::list<int>::iterator i = a[next].begin(); i != a[next].end(); i++)
if(*i == c)
{
a[next].erase(i);
break;
}
}
void solve()
{
int v = 1;
do
{
euler(v);
v = S.top();
S.pop();
f.push_front(v);
}while(!S.empty());
}