Pagini recente » Cod sursa (job #1376429) | Cod sursa (job #51349) | Cod sursa (job #2114814) | Cod sursa (job #1167252) | Cod sursa (job #2064482)
#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[NMAX];
int n, m;
int st[MMAX], dr[MMAX];
bool vis[MMAX];
bool vis2[MMAX];
void read()
{
int x, y;
fin >> n >> m;
for (int i = 1; i <= m; ++i)
{
fin >> x >> y;
st[i] = x; dr[i] = y;
graph[x].push_back(i);
graph[y].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();
}
}
int main()
{
read();
dfs(1);
if (check())
write();
else
fout << -1;
return 0;
}