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

// The main method must be in a class named "Main".
class Main {
    static List<List<Integer>> g;

    static int[] depth;

    static void dfs(int v, int p) {
        for (int u : g.get(v)) {
            if (u != p) {
                depth[u] = depth[v] + 1; 
                dfs(u, v);
            }
        }
    }

    
    
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), m = n-1, s = sc.nextInt()-1; 
        g = new ArrayList<List<Integer>>(n);
        for (int i=0; i<n; i++) {
            g.add(new ArrayList<Integer>());
        }

        for (int i=0; i<m; i++) {
            int a = sc.nextInt() -1, b = sc.nextInt()-1; 
            g.get(a).add(b);
            g.get(b).add(a); 
        }

        depth = new int[n]; 

        dfs(s, -1);

        for (int i = 0; i < n; i++) {
            System.out.println("vertex" + (i + 1) + "\'s depth is " + depth[i]);
        }

        

        
    }
}

Embed on website

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