Pagini recente » Cod sursa (job #1814437) | Cod sursa (job #2898118) | Cod sursa (job #1400429) | Cod sursa (job #1843394) | Cod sursa (job #2838489)
#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() & 1)
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)
{
for(unsigned int i = 0; i < sol.size()-1; i++)
g<<sol[i]<<" ";
g<<'\n';
}
else
return -1;
return 0;
}