Pagini recente » Cod sursa (job #187893) | Cod sursa (job #1260428) | Cod sursa (job #428833) | Cod sursa (job #2238931)
#include <cstdio>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
const int NMAX = 100005;
int n,m;
vector<int>G[NMAX];
stack<int>st;
vector<int>sol;
bool viz[NMAX];
inline void sterge(int nod,int l)
{
vector<int>::iterator it;
it = find(G[l].begin(),G[l].end(),nod);
G[l].erase(it);
}
void euler(int nod)
{
int node,other;
node = nod;
while(G[node].size()>0)
{
st.push(node);
other = G[node][G[node].size()-1];
G[node].pop_back();
sterge(node,other); /// sterge nod de pe linia other
node = other;
}
}
void dfs(int nod)
{
viz[nod] = 1;
for(int i = 0 ; i < G[nod].size() ; i++)
{
int val = G[nod][i];
if(!viz[val])
dfs(val);
}
}
bool verifGrade()
{
for(int i = 1 ; i <= n ; i++)
{
if(G[i].size() % 2 == 1)
return false;
}
return true;
}
bool verifOK()
{
int ct = 0;
for(int i = 1 ; i <= n ; i++)
if(!viz[i])
{
dfs(i);
ct++;
}
if(ct >= 2)
return false;
if(verifGrade() == false)
return false;
return true;
}
int main()
{
freopen("ciclueuler.in","r",stdin);
freopen("ciclueuler.out","w",stdout);
int a , b;
scanf("%d%d",&n,&m);
for(int i = 1 ; i <= m ; i++)
{
scanf("%d%d",&a,&b);
G[a].push_back(b);
G[b].push_back(a);
}
if(verifOK())
{
sol.push_back(1);
euler(1);
while(!st.empty())
{
int nod = st.top();
sol.push_back(nod);
if(G[nod].size()!=0)
euler(nod);
st.pop();
}
for(int i = 0 ; i < sol.size()-1 ; i++)
printf("%d ",sol[i]);
}
else
printf("-1");
return 0;
}