Pagini recente » Istoria paginii runda/preoji_cl10_lspvs | Cod sursa (job #62487) | Cod sursa (job #1640532) | Cod sursa (job #1946232) | Cod sursa (job #1111567)
#include <fstream>
#include <vector>
using namespace std;
const int N = 1 + 1e5, M = 1 + 5e5;
class Stack{
int v[M];
public:
Stack(){
v[0] = 0;
}
void push(int x){
v[ ++v[0] ] = x;
}
int pop(){
return v[ --v[0] ];
}
int top(){
return v[ v[0] ];
}
bool isEmpty(){
return v[0] == 0;
}
};
struct Muchie{
int x, index;
Muchie(int x, int i) : x(x), index(i) {};
};
vector<Muchie> graph[N];
bool usedEdge[N];
Stack S;
int n;
ifstream in("ciclueuler.in");
ofstream out("ciclueuler.out");
void readGraph(){
int m, x, y;
in >> n >> m;
for (int i = 1 ; i <= m ; i++){
in >> x >> y;
graph[x].push_back( Muchie(y, i) );
graph[y].push_back( Muchie(x, i) );
}
}
void euler(int x){
while ( !graph[x].empty() && ( !graph[x].back().x || usedEdge[ graph[x].back().index ] ) )
graph[x].pop_back();
if (!graph[x].empty()){
usedEdge[ graph[x].back().index ] = true;
int y = graph[x].back().x;
graph[x].pop_back();
euler(y);
out << x << " ";
}
}
int main(){
readGraph();
euler(1);
return 0;
}