Problem:
Score : 100 points

Problem Statement

There is a tree with N vertices, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N - 1), the i-th edge connects Vertex x_i and y_i.

Taro has decided to paint each vertex in white or black, so that any black vertex can be reached from any other black vertex by passing through only black vertices.

You are given a positive integer M.
For each v (1 \leq v \leq N), answer the following question:

Assuming that Vertex v has to be black, find the number of ways in which the vertices can be painted, modulo M.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

2 \leq M \leq 10^9

1 \leq x_i, y_i \leq N

The given graph is a tree.

Input

Input is given from Standard Input in the following format:

N M
x_1 y_1
x_2 y_2
:
x_{N - 1} y_{N - 1}

Output

Print N lines.
The v-th (1 \leq v \leq N) line should contain the answer to the following question:

Assuming that Vertex v has to be black, find the number of ways in which the vertices can be painted, modulo M.

Sample Input 1

3 100
1 2
2 3

Sample Output 1

3
4
3

There are seven ways to paint the vertices, as shown in the figure below.
Among them, there are three ways such that Vertex 1 is black, four ways such that Vertex 2 is black and three ways such that Vertex 3 is black.

Sample Input 2

4 100
1 2
1 3
1 4

Sample Output 2

8
5
5
5

Sample Input 3

1 100

Sample Output 3

1

Sample Input 4

10 2
8 5
10 8
6 5
1 5
4 8
2 10
3 6
9 2
1 7

Sample Output 4

0
0
1
1
1
0
1
0
1
1

Be sure to print the answers modulo M.

Solution in Java (OpenJDK 11.0.6) (judge verdict: Accepted)


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;


public class Main {// Main

	public static void main(String[] args) throws Exception {
		in = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);
		sc = new FastScanner(in);
		
		
		int n =sc.nextInt();
		m = sc.nextLong();
		map = undirected(n);
	//	System.out.println(map);
		
		an = new long[n+1];
		dfs(1 , -1);
		partochild(1, 1);
		for(int i =1 ;i<an.length ;i++) {
			out.println(an[i]);
		}
		out.flush();
	}
	private static void partochild(int cur , long from_parent) {
		if(map.get(cur).size()==0)return ;
		long [] arr = dp.get(cur);
		long[] pre = new long[arr.length];
		long[] suf = new long[arr.length];
		
		int n = arr.length; 
		pre[0]=1;
		for(int i = 1 ;i < n ;i++) {
			pre[i]= (pre[i-1]*arr[i-1])%m;
		}
		suf[n-1]=1;
		for(int i = n-2 ;i >=0 ;i--) {
			suf[i]=(suf[i+1]*arr[i+1])%m;
		}		
		
		
		
		
		int i = 0 ;
		for(int nbr : map.get(cur)) {
			
			long temp =( pre[i] * suf[i] )%m;
			temp = (temp * from_parent)%m;
		     temp = (temp + 1)%m;	
		     an[nbr] = (an[nbr] * temp)%m;
		     partochild(nbr, temp );
		     i++;
		}			
			
					
			
		
	}
	 private static long dfs(int cur, int par) {
		// TODO Auto-generated method stub
		long ans = 1; 	
	//	System.out.println(map.get(cur));
		if(par!=-1)map.get(cur).remove(par);
		
		
		dp.put(cur, new long[map.get(cur).size()] );
		
		
		
		int i = 0 ;
  		for(int nbr : map.get(cur)) {
			if(nbr==par)continue;
			
			long temp = dfs(nbr ,cur)+1;
			
			temp%=m;
			dp.get(cur)[i] = temp;
			
			
			ans = (ans * temp)%m;
			i++;
		}
		 an[cur] = ans;
		 
		 return ans;
		 
	}
	 static long m ;
	static long [] an ; 
	 static TreeMap<Integer , TreeSet<Integer> >  map ; 
	
	 static TreeMap<Integer ,long[] >  dp =new TreeMap<Integer, long[]>(); 

	public static TreeMap<Integer , TreeSet<Integer> > undirected(int n ) throws Exception{
		TreeMap<Integer , TreeSet<Integer> > map = new TreeMap<>();
		for(int i = 1 ;i<=n ;i++)map.put(i, new TreeSet<Integer>());
		

		for(int i = 1 ;i < n;i++) {
			int a = sc.nextInt();
			int b = sc.nextInt();
			map.get(a).add(b);
			map.get(b).add(a);
		}
		return map;
	}
	
	static long mod = 1000000007;
	public static long[][] input(int n, int m) throws Exception {

		long[][] arr = new long[n][m];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {

				arr[i][j] = sc.nextInt();
			}
		}
		return arr;
	}
	
	
	
	static BufferedReader in;
	static FastScanner sc;
	static PrintWriter out;

	static class FastScanner {
		BufferedReader in;
		StringTokenizer st;

		public FastScanner(BufferedReader in) {
			this.in = in;
		}

		public String nextToken() throws Exception {
			while (st == null || !st.hasMoreTokens()) {
				st = new StringTokenizer(in.readLine());
			}
			return st.nextToken();
		}

		public int nextInt() throws Exception {
			return Integer.parseInt(nextToken());
		}

		public long nextLong() throws Exception {
			return Long.parseLong(nextToken());
		}

		public double nextDouble() throws Exception {
			return Double.parseDouble(nextToken());
		}
	}
	
}