Pagini recente » Cod sursa (job #1854576) | Cod sursa (job #2969114) | Cod sursa (job #1180484) | Cod sursa (job #2238117) | Cod sursa (job #2143460)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
int n, m, i, x, y, viz[500010];
vector < int > from, to, g[100010], s, sol;
int main()
{
fin >> n >> m;
for(i = 0 ; i < m ; i++)
{
fin >> x >> y;
g[x].push_back(i);
g[y].push_back(i);
from.push_back(x);
to.push_back(y);
}
for(i = 1 ; i <= n ; i++)
if(g[i].size() % 2 == 1)
{
fout << -1;
return 0;
}
s.push_back(1);
while(!s.empty())
{
int nod = s.back();
if(!g[nod].empty())
{
int muchie = g[nod].back();
g[nod].pop_back();
if(viz[muchie] == 0)
{
int nod1 = 0;
if(from[muchie] == nod)
nod1 = to[muchie];
else
nod1 = from[muchie];
s.push_back(nod1);
viz[muchie] = 1;
}
}
else
{
sol.push_back(nod);
s.pop_back();
}
}
for(i = 0; i < sol.size() - 1; i++)
fout << sol[i] << " ";
return 0;
}