Pagini recente » Cod sursa (job #3331752) | Cod sursa (job #3331822) | Cod sursa (job #3331749) | Cod sursa (job #3341878) | Cod sursa (job #3341859)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
const int NMAX = 1e5 + 1;
int n, m;
bool viz[5 * NMAX];
vector<pair<int, int>> adj[NMAX];
vector<int> ciclu;
bool grad_impar()
{
bool check = false;
for(int node = 1; node <= n; node++)
check |= (adj[node].size() & 1);
return check;
}
bool all_used()
{
bool check = true;
for(int edge = 1; edge <= m; edge++)
check &= viz[edge];
return check;
}
void dfs(int node)
{
for(auto [next, id] : adj[node])
{
if(viz[id])
continue;
viz[id] = true;
dfs(next);
}
ciclu.push_back(node);
}
int main()
{
fin >> n >> m;
for(int i = 1; i <= m; i++)
{
int u, v;
fin >> u >> v;
adj[u].emplace_back(v, i);
adj[v].emplace_back(u, i);
}
if(grad_impar())
{
fout << -1;
return 0;
}
dfs(1);
if(all_used())
{
ciclu.pop_back();
for(int node : ciclu)
fout << node << " ";
}
else
fout << -1;
fin.close();
fout.close();
return 0;
}