!Probability

2.9

12 votes
Algorithms, Depth-first search, Trees, Dynamic Programming
Problem

Given a tree with N nodes and N1 edges rooted at node 1. You have to answer Q queries. In every query, you will be given a node x.

Alice will tell his friend Bob the distance from 1 to x. But Bob only knows how to reach a particular distance from 1. Find the probability that Bob will reach x in one try. If P will be the probability then print 1P in each query.

Note - The distance between two vertices in a tree is equal to the number of edges on the unique simple path between them.

Input Format

  • The first line contains N and Q — number of nodes and queries.
  • The next  N1 lines follow. The ith line contains two integers Xi,Yi denoting an edge between the nodes Xi,Yi. It is guaranteed that the given nodes and edges form a tree.
  • The next Q lines contain a node x.

Output Format

For each query, If P will be the probability to  reach x in one try then print 1P in a separate line.

Constraints

1N,Q1051Xi,YiNXiYi1xN

 

Time Limit: 1
Memory Limit: 512
Source Limit:
Explanation
  • For the first query , node 2 is at the distance of 1 from node 1, but at the same time node 3 is also at the distance of 1 from node 1. Probability P will be 12. so the output will be 1P=2.
  • Same for the second query.
Editor Image

?