Pagini recente » Cod sursa (job #2001975) | Cod sursa (job #837262) | Cod sursa (job #2077732) | Cod sursa (job #2282483) | Cod sursa (job #2844994)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#define N 100000
#define M 500000
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
int n, m;
int x, y;
vector <int> answer;
vector <pair <int, int> > g[N+5];
stack <int> st;
int f[M+5];
void citire()
{
fin >> n >> m;
for(int i = 1; i <= m; ++i)
{
fin >> x >> y;
g[x].push_back({y, i});
g[y].push_back({x, i});
}
}
int main()
{
citire();
for(int i = 1; i <= n; ++i)
if(g[i].size() % 2 == 1)
{
fout << -1;
return 0;
}
st.push(1);
while(!st.empty())
{
int nod = st.top();
int im = g[nod][g[nod].size() - 1].second;
if(g[nod].size())
{
if(f[im] == 0)
{
f[im] = 1;
st.push(g[nod][g[nod].size() - 1].first);
}
g[nod].pop_back();
}
else
{
st.pop();
answer.push_back(nod);
}
}
answer.pop_back();
for(int i = 0; i <= answer.size() - 1; ++i)
fout << answer[i] << " ";
return 0;
}