Pagini recente » Cod sursa (job #2323945) | Cod sursa (job #2074786) | Cod sursa (job #162378) | Cod sursa (job #1930921) | Cod sursa (job #582196)
Cod sursa(job #582196)
#include <cstdio>
#include <vector>
#include <stack>
using namespace std;
#define nmax 100010
#define mmax 500010
#define f first
#define s second
vector <pair<int, int> > g[nmax];
stack <int> s, mc, t;
int n, m, a[mmax], l;
char u[mmax];
int df_verif()
{
int nod, i, v;
s.push(1);
mc.push(0);
while (!s.empty())
{
if (!u[mc.top()])
{
nod=s.top();
u[mc.top()]=1;
s.pop();
mc.pop();
for (i=0; i<g[nod].size(); i++)
if (!u[g[nod][i].s])
{
v=g[nod][i].f;
s.push(v);
mc.push(g[nod][i].s);
}
} else
{
s.pop();
mc.pop();
}
}
for (i=1; i<=m; i++)
if (!u[i]) return 0;
return 1;
}
void df()
{
int nod, v, i, prec=0;
s.push(1);
mc.push(0);
t.push(0);
while (!s.empty())
{
if (!u[mc.top()])
{
nod=s.top();
u[mc.top()]=1;
while (t.top()!=prec && l)
{
printf("%d ",a[l]);
l--;
}
a[++l]=nod;
prec=nod;
s.pop();
t.pop();
mc.pop();
for (i=0; i<g[nod].size(); i++)
if (!u[g[nod][i].s])
{
v=g[nod][i].f;
s.push(v);
t.push(nod);
mc.push(g[nod][i].s);
}
} else
{
s.pop();
t.pop();
mc.pop();
}
}
}
int main()
{
freopen("ciclueuler.in","r",stdin);
freopen("ciclueuler.out","w",stdout);
scanf("%d %d", &n, &m);
int i, x, y;
for (i=1; i<=m; i++)
{
scanf("%d %d", &x, &y);
g[x].push_back(make_pair(y,i));
g[y].push_back(make_pair(x,i));
}
/*if (!df_verif())
{
printf("-1\n");
return 0;
}*/
for (i=0; i<=m; i++) u[i]=0;
l=0;
df();
while (l>1)
{
printf("%d ",a[l]);
l--;
}
printf("\n");
return 0;
}