Pagini recente » Cod sursa (job #106700) | Cod sursa (job #2548560) | Cod sursa (job #1681429) | Cod sursa (job #878201) | Cod sursa (job #1156124)
#include <fstream>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
int N, M;
vector<pair<int, int> > V[100001];
stack<int> ST;
bool instack[100001], used[500002];
int is_euler(int nod)
{
ST.push(nod);
instack[nod] = true;
while (!ST.empty())
{
int now = ST.top();
ST.pop();
for (vector<pair<int, int> >::iterator it = V[now].begin(); it != V[now].end(); ++it)
{
if (!instack[it->first])
{
ST.push(it->first);
instack[it->first] = true;
}
}
}
int ok = 1;
for (int i = 1; i <= N; ++i)
{
if (V[i].size() % 2 == 1 || !instack[i])
{
ok = 0;
break;
}
}
return ok;
}
void solve_euler(int nod)
{
ST.push(nod);
while (!ST.empty())
{
int now = ST.top();
bool ok = true;
while (V[now].size() > 0)
{
pair<int, int> neighbour = V[now].back();
V[now].pop_back();
if (!used[neighbour.second])
{
used[neighbour.second] = true;
ST.push(neighbour.first);
ok = false;
break;
}
}
if (ok)
{
fout << now << ' ';
ST.pop();
}
}
}
int main()
{
fin >> N >> M;
for (int i = 1, nod1, nod2; i <= M; ++i)
{
fin >> nod1 >> nod2;
V[nod1].push_back(make_pair(nod2, i));
V[nod2].push_back(make_pair(nod1, i));
}
if (!is_euler(1))
{
fout << -1 << '\n';
fin.close();
fout.close();
return 0;
}
solve_euler(1);
fin.close();
fout.close();
return 0;
}