Expected Value

0

0 votes
Hard
Problem

You are given a rooted tree of N nodes. Each node is numbered from 1 to N and the value associated with that node is the node number itself. Additionally, you are also given an integer K. Now for every node of the tree, you can choose any combination of exactly K size of its children. Each combination is equally probable. The special value of a node is denoted by the product of node values involved in the combination of children chosen. You need to calculate the expected special value of each node in the tree. If there are less than K children of that node, then expected special value is 0.
Note: 1 is the root node of the tree.

Input
The first line contains an integer N  and an integer K as input.
The next N1 lines contain a pair of integers u,v as input that denotes that there is an edge between the nodes u and v.

Output
In the output, you need to print the expected special value of each node modulo 998244353.

Constraints
1N1051K105

Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

In the sample, there is only one node for which non-zero answer is possible i.e. the node 1. The sum of products of its child values if taken 3 at a time has 4 distinct possibilities, thus the probability of each combination is 1/4. Now expected value is given by - 
(2*3*4) / 4 + (2*3*5) / 4 + (2*3*5) / 4 + (3*4*5) / 4 = 154/4 = 499122215 (inverse modulo calculated here)

Editor Image

?