Pagini recente » Cod sursa (job #2800393) | Cod sursa (job #2957262) | Cod sursa (job #2529244) | Cod sursa (job #2158861) | Cod sursa (job #2349475)
#include <bits/stdc++.h>
using namespace std;
const int NMAX = 100002;
vector < pair < int , int > > v[NMAX];
vector < int > sol;
queue < int > q;
bitset < NMAX > mark, del;
stack < int > st;
int nr[NMAX];
int n, m;
void bfs(int st)
{
mark[st] = 1;
q.push(st);
while(!q.empty())
{
int nod = q.front();
q.pop();
for(int i = 0; i < v[nod].size(); i++)
if(!mark[v[nod][i].first])
mark[v[nod][i].first] = 1, q.push(v[nod][i].first);
}
}
bool isEuler()
{
bfs(1);
for(int i = 1; i <= n; i++)
if(!mark[i] || v[i].size() % 2)
return 0;
return 1;
}
int main()
{
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
f >> n >> m;
for(int i = 1, x, y; i <= m; i++)
{
f >> x >> y;
v[x].push_back({y, i});
v[y].push_back({x, i});
}
if(isEuler())
{
for(int i = 1; i <= n; i++)
nr[i] = v[i].size();
st.push(1);
while(!st.empty())
{
int x = st.top();
if(v[x].size())
{
int y = v[x][v[x].size() - 1].first, ind = v[x][v[x].size() - 1].second;
if(!del[ind])
{
del[ind] = 1;
st.push(y);
}
v[x].pop_back();
}
else
{
sol.push_back(x);
st.pop();
}
}
for(int i = 0; i < sol.size() - 1; i++)
g << sol[i] << " ";
}
else
g << -1;
g << '\n';
f.close();
g.close();
return 0;
}