Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #define MAX 1000
- #define MAXPATH 1000000
- using namespace std;
- int S[MAX],G[MAX][MAX];
- int N,M,top;
- int path[MAXPATH], pathSize=0;
- void make_empty(){
- top =-1;
- }
- void push(int x){
- S[++top]=x;
- }
- void pop(){
- path[pathSize++]=S[top--];
- }
- int look(){
- return S[top];
- }
- bool not_empty(){
- return top>-1;
- }
- void EulerCircuit(int r)
- { make_empty();
- push(r);
- while(not_empty())
- { int t=look();
- if(G[t][0]>0)
- {
- int v=G[t][G[t][0]--];
- for(int j=1;j<=G[v][0];j++)
- {
- if(G[v][j]==t) {G[v][j]=G[v][G[v][0]--];
- break; }
- }
- push(v);
- }
- else pop();
- }
- }
- int main(){
- int x,y;
- cin >> N >> M;
- for( int i=0; i<M; i++ ){
- cin >> y >> x;
- G[x][++G[x][0]] = y;
- G[y][++G[y][0]] = x;
- }
- EulerCircuit(1);
- if(pathSize<M+1){
- cout <<"Sorry"<<endl;
- return EXIT_SUCCESS;
- }
- for(int i=0;i<pathSize;i++) {
- cout <<path[i]<<" ";
- }
- }
- /*
- input 1:
- 6 10
- 1 2
- 1 3
- 1 4
- 2 3
- 2 5
- 3 4
- 3 5
- 4 5
- 4 6
- 5 6
- input 2:
- 6 6
- 1 2
- 1 3
- 1 4
- 2 3
- 3 4
- 5 6
- */
Advertisement
Add Comment
Please, Sign In to add comment