Pagini recente » Cod sursa (job #1637533) | Cod sursa (job #1999488) | Cod sursa (job #72576) | Cod sursa (job #397979) | Cod sursa (job #651718)
Cod sursa(job #651718)
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
const char InFile[]="ciclueuler.in";
const char OutFile[]="ciclueuler.out";
const int MaxN=100111;
const int MaxM=500111;
ifstream fin(InFile);
ofstream fout(OutFile);
struct Edge
{
Edge(int to=0, int index=0):to(to),index(index){}
int to,index;
};
int N,M,x,y,viz[MaxM],D[MaxN],vizN[MaxN];
vector<Edge> G[MaxN];
vector<int> sol;
stack<int> S;
void DFS(int nod)
{
vizN[nod]=1;
for(vector<Edge>::iterator it=G[nod].begin();it!=G[nod].end();++it)
{
if(!vizN[it->to])
{
DFS(it->to);
}
}
}
inline bool Euler()
{
for(register int i=1;i<=N;++i)
{
if(D[i]&1)
{
return false;
}
}
DFS(1);
for(register int i=2;i<=N;++i)
{
if(!vizN[i] && D[i]>0)
{
return false;
}
}
S.push(1);
while(!S.empty())
{
int nod=S.top();
if(G[nod].size()==0)
{
S.pop();
sol.push_back(nod);
}
else
{
Edge e=G[nod][G[nod].size()-1];
if(!viz[e.index])
{
S.push(e.to);
viz[e.index]=1;
}
G[nod].pop_back();
}
}
return true;
}
int main()
{
fin>>N>>M;
for(register int i=1;i<=M;++i)
{
fin>>x>>y;
G[x].push_back(Edge(y,i));
G[y].push_back(Edge(x,i));
++D[x];
++D[y];
}
fin.close();
if(!Euler())
{
fout<<"-1";
}
else
{
vector<int>::iterator last=sol.end();
--last;
for(vector<int>::iterator it=sol.begin();it!=last;++it)
{
fout<<*it<<" ";
}
}
fout.close();
return 0;
}