← 返回 doordash 的题目列表Compute subtree sizes for each node from parent-child pairs
类型:online_judge
doordash
Given a tree structure where each node represents a tree node, write a function to compute the size of the subtree for each node. Provide test cases. Assume the number of nodes in the tree does not exceed 1000, and node values are unique integers. The node information is represented as (parent, child) pairs. The input is a series of (parent, child) pairs, and the output should be the size of the subtree for each node, sorted by node value in ascending order.
Input
First line: an integer n, the number of (parent, child) pairs.
The next n lines: each line contains two integers u and v, representing a (parent, child) pair.
Output
Print n + 1 lines representing the size of the subtree for each node. Each line has two integers: the node's value and the size of that node's subtree, sorted by node value in ascending order.
Example
Input
2
1 2
1 3
Output
1 3
2 1
3 1
Constraints
Up to 1000 nodes.
Nodes have unique integer values.
Example
Input
2
1 2
1 3