Pagini recente » Cod sursa (job #2511785) | Cod sursa (job #2301494) | Cod sursa (job #2453665) | Cod sursa (job #1690217) | Cod sursa (job #2374102)
#include <iostream>
#include <fstream>
#include <vector>
#include <stack>
#include <bitset>
#define pb push_back
using namespace std;
ifstream f("ciclueuler.in");
ofstream g("ciclueuler.out");
int n, m, st, fn;
stack<int>s;
bitset<500005>viz;
vector<int>graph[100005];
vector<int>rez;
vector<pair<int,int> >muchii;
void euler(int ind)
{
s.push(ind);
while (!s.empty())
{
int nod=s.top();
if (!graph[nod].size())
{
rez.push_back(nod);
s.pop();
}
else
{
int ultim=graph[nod].back();
if (!viz[ultim])
{
pair<int,int>muchie=muchii[ultim];
viz[ultim]=true;
if (muchie.first!=nod)
{
s.push(muchie.first);
}
else
{
s.push(muchie.second);
}
}
graph[nod].pop_back();
}
}
}
int main()
{
f >> n >> m;
for (int i=0; i<m; ++i)
{
f >> st >> fn;
muchii.pb({st,fn});
graph[st].pb(i);
graph[fn].pb(i);
}
euler(1);
int lung=rez.size();
for (int i=0; i<lung-1; ++i)
{
g << rez[i] <<' ';
}
return 0;
}