!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
Alice will tell his friend Bob the distance from
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
and — number of nodes and queries. - The next
lines follow. The line contains two integers denoting an edge between the nodes . It is guaranteed that the given nodes and edges form a tree. - The next
lines contain a node .
Output Format
For each query, If
Constraints
Sample Input
4 3 1 2 1 3 3 4 2 3 1
Sample Output
2 2 1
Explanation
- For the first query , node
is at the distance of from node 1, but at the same time node is also at the distance of from node . Probability will be . so the output will be . - Same for the second query.
Code Editor
Please login to use the editor
You need to be logged in to access the code editor
Submissions
Please login to view your submissions
Similar Problems
1.Graph
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
Please login to use the editor
You need to be logged in to access the code editor