Pagini recente » Cod sursa (job #2401682) | Cod sursa (job #228643) | Cod sursa (job #2553984) | Cod sursa (job #315316) | Cod sursa (job #2527425)
#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++;
}
bool semafor = true;
for(int i = 1; i <= n && semafor == true; i++)if(graph[i].size() % 2)semafor = false;
if(semafor)
{
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())
{
fout << nod << " ";
height--;
}
}
}else fout << -1;
fin.close();
fout.close();
return 0;
}