Pagini recente » Cod sursa (job #256336) | Cod sursa (job #1393088) | Cod sursa (job #3292609) | Cod sursa (job #2241270) | Cod sursa (job #2064498)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
const int NMAX = 100000 + 5;
const int MMAX = 500000 + 5;
queue <int> ans;
vector <int> graph[MMAX];
int n, m;
int st[MMAX], dr[MMAX];
bool vis[MMAX];
bool vis2[MMAX];
void read()
{
fin >> n >> m;
for (int i = 1; i <= m; ++i)
{
fin >> st[i] >> dr[i];
graph[st[i]].push_back(i);
graph[dr[i]].push_back(i);
}
}
void dfs(int nod)
{
vis2[nod] = true;
for (int i = 0; i < graph[nod].size(); ++i)
{
if (!vis[graph[nod][i]])
{
vis[graph[nod][i]] = true;
dfs(st[graph[nod][i]] + dr[graph[nod][i]] - nod);
}
}
ans.push(nod);
}
bool check()
{
for (int i = 1; i <= m; ++i)
if (!vis[i])
return 0;
for (int i = 1; i <= n; ++i)
if (!vis2[i])
return 0;
return 1;
}
void write()
{
ans.pop();
while (!ans.empty())
{
fout << ans.front() << " ";
ans.pop();
}
}
bool euler()
{
for (int i = 1; i <= n; ++i)
if (graph[i].size() % 2)
return 0;
return 1;
}
int main()
{
read();
if (euler())
dfs(1);
else
{
fout << -1;
return 0;
}
if (check())
write();
else
fout << -1;
return 0;
}