Pagini recente » Cod sursa (job #2046286) | Cod sursa (job #1252885) | Cod sursa (job #336827) | Cod sursa (job #1465002) | Cod sursa (job #1790918)
#include <cstdio>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
const int Nmax = 100005;
int N,M;
vector<int> G[ Nmax ],answer;
stack <int> S;
#define DIM 10000
char buff[DIM];
int poz=0;
void scan(int &numar)
{
numar = 0;
//cat timp caracterul din buffer nu e cifra ignor
while (buff[poz] < '0' || buff[poz] > '9')
//daca am "golit" bufferul atunci il umplu
if (++poz == DIM)
fread(buff,1,DIM,stdin),poz=0;
//cat timp dau de o cifra recalculez numarul
while ('0'<=buff[poz] && buff[poz]<='9')
{
numar = numar*10 + buff[poz] - '0';
if (++poz == DIM)
fread(buff,1,DIM,stdin),poz=0;
}
}
inline int eulerian(){
for(int i = 1; i <= N; ++i)
if(G[i].size() & 1)return 0;
return 1;
}
inline void read()
{
scan(N); scan(M);
int a,b;
for(int i=1;i<=M;++i)
{
scan(a); scan(b);
G[a].push_back(b);
G[b].push_back(a);
}
}
inline void DFS(int nodc) // dfs iterativ
{
S.push(nodc);
int y;
while(!S.empty())
{
nodc = S.top();
if(!G[nodc].size()) // ne intoarcem in stiva
{
S.pop();
answer.push_back(nodc);
}
else
{
y = G[nodc].back();
G[nodc].pop_back();
S.push(y);
G[y].erase(find(G[y].begin(),G[y].end(),nodc));
}
}
}
int main()
{
freopen("ciclueuler.in","r",stdin);
freopen("ciclueuler.out","w",stdout);
read();
if(eulerian())
{
DFS(1);
for(int i = 0; i < answer.size() ;++i)
printf("%d ",answer[i]);
}
else
printf("-1");
return 0;
}