https://codeforces.com/contest/1131/problem/F

 

#include<bits/stdc++.h>
using namespace std;
int n;
vector<vector<int> > v;
vector<int> par;
int find(int d){
    if(d==par[d])
        return d;
    else
        return par[d]=find(par[d]);
}
void unite(int x,int y){
    x=find(x);
    y=find(y);
    if(v[x].size()<v[y].size())
        swap(x,y);
    for(int i=0;i<v[y].size();i++)
        v[x].push_back(v[y][i]);
    par[y]=x;
}
int main(){
    int n;
    cin>>n;
    v.resize(n+1);
    par.resize(n+1);
    for(int i=1;i<=n;i++){
        par[i]=i;    
        v[i].push_back(i);    
    }
    for(int i=0;i<n-1;i++){
        int a,b;
        cin>>a>>b;
        unite(a,b);
    }
    for(int x:v[find(1)])cout<<x<<' ';
    cout<<endl;
    return 0;
}