Pagini recente » Cod sursa (job #3127235) | Cod sursa (job #477762) | Cod sursa (job #2198391) | Cod sursa (job #3245742) | Cod sursa (job #2838592)
#include <bits/stdc++.h>
#define Dmax 100005
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
struct three{
int first,second; bool third;
};
three M[500005];
vector < int > sol,G[Dmax];
stack < int > S;
int n,m;
bool EsteEuler()
{
for(int i = 1; i <= n; i++)
if(G[i].size() & 1)
return false;
return true;
}
int main()
{
f>>n>>m;
for(int i = 1; i <= m; i++)
{
int x,y;
f>>x>>y;
M[i]={x,y,false};
G[x].push_back(i);
G[y].push_back(i);
}
if(!EsteEuler)
{
g<<"-1\n";
return 0;
}
S.push(1);
while(!S.empty())
{
int nod = S.top();
if(G[nod].empty())
{
sol.push_back(nod);
S.pop();
}
else
{
int muchie = G[nod].back();
G[nod].pop_back();
if(!M[muchie].third)
{
M[muchie].third=true;
if(M[muchie].second == nod)
S.push(M[muchie].first);
else
S.push(M[muchie].second);
}
}
}
if(sol.size() == m+1)
{
for(unsigned int i = 0; i < sol.size() - 1; i++)
g<<sol[i]<<" ";
}
else
{
g<<"-1\n";
return 0;
}
return 0;
}