import java.util.*;
import java.lang.*;
import java.io.*;


class Main {
    public static void main(String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());
        
        int N = Integer.parseInt(st.nextToken());
        int X = Integer.parseInt(st.nextToken());

        int[] graph = new int[N*2+5];
        int[] graph_idx = new int[N*2+5];
        
        for (int i=0; i<N-1; i++) {
            st = new StringTokenizer(br.readLine());
            int a = Integer.parseInt(st.nextToken());
            int b = Integer.parseInt(st.nextToken()); 
            if (graph[graph_idx[a]] == 0) {
                graph[i] = a;
                graph_idx[a] = i;

                if(graph_idx[b] == 0) {
                    graph[i*2+1] = b;
                    graph_idx[b] = i*2+1;
                }
                
            } else {
                if(graph_idx[b] == 0) {
                    graph[i*2+1] = b;
                    graph_idx[b] = i*2+1;
                }
            }            
        }
        StringBuilder sb = new StringBuilder();
        for (int i=0; i<N; i++) {
            sb.append(graph[i]);
            if (i != N-1) sb.append(" ");
        }
        System.out.println(sb);
        
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: