Pagini recente » Cod sursa (job #1034323) | Cod sursa (job #1705557) | Cod sursa (job #2234438) | Cod sursa (job #2196431) | Cod sursa (job #2681324)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
vector<pair <int, int> > nodes[100005];
int ciclu[500005], n, m, p, q, k;
bool viz[500005];
void euler(int nod)
{
while (!nodes[nod].empty())
{
p = nodes[nod].back().second;
q = nodes[nod].back().first;
nodes[nod].pop_back();
if (!viz[p])
{
viz[p] = 1;
euler(q);
}
}
ciclu[++k] = nod;
}
int main()
{
fin >> n >> m;
for (int i = 1; i <= m; ++i)
{
int x,y;
fin >> x >> y;
nodes[x].push_back({ y, i });
nodes[y].push_back({ x, i });
}
int ok = 1;
for (int i = 1; i <= n; i++)
if (nodes[i].size() % 2 == 1)
{
fout << -1;
ok = 0;
break;
}
if(ok == 1)
{
euler(1);
for (int i = 1; i < k; i++)
fout << ciclu[i] << ' ';
}
return 0;
}