Pagini recente » Cod sursa (job #908718) | Cod sursa (job #336715) | Borderou de evaluare (job #2629766) | Cod sursa (job #2913688) | Cod sursa (job #3311801)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
const int NMAX = 1e5;
const int MMAX = 5e5;
struct str{
int first;
int second;
};
int n, m, x, y;
vector <str> graf[NMAX+1];
vector <int> st;
int grad[NMAX+1];
int trecut[MMAX+1];
int ans[MMAX+1];
int main()
{
fin >> n >> m;
for(int i=1; i<=m; i++)
{
fin >> x >> y;
graf[x].push_back( {y,i} );
graf[y].push_back( {x,i} );
grad[x]++; grad[y]++;
}
for(int i=1; i<=n; i++)
if(grad[i] % 2)
{
fout << -1;
return 0;
}
st.push_back(1);
int cnt = 0;
while(!st.empty())
{
int gol = 1;
int nod = st.back();
if(!graf[nod].empty())
{
str a = graf[nod].back();
if(!trecut[a.second])
{
st.push_back(a.first);
trecut[a.second] = 1;
}
graf[nod].pop_back();
}
else
gol = 1;
if(!st.empty() && gol == 1)
ans[++cnt] = st.back(), st.pop_back();
}
if(cnt-1 != m)
{
fout << -1;
return 0;
}
for(int i=1; i<cnt; i++)
fout << ans[i] << ' ';
return 0;
}