Pagini recente » Cod sursa (job #1374624) | Cod sursa (job #750499) | Cod sursa (job #2690448) | Cod sursa (job #326568) | Cod sursa (job #969340)
Cod sursa(job #969340)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <list>
#define N 100005
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
int n, m;
vector < list <int> > a(N);
vector <int> gr(N, 0);
vector <bool> viz(N, 0);
queue <int> q;
stack <int> stk;
void bfs()
{
q.push(1); viz[1] = 1;
while(!q.empty())
{
int x = q.front(); q.pop();
for( list <int> :: iterator it = a[x].begin(); it != a[x].end(); it++)
if(!viz[*it])
{
viz[*it] = 1;
q.push(*it);
}
}
}
bool ok()
{
for(int i=1; i<=n; i++)
if(gr[i] & 1 || (!viz[i] && gr[i]))
return 0;
return 1;
}
void Euler()
{
stk.push(1);
while(!stk.empty())
{
int x = stk.top();
if(!a[x].size())
{
stk.pop();
fout<<x<<' ';
}
else
{
int y = *a[x].begin();
a[x].erase(a[x].begin());
list <int> :: iterator it = a[y].begin();
for(; it != a[y].end() && *it != x; it++);
a[y].erase(it);
stk.push(y);
}
}
}
int main()
{
fin>>n>>m;
while(m--)
{
int x, y;
fin>>x>>y;
if(x != y)
gr[x]++, gr[y]++;
a[x].push_back(y);
a[y].push_back(x);
}
bfs();
if(ok()) Euler();
else fout<<(-1);
return 0;
}