Pagini recente » Cod sursa (job #2515569) | Cod sursa (job #1663347) | Cod sursa (job #63625) | Cod sursa (job #2216359) | Cod sursa (job #2343168)
#include <fstream>
#include <vector>
#include <algorithm>
#include <bitset>
#include <stack>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
const int NMAX=100000;
bitset<NMAX+5>viz;
vector <int>G[NMAX+5];
int st[500005], top;
void dfs(int nod)
{
int i;
viz[nod]=1;
for(i=0;i<G[nod].size();i++)
{
if(viz[G[nod][i]]==0)
dfs(G[nod][i]);
}
}
void eulerian(int nod)
{
int other;
while(!G[nod].empty())
{
st[++top]=nod;
other=G[nod].back();
G[nod].pop_back();
G[other].erase(find(G[other].begin(), G[other].end(), nod));
nod=other;
}
}
int main()
{
int n, m, i, x, y;
fin>>n>>m;
for(i=1;i<=m;i++)
{
fin>>x>>y;
G[x].push_back(y);
G[y].push_back(x);
}
for(i=1;i<=n;i++)
{
if(G[i].size()%2!=0)
{
fout<<"-1"<<"\n";
return 0;
}
}
dfs(1);
for(i=1;i<=n;i++)
{
if(viz[i]==0)
{
fout<<"-1"<<"\n";
return 0;
}
}
//am verificat daca graful este conex si daca gradele nodurilor sunt pare
int nod=1;
do{
eulerian(nod);
nod=st[top];
fout<<nod<<" ";
top--;
}while(top>0);
return 0;
}