Pagini recente » Cod sursa (job #1807569) | Cod sursa (job #691868) | Cod sursa (job #2134876) | Cod sursa (job #300848) | Cod sursa (job #2573469)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
ifstream fin("ciclueuler.in");
ofstream fout("ciclueuler.out");
int N, M, x, y;
vector<int>graf[100];
stack<int>s;
stack<int>snou;
vector<int>grafNou[100];
int used[100];
void scoate(int x, int v)
{
vector<int>::iterator it;
for(it = graf[x].begin(); it != graf[x].end(); it++)
if(*it == v)
{
graf[x].erase(it);
--used[x];
break;
}
for(it = graf[v].begin(); it != graf[v].end(); it++)
if(*it == x)
{
graf[v].erase(it);
--used[v];
break;
}
}
void eulerDFS(int nod){
int nextNode = graf[nod][0];
scoate(nod, nextNode);
s.push(nextNode);
eulerDFS(nextNode);
}
int main()
{
fin >> N >> M;
for(int i = 1; i <= M; i++){
fin >> x >> y;
if(x != y){
graf[x].push_back(y);
graf[y].push_back(x);
used[x]++;used[y]++;
}else{
graf[x].push_back(x);
used[x]++;
}
}
s.push(1);
eulerDFS(1);
while(!s.empty()){
int nodCurent = s.top();
if(used[nodCurent]){
eulerDFS(nodCurent);
}
else{
s.pop();
snou.push(nodCurent);
}
}
while(!snou.empty()){
fout << snou.top() << ' ';
snou.pop();
}
return 0;
}