Cod sursa(job #1249951)
Utilizator | Data | 27 octombrie 2014 18:05:48 | |
---|---|---|---|
Problema | Ciclu Eulerian | Scor | 100 |
Compilator | cpp | Status | done |
Runda | Arhiva educationala | Marime | 1.66 kb |
#include <cstdio>
#include <vector>
#include <list>
using namespace std;
vector <int> g[100001];
list <int> q;
void euler (int x)
{
vector <int> :: iterator it;
q.push_front(x);
while (!q.empty())
{
x=q.front();
if (g[x].empty())
{
q.pop_front();
if (!q.empty()) printf("%d ",x);
}
else
{
int y=g[x].back();
q.push_front(y);
g[x].pop_back();
for (it=g[y].begin(); it!=g[y].end(); it++)
{
if (*it==x)
{
g[y].erase(it);
break;
}
}
}
}
}
int main()
{
int m, n, 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!=0)
{
printf("-1\n");
return 0;
}
}
euler(1);
fclose(stdin);
fclose(stdout);
return 0;
}