Pagini recente » Cod sursa (job #1739390) | Cod sursa (job #1783809) | Cod sursa (job #908201) | Cod sursa (job #1325400) | Cod sursa (job #2887126)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <iomanip>
#include <algorithm>
using namespace std;
const string filename = "ciclueuler";
ifstream fin(filename + ".in");
ofstream fout(filename + ".out");
const int maxN = 100005;
int n, m;
bool used[5 * maxN];
struct muchie {
int nod, ind;
};
vector <muchie> G[maxN];
vector <int> stiva;
int main()
{
fin >> n >> m;
for(int x, y, i = 1; i <= m; i++)
{
fin >> x >> y;
G[x].push_back({y, i});
G[y].push_back({x, i});
}
bool ok = 1;
for(int i = 1; i <= n; i++)
if(G[i].size() % 2 == 1)
ok = 0;
if(!ok)
{
fout << -1;
return 0;
}
stiva.push_back(1);
for(int i = 1; i <= m; i++)
{
int nod = stiva.back();
while(!G[nod].empty())
{
muchie nxt = G[nod].back();
G[nod].pop_back();
if(used[nxt.ind])
continue;
used[nxt.ind] = 1;
nod = nxt.nod;
stiva.push_back(nod);
}
fout << nod << ' ';
stiva.pop_back();
}
return 0;
}