Pagini recente » Cod sursa (job #2375674) | Cod sursa (job #1441323) | Cod sursa (job #850330) | Cod sursa (job #1814504) | Cod sursa (job #2922578)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
const int nmax = 100000;
int n, m;
vector<pair<int, int>> g[nmax + 5];
bool vizitat[5 * nmax + 5];
int grad[nmax + 5];
int euler[5 * nmax + 5], cnt;
int main()
{
fin >> n >> m;
for(int i = 1; i <= m; i ++)
{
int u, v;
fin >> u >> v;
g[u].push_back(make_pair(v, i));
g[v].push_back(make_pair(u, i));
grad[u] ++;
grad[v] ++;
}
bool noCycle = false;
for(int i = 1; i <= n; i ++)
{
if(grad[i] % 2 == 1)
{
noCycle = true;
}
}
if(noCycle == true)
{
fout << -1 << '\n';
return 0;
}
stack<int> stiva;
stiva.push(1);
while(!stiva.empty())
{
int nod = stiva.top();
while(!g[nod].empty() && vizitat[g[nod].back().second] == true)
{
g[nod].pop_back();
}
if(g[nod].empty() == true)
{
euler[++cnt] = nod;
stiva.pop();
}
else
{
stiva.push(g[nod].back().first);
vizitat[g[nod].back().second] = true;
}
}
for(int i = 1; i < cnt; i ++)
{
fout << euler[i] << ' ';
}
fout << '\n';
return 0;
}