Pagini recente » Cod sursa (job #337442) | Cod sursa (job #2569070) | Cod sursa (job #1704645) | Cod sursa (job #42576) | Cod sursa (job #2643589)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#include <limits.h>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
const int N = 100005;
const int M = 500005;
// <destinatie, identificator muchie>
vector<pair<int, int>> g[N];
vector<int> ord;
bool viz[M];
int n, m;
void dfs(int nod)
{
stack<int> st;
st.push(nod);
while(!st.empty())
{
int nod = st.top();
st.pop();
// am consumat toate muchiile din nod
if(g[nod].size() == 0)
ord.push_back(nod);
else
{
int dest = g[nod].back().first;
int id = g[nod].back().second;
g[nod].pop_back();
st.push(nod);
if(!viz[id])
{
viz[id] = true;
st.push(dest);
}
}
}
}
int main()
{
fin >> n >> m;
for(int i = 0; i < m; i++)
{
int x, y;
fin >> x >> y;
g[x].push_back({y, i});
g[y].push_back({x, i});
}
// verificare
for(int i = 1; i <= n; i++)
if(g[i].size() % 2)
{
fout << -1;
return 0;
}
dfs(1);
ord.pop_back();
for(int x : ord)
fout << x << ' ';
return 0;
}