!Probability
Practice
2.9 (12 votes)
Algorithms
Depth First search
Trees
Dynamic programming
Problem
33% Success 1105 Attempts 30 Points 1s Time Limit 512MB Memory 1024 KB Max Code

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,Q105
1Xi,YiN
XiYi
1xN

 

Sample Input
4 3
1 2
1 3
3 4
2
3
1
Sample Output
2
2
1
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.
Code Editor

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Submissions
Please login to view your submissions
Similar Problems
Points:30
1 votes
Tags:
TreeDynamic ProgrammingAlgorithmsMedium
Points:30
8 votes
Tags:
Binary search algorithmDynamic ProgrammingAlgorithmsMediumTree
Points:30
10 votes
Tags:
Dynamic ProgrammingAlgorithmsTrees
Editorial

Login to unlock the editorial