Pagini recente » Cod sursa (job #2493484) | Cod sursa (job #762935) | Cod sursa (job #1839826) | Cod sursa (job #2188402) | Cod sursa (job #328574)
Cod sursa(job #328574)
#include<stdio.h>
#include<vector>
#include<stack>
#include<stdlib.h>
#include<list>
using namespace std;
vector <int> ut,par,Rez;
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(y);
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();
Rez.push_back(x);
}while(!st.empty());
for(vIt=Rez.begin();vIt!=Rez.end();vIt++)
printf("%d ",*vIt);
printf("\n");
return 0;
}