Pagini recente » Cod sursa (job #399) | Cod sursa (job #2559065) | Cod sursa (job #714240) | Cod sursa (job #871711) | Cod sursa (job #2638388)
#include <fstream>
#include <vector>
#include <stack>
using namespace std;
ifstream cin("ciclueuler.in");
ofstream cout("ciclueuler.out");
const int NMAX = 1e5;
const int MMAX = 5e5;
int N, M;
vector <pair<int,int>> g[NMAX + 2];
int deg[NMAX + 2];
bool d[MMAX + 2];
int main()
{
cin >> N >> M;
for(int i = 1; i <= M; i++)
{
int x, y;
cin >> x >> y;
g[x].push_back({y, i});
g[y].push_back({x, i});
deg[x]++, deg[y]++;
}
for(int i = 1; i <= N; i++)
if(deg[i] % 2 == 1)
{
cout << -1;
return 0;
}
vector <int> res;
stack <int> st;
st.push(1);
while(!st.empty())
{
int node = st.top();
while(!g[node].empty() && d[g[node].back().second])
g[node].pop_back();
if(!g[node].empty())
{
st.push(g[node].back().first);
d[g[node].back().second] = true;
g[node].pop_back();
}
else
res.push_back(node), st.pop();
}
for(int i = 1; i < (int)res.size(); i++)
cout << res[i] << ' ';
return 0;
}