Language: C++

#include <stdio.h>
#include <string.h>
#include <map>
#include <vector>
#include <list>
#include <pair>

using namespace std;


int N,M;
map<int,vector<int>> paths;
list<int> output;

int l,r;

bool hasYouso(list<int> &a, int k);
void chkLeft(void);
void chkRight(void);
void printOutput(void);
void erasePath(int a,int b);
void printPaths(void);

int main(int argc, const char * argv[]) {
    scanf("%d %d",&N, &M);
    int a,b;
    scanf("%d %d",&a,&b);
    output.push_back(a);
    output.push_back(b);
    l=a;
    r=b;
    for(int k=0;k<N-1;k++){
        scanf("%d %d",&a,&b);
        if(l == a){
            if(!hasYouso(output, b)){
                output.push_front(b);
                l = b;
                chkLeft();
                continue;
            }
        }else if(l == b){
            if(!hasYouso(output,a)){
                output.push_front(a);
                l=a;
                chkLeft();
                continue;
            }
        }else if(r == a){
            if(!hasYouso(output,b)){
                output.push_back(b);
                r=b;
                chkRight();
                continue;
            }
        }else if(r == b){
            if(!hasYouso(output,a)){
                output.push_back(a);
                r=a;
                chkRight();
                continue;
            }
        }else{
            paths[a].push_back(b);
            paths[b].push_back(a);
        }
//        printOutput();
//        printPaths();
    }
    printOutput();
    
    return 0;
}

void printPaths(void){
    for(map<int,vector<int>>::iterator i=paths.begin();i!=paths.end();i++){
        printf("%d-",(*i).first);
        for(auto a=(*i).second.begin();a!=(*i).second.end();a++){
            printf("%d ",*a);
        }
        printf("\n");
    }
}

void printOutput(void){
    printf("%ld\n",output.size());
    for(auto i=output.begin();i!=output.end();i++){
        printf("%d ",*i);
    }
    printf("\n");
}

void erasePath(int a,int b){
    vector<int> *vec = &paths[a];
    for(auto p=vec->begin();p!=vec->end();p++){
        if(*p == b){
            vec->erase(p);
            return;
        }
    }
}

void chkLeft(void){
jmpl:
    if(paths[l].size() != 0){
        vector<int> *vec = &paths[l];
        vector<pair<int,int>> removePath;
        for(auto p=vec->begin();p!=vec->end();p++){
            if(!hasYouso(output, *p)){
                output.push_front(*p);
                int remove = *p;
                vec->erase(p);
                erasePath(remove,l);
//                printf("%d not found\n",*p);
                l=*p;
                goto jmpl;
            }else{
                int remove = *p;
                removePath.push_back(make_pair(remove, l));
            }
        }
        for(auto remove = removePath.begin();remove != removePath.end();remove++){
            erasePath(remove->first, remove->second);
            erasePath(remove->second, remove->first);
        }
    }
}
void chkRight(void){
jmpr:
    if(paths[r].size() != 0){
        vector<int> *vec = &paths[r];
        vector<pair<int,int>> removePath;
        for(auto p=vec->begin();p!=vec->end();p++){
            if(!hasYouso(output, *p)){
                output.push_back(*p);
                int remove = *p;
                vec->erase(p);
                erasePath(remove,r);
                r=*p;
                goto jmpr;
            }else{
                int remove = *p;
                removePath.push_back(make_pair(remove, r));
            }
        }
        for(auto remove = removePath.begin();remove != removePath.end();remove++){
            erasePath(remove->first, remove->second);
            erasePath(remove->second, remove->first);
        }
    }
}

bool hasYouso(list<int> &a, int k){
    for(auto p=a.begin();p!=a.end();p++){
        if(k == *p){
            return true;
        }
    }
    return false;
}


Compiler output:
a.cc:6:10: fatal error: pair: No such file or directory
    6 | #include <pair>
      |          ^~~~~~
compilation terminated.