Cod sursa(job #1372524)
Utilizator | Data | 4 martie 2015 13:59:39 | |
---|---|---|---|
Problema | Ciclu Eulerian | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 1.73 kb |
#include <cstdio>
#include <vector>
#include <list>
using namespace std;
vector <int> g[100001];
vector <int> sol;
vector <int> :: iterator it;
list <int> q;
void euler (void)
{
int nod, y; q.push_back(1);
while (!q.empty())
{
nod=q.back();
if (!g[nod].empty())
{
y=g[nod].back();
q.push_back(y);
g[nod].pop_back();
for (it=g[y].begin(); it!=g[y].end(); it++)
{
if (*it==nod)
{
g[y].erase(it);
break;
}
}
}
else
{
sol.push_back(nod);
q.pop_back();
}
}
}
int main()
{
int n, m, i, x, y;
freopen("ciclueuler.in","r",stdin);
freopen("ciclueuler.out","w",stdout);
scanf("%d%d",&n,&m);
for (i=1; i<=m; i++)
{
scanf("%d%d",&x,&y);
g[x].push_back(y);
g[y].push_back(x);
}
for (i=1; i<=n; i++)
{
if (g[i].size()%2==1)
{
printf("-1");
return 0;
}
}
euler();
for (i=0; i<sol.size(); i++)
{
printf("%d ",sol[i]);
}
return 0;
}