Pagini recente » Cod sursa (job #422862) | Cod sursa (job #2422760) | Cod sursa (job #2635448) | Cod sursa (job #1768172) | Cod sursa (job #2527419)
#include <fstream>
#include <vector>
#include <utility>
#include <iostream>
#define MAX 100000
using namespace std;
vector< pair<int, int> >graph[MAX + 1];
vector<int> listNodes;
int stiva[MAX * 5 + 1];
bool vizitat[5 * MAX + 1];
int edgeCounter = 0, edgeNr = 0;
void DFS(int nod, int depth)
{
cout << depth << " ";
}
int main()
{
int n, m, a, b;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
fin >> n >> m;
for(int i = 0; i < m; i++)
{
fin >> a >> b;
graph[a].push_back({b, edgeCounter});
graph[b].push_back({a, edgeCounter});
edgeCounter++;
}
int height = 1;
stiva[height] = 1;
while(height)
{
int nod = stiva[height];
//cout<<nod<<" ";
int i;
for(i = 0; i < graph[nod].size(); i++)
{
if(!vizitat[graph[nod][i].second])
{
height++;
stiva[height] = graph[nod][i].first;
vizitat[graph[nod][i].second] = 1;
i = graph[nod].size();
}
}
if(i == graph[nod].size())
{
listNodes.push_back(nod);
height--;
}
}
listNodes.pop_back();
if(listNodes.size() == edgeCounter)for(auto i : listNodes)fout << i << " ";
else fout << -1;
fin.close();
fout.close();
return 0;
}