Pagini recente » Cod sursa (job #1103051) | Cod sursa (job #742699) | Cod sursa (job #2657613) | Cod sursa (job #2414513) | Cod sursa (job #2781754)
#include <iostream>
#include <vector>
#include <stack>
#include <fstream>
#define VMAX 100000
#define EMAX 500000
#define NOTFOUND -1
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
int V, E, x, y;
vector <int> adj[VMAX];
int deg[VMAX];
int epath[EMAX], lge;
int cpath[EMAX], lgc;
int getIndex(int src, int dest);
void removeEdge(int src, int dest);
int main()
{
ios_base::sync_with_stdio(false);
fin.tie(0);
fout.tie(0);
fin >> V >> E;
while (E--)
{
fin >> x >> y;
x--, y--;
adj[x].emplace_back(y);
adj[y].emplace_back(x);
deg[x]++, deg[y]++;
}
for (int i = 0; i < V; ++i)
if (deg[i] & 1)
{
fout << -1;
return 0;
}
int u, w;
cpath[lgc++] = 0;
while (lgc)
{
u = cpath[lgc - 1];
if (adj[u].empty())
{
epath[lge++] = u;
lgc--;
}
else
{
w = adj[u][0];
removeEdge(u, w);
cpath[lgc++] = w;
}
}
for (int i = lge - 1; i >= 1; --i)
fout << epath[i] + 1 << " ";
fin.close();
fout.close();
return 0;
}
void removeEdge(int src, int dest)
{
int index = getIndex(src, dest);
if (index != NOTFOUND)
{
E--;
adj[src].erase(adj[src].begin() + index);
adj[dest].erase(adj[dest].begin() + getIndex(dest, src));
}
}
int getIndex(int src, int dest)
{
for (auto it = adj[src].begin(); it != adj[src].end(); ++it)
if (*it == dest) return it - adj[src].begin();
return NOTFOUND;
}