Pagini recente » Cod sursa (job #1484217) | Cod sursa (job #104624) | Cod sursa (job #2339433) | Cod sursa (job #2026289) | Cod sursa (job #1143128)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in ("ciclueuler.in");
ofstream out ("ciclueuler.out");
const int N = 100009;
struct Muchie {
int first;
int second;
};
Muchie getMuchie (int x, int y)
{
Muchie m;
m.first = x;
m.second = y;
return m;
}
int noduri, muchii;
vector <int> indici[N*5];
bool folosit[N*5];
vector <int> ciclu;
bool vizitat[N*5];
Muchie v[N*5];
queue <int> coada;
void read()
{
in >> noduri >> muchii;
for (int i = 1; i <= muchii; ++i) {
int x, y;
in >> y >> x;
indici[x].push_back(i);
indici[y].push_back(i);
v[i] = getMuchie(x, y);
}
}
inline int varf (int nod, int valoare)
{
if (v[nod].first == valoare) {
return v[nod].second;
}
return v[nod].first;
}
inline void euler (int x)
{
for (vector<int>::iterator it = indici[x].begin(); it < indici[x].end(); ++it) {
int indice = *it;
if (!folosit[indice]) {
folosit[indice] = true;
//out << "nod = " << indice << "\n";
//out << "valoare = " << x << "\n\n";
int y = varf(indice, x);
euler(y);
}
}
ciclu.push_back(x);
}
void bfs (int sursa)
{
coada.push(sursa);
while(!coada.empty()) {
int curent = coada.front();
coada.pop();
for (vector<int>::iterator it = indici[curent].begin(); it < indici[curent].end(); ++it) {
int fiu = *it;
if (!vizitat[fiu]) {
vizitat[fiu] = true;
coada.push(fiu);
}
}
}
}
void dfs (int x)
{
vizitat[x] = true;
//out << "x = " << x << "\n";
for (vector<int>::iterator it = indici[x].begin(); it < indici[x].end(); ++it) {
int y = varf(*it, x);
//out << "y = " << y << "\n\n";
if (!vizitat[y]) {
dfs(y);
}
}
}
bool check ()
{
dfs(1);
//bfs(1);
//out << "Sunt in check\n";
for (int i = 1; i <= noduri; ++i) {
if (!vizitat[i] or indici[i].size() % 2) {
return false;
}
}
return true;
}
void solve ()
{
//modificaStiva();
bool ok = check();
//out << "ok = " << ok << "\n";
if (!ok) {
out << "-1\n";
return;
}
euler(1);
//out << elementeCiclu << "\n";
//out << "size = " << ciclu.size() << "\n";
if (ciclu.size() == muchii + 1) {
for (int i = 0; i < muchii; ++i) {
out << ciclu[i] << " ";
}
out << "\n";
} else {
out << "-1\n";
}
}
int main()
{
read();
solve();
return 0;
}