Pagini recente » Cod sursa (job #1375258) | Cod sursa (job #910508) | Cod sursa (job #1915713) | Cod sursa (job #2157263) | Cod sursa (job #2837877)
#include <iostream>
#include <fstream>
#include <unordered_map>
#include <unordered_set>
#include <algorithm>
#include <stack>
#define maxi 100000
using namespace std;
ifstream f;
ofstream g;
unordered_map<int,unordered_multiset<int>> M;
unordered_multiset<int>::iterator I,I2;
stack<int> stiva;
int n,m,a,b,d[maxi+10];
bool found,viz[maxi+10];
void read()
{
f.open("ciclueuler.in",ios::in);
f>>n>>m;
for(int i=1;i<=m;i++)
{
f>>a>>b;
d[a]++;
d[b]++;
M[a].insert(b);
M[b].insert(a);
}
f.close();
return;
}
bool grade_pare()
{
for(int i=1;i<=n;i++)
if(d[i]%2)
return false;
return true;
}
void dfs(int x)
{
viz[x]=true;
for(auto a:M[x])
{
if(!viz[a])
dfs(a);
}
}
void euler(int x)
{
stiva.push(x);
while(!stiva.empty())
{
int sursa=stiva.top();
if(M[sursa].size())
{
I=M[sursa].begin();
int vecin=*I;
M[sursa].erase(I);
I2=M[vecin].find(sursa);
M[vecin].erase(I2);
stiva.push(vecin);
}
else{
if(stiva.top()==1&&!found)
{
found=true;
g<<stiva.top()<<" ";
}
else if(stiva.top()!=1)
g<<stiva.top()<<" ";
stiva.pop();
}
}
return;
}
bool este_eulerian()
{
dfs(1);
for(int i=1;i<=n;i++)
if(!viz[i])
return false;
return grade_pare();
}
int main()
{
read();
g.open("ciclueuler.out",ios::out);
if(este_eulerian())
euler(1);
else g<<-1;
g.close();
return 0;
}