Pagini recente » Cod sursa (job #1373714) | Cod sursa (job #1131271) | Monitorul de evaluare | simulare_de_oni_5 | Cod sursa (job #971828)
Cod sursa(job #971828)
#include <vector>
#include <fstream>
#include <set>
#include <queue>
using namespace std;
ifstream fin ("ciclueuler.in");
ofstream fout ("ciclueuler.out");
vector <bool> viz;
vector <multiset <int> > g;
vector <int> st;
queue <int> Q;
int n, m;
void bfs(int x) {
viz[x] = 1;
Q.push(x);
while (Q.size()) {
x = Q.front();
Q.pop();
for (multiset <int> :: iterator it = g[x].begin(); it != g[x].end(); ++it)
if (!viz[*it]) {
Q.push(*it);
viz[*it] = 1;
}
}
}
bool verif() {
for (int i = 1; i <= n; ++i)
if (g[i].size() & 1 || (!viz[i] && g[i].size()))
return 0;
return 1;
}
int main() {
fin >> n >> m;
viz.resize(n+1);
g.resize(n+1);
while (m--) {
int x, y;
fin >> x >> y;
g[x].insert(y);
g[y].insert(x);
}
int x = 0;
for (int i = 1; i <= n && !x; ++i)
x = (g[i].size()) ? i : 0;
bfs(x);
if (!verif()) {
fout << -1;
return 0;
}
st.push_back(*g[x].begin());
int y = st.back();
g[x].erase(g[x].begin());
g[y].erase(x);
while (st.size()) {
int now = st.back();
if (!g[now].size()) {
fout << now << " ";
st.pop_back();
continue;
}
int y = *g[now].begin();
g[now].erase(g[now].begin());
g[y].erase(g[y].find(now));
st.push_back(y);
}
fout.close();
}