Pagini recente » Cod sursa (job #1439304) | Cod sursa (job #441216) | Cod sursa (job #2905349) | Cod sursa (job #1422180) | Cod sursa (job #383135)
Cod sursa(job #383135)
#include <stdio.h>
#include <vector>
#include <algorithm>
#define max 100010
using namespace std;
vector<pair<int,int> > g[max];
vector<int>::iterator it;
int n,m,i,j,k,top,stack[5*max],deg[max];
char s[5*max];
void dfs(int v)
{
int w,i;
stack[++top]=v;
while(top)
{
w=stack[top];
if(!g[w].empty())
if(!s[g[w].back().second])
{
s[g[w].back().second]=1;
stack[++top]=g[w].back().first;
g[w].pop_back();
}
else g[w].pop_back();
else printf("%d ",stack[top--]);
}
}
int main()
{
freopen("ciclueuler.in","r",stdin);
freopen("ciclueuler.out","w",stdout);
scanf("%d%d",&n,&m);
for(k=1; k<=m; k++)
{
scanf("%d%d",&i,&j);
g[i].push_back(make_pair(j,k));
g[j].push_back(make_pair(i,k));
deg[i]++; deg[j]++;
}
for(i=1; i<=n; i++)
if(deg[i]%2==1) { printf("-1"); return 0;}
dfs(1);
return 0;
}