Pagini recente » Cod sursa (job #2043232) | Cod sursa (job #2023498) | Cod sursa (job #2913645) | Istoria paginii problema/produse2 | Cod sursa (job #2259705)
#include <iostream>
#include <fstream>
#include <vector>
#include <bitset>
#include <stack>
#define MAX 100010
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
//#define cout g
vector<int> G[MAX];
bitset<MAX> viz;
vector<int> sol;
stack<int> s;
void dfs(int node)
{
if (viz[node] == 1)
{
return;
}
viz[node] = 1;
for (size_t i = 0; i < G[node].size(); ++i)
{
dfs(G[node][i]);
}
}
void euler(int node_arg)
{
s.push(node_arg);
while (!s.empty())
{
here:
int node = s.top();
while (!G[node].empty())
{
int nnode = G[node][G[node].size() - 1];
G[node].pop_back();
for (size_t i = 0; i < G[nnode].size(); ++i)
{
if (G[nnode][i] == node)
{
G[nnode][i] = G[nnode][G[nnode].size() - 1];
G[nnode].pop_back();
break;
}
}
s.push(nnode);
goto here;
}
sol.push_back(node);
s.pop();
}
}
int main()
{
int n, m;
f >> n >> m;
for (int i = 0; i < m; ++i)
{
int x, y;
f >> x >> y;
G[x].push_back(y);
G[y].push_back(x);
}
bool eulerian = true;
for (int i = 0; i < n; ++i)
{
if (G[i].size() % 2 != 0)
{
eulerian = false;
}
}
dfs(1);
for (int i = 1; i <= n; ++i)
{
if (viz[i] == 0)
{
eulerian = false;
}
}
if (eulerian)
{
euler(1);
for (int i = 0; i < sol.size() - 1; ++i)
{
cout << sol[i] << ' ';
}
}
else
{
cout << -1 << endl;
}
return 0;
}