Y
Time limit1.5sMemory limit1024 MB
From a given tree, delete any vertices to leave the largest subtree shaped like a Y: three leaf paths meeting at one center, with no vertex of degree 4 or more.
- Level
Hard8 of 10
- Topics
- Tree, DFS, Dynamic programming, Greedy
- Solved
- No attempts yet
Problem
A Y-tree is a tree that satisfies all of the following conditions.
- No vertex is adjacent to or more vertices.
- There is exactly one vertex adjacent to vertices.
- There are exactly three vertices adjacent to exactly one vertex.
The size of a Y-tree is the number of vertices that form it.
You are given a tree with vertices numbered , , …, , one number per vertex. Write a program that finds the size of the largest Y-tree that can be obtained by deleting zero or more vertices from the given tree.
Input
The first line gives the integer , the number of vertices in the tree. ()
The next lines each give two integers and describing an edge of the tree. This means there is an edge connecting vertex and vertex . (, )
Every edge of the tree is given exactly once.
Output
On the first line, print the size of the largest Y-tree that can be made from the given tree.
If no Y-tree can be made, print .