← 返回 snowflake 的题目列表Delete Nodes in an N-ary Tree with Children Reattached; Compute Resulting Height
类型:online_judge
Problem
You are given a rooted N-ary tree and a set of nodes to_delete.
When deleting a node x:
x is removed from the tree
all children of x are reattached directly to x's parent (preserving their relative order)
if the root is deleted, the result is a forest; define the final height as the maximum height among all resulting trees (if no nodes remain, height is 0)
Compute the maximum height after performing all deletions.
Height is the number of nodes on the longest path from a root to any leaf.
Input Format (one common representation)
Line 1: integer n with nodes labeled 0..n-1
Line 2: integer root
Line 3: integer m (# of nodes to delete)
Line 4: m integers for to_delete
Next n lines: children list for each node i:
each line starts with c followed by c child indices
Output
Print one integer: the maximum height after deletions.
Constraints
1 <= n <= 2e5
Total number of child links is n-1
Examples
(See Chinese section for 5 sample I/O cases.)
Example
Input
5
0
1
1
2 1 2
2 3 4
0
0
0
Output
2