Pagini recente » Cod sursa (job #2077681) | Cod sursa (job #2309512) | Cod sursa (job #922121) | Cod sursa (job #2485731) | Cod sursa (job #328581)
Cod sursa(job #328581)
#include<stdio.h>
#include<vector>
#include<stack>
#include<stdlib.h>
#include<list>
using namespace std;
vector <int> ut,par;
list <int> a[100010];
list <int> ::iterator lIt;
vector <int> ::iterator vIt;
stack <int> st;
int n,m,i,j,x,y;
void check()
{
for(i=1;i<=n;i++)
if(par[i]&1)
{
printf("-1\n");
exit (0);
}
st.push(1);
while(!st.empty())
{
x=st.top();st.pop();
for(lIt=a[x].begin();lIt!=a[x].end();lIt++)
if(!ut[*lIt])
{
st.push(*lIt);
ut[*lIt]=1;
}
}
for(i=1;i<=n;i++)
if(!ut[i])
{
printf("-1\n");
exit (0);
}
}
void Del(int x,int y)
{
a[x].pop_front();
for(lIt=a[y].begin();lIt!=a[y].end();lIt++)
if(*lIt==x)
{
a[y].erase(lIt);
return ;
}
}
void euler(int x)
{
while(1)
{
if(a[x].empty())
return ;
y=a[x].front();
st.push(x);
Del(x,y);
x=y;
}
}
int main()
{
freopen("ciclueuler.in","r",stdin);
freopen("ciclueuler.out","w",stdout);
scanf("%d%d",&n,&m);
ut.resize(n+1,0);par.resize(n+1,0);
for(i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
par[x]++;par[y]++;
a[x].push_back(y);
a[y].push_back(x);
}
check();x=1;
do
{
euler(x);
x=st.top();st.pop();
printf("%d ",x);
}while(!st.empty());
printf("\n");
return 0;
}