Pagini recente » Cod sursa (job #1377601) | Cod sursa (job #370441) | Cod sursa (job #2268360) | Cod sursa (job #1433028) | Cod sursa (job #2838486)
#include <bits/stdc++.h>
#define Dmax 100005
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
struct triple{
int first,second;bool third;
};
vector < triple > M;
vector < int > G[Dmax],sol;
stack < int > S;
int n,m;
bool ExistaEuler()
{
for(int i = 1; i <= n; i++)
if(G[i].size() % 2)
return false;
return true;
}
int main()
{
f>>n>>m;
for(int i = 1; i <= m; i++)
{
int x,y;
f>>x>>y;
M.push_back(triple{x,y,false});
G[x].push_back(M.size() - 1);
G[y].push_back(M.size() - 1);
}
if(!ExistaEuler())
return -1;
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)
return -1;
else
{
for(unsigned int i = 0; i < sol.size()-1; i++)
g<<sol[i]<<" ";
}
return 0;
}