Pagini recente » Cod sursa (job #1524152) | Cod sursa (job #1757780) | Cod sursa (job #2531929) | Cod sursa (job #871221) | Cod sursa (job #2162309)
#include <fstream>
#include <vector>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
int n, m;
bool grd[100001], b[100001], M[100001], viz[100001];
vector<pair<int, int> > adj[100001];
void dfs(int nod)
{
b[nod] = 1;
for (int i = 0; i < adj[nod].size(); i++)
if (!b[adj[nod][i].first])
{
M[adj[nod][i].second] = 1;
dfs(adj[nod][i].first);
}
}
void euler(int nod)
{
while (adj[nod].size())
{
fout << nod << ' ';
bool ok = true;
for (int i = 0; i < adj[nod].size() && ok; i++)
{
int next = adj[nod][i].first, ind = adj[nod][i].second;
if (!viz[ind] && !M[ind])
{
viz[ind] = 1;
nod = next;
ok = false;
}
}
while (adj[nod].size() && ok)
{
int next = adj[nod].back().first, ind = adj[nod].back().second;
adj[nod].pop_back();
if (!viz[ind])
{
viz[ind] = 1;
nod = next;
ok = false;
}
}
}
}
int main()
{
int x, y;
fin >> n >> m;
for (int i = 1; i <= m; i++)
{
fin >> x >> y;
adj[x].push_back(make_pair(y, i));
if (x != y)
adj[y].push_back(make_pair(x, i));
grd[x] = !grd[x];
grd[y] = !grd[y];
}
for (int i = 1; i <= n; i++)
if (grd[i])
{
fout << -1;
return 0;
}
dfs(1);
euler(1);
return 0;
}