Preview:
#include <bits/stdc++.h> 
#define int long long
using namespace std;

vector<int> g[100010];
vector<int> vis(1001);

void dfs(int node){
    if(vis[node]) return;
    vis[node] = 1;
    cout << node <<  " ";
    for(int i = 0;i<v[node].size();i++){
        dfs(v[node][i]);
    }
}

int32_t main(){
    int n;
    cin >> n;
    for(int i = 1;i<=n;i++){
        int x,y;
        cin >> x >> y;
        v[x].push_back(y);
        v[y].push_back(x);
    }
    dfs(5);
}
downloadDownload PNG downloadDownload JPEG downloadDownload SVG

Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!

Click to optimize width for Twitter