Pagini recente » Cod sursa (job #2889028) | Cod sursa (job #2692859) | Cod sursa (job #2084398) | Cod sursa (job #1459485) | Cod sursa (job #2331762)
#include <fstream>
#include <vector>
#include <algorithm>
#include <stack>
#define input "ciclueuler.in"
#define output "ciclueuler.out"
#define NMAX 100005
using namespace std;
ifstream in(input);
ofstream out(output);
struct p
{
int x, y;
};
vector < int > muchii[NMAX];
vector < int > sol;
stack < int > stiva;
int N, M;
bool uz[5 * NMAX];
bool Sort_Method(p a, p b)
{
if (a.x != b.x) return a.x < b.x;
return a.y < b.y;
}
void Read_Data()
{
in >> N >> M;
for (int i = 1; i <= M; i++)
{
int x, y;
in >> x >> y;
muchii[x].push_back(y);
muchii[y].push_back(x);
}
for (int i = 1; i <= N; i++)
sort(muchii[i].begin(), muchii[i].end());
}
int B_S(int nod, int v)
{
int st = 0, dr = muchii[nod].size() - 1, poz = -1;
while (st <= dr)
{
int mid = (st + dr) / 2;
if (muchii[nod][mid] == v)
{
poz = mid; break;
}
else if (muchii[nod][mid] > v) dr = mid - 1;
else st = mid + 1;
}
return poz;
}
void Solve()
{
stiva.push(1);
while (!stiva.empty())
{
int nod = stiva.top();
out << nod << "\n";
if (muchii[nod].size())
{
int new_nod = muchii[nod].back();
muchii[nod].pop_back();
// Find nod in new_nod muchii
int poz = B_S(new_nod, nod);
muchii[new_nod].erase(muchii[new_nod].begin() + poz);
stiva.push(new_nod);
}
else
{
stiva.pop();
sol.push_back(nod);
}
}
for (unsigned i = 0; i < sol.size() - 1; i++)
out << sol[i] << " ";
}
int main()
{
Read_Data();
Solve();
return 0;
}