Building 2
Time limit1sMemory limit64 MB
Given a tree of cities with building heights, find the longest subsequence of heights that strictly increases along a simple path, and output its length.
- Level
Medium7 of 10
- Topics
- Divide and conquer, Tree, Dynamic programming, Binary search
- Solved
- No attempts yet
Problem
There are cities in Japan, and they are connected by two-way roads. You can travel from any city to any other city by following some of these roads. Each city has one government office building, and the height of the building in city is .
To welcome athletes from around the world to the International Olympiad in Informatics held in Japan, a sightseeing tour is being planned. A tour starts in one city, moves along roads to other cities, and ends in some city. A city may not be visited more than once.
To make the welcome even more special, some of the government buildings along the tour will be decorated. A designer said that the buildings used for decoration must get taller from the starting city toward the ending city. Let the decorated cities be , visited in that order during the tour. Then must hold. Not every visited building has to be decorated.
To make the decorations as impressive as possible, you want to use as many buildings as you can. Choose the starting city, the ending city, and the decorated cities freely. Write a program that computes the maximum number of buildings that can be used for decoration.
Input
Read the following input from standard input.
- The first line contains an integer , the number of cities.
- The next lines contain the building heights. The -th line contains , the height of the building in city .
- The next lines contain the roads. The -th line contains two integers and (), separated by a space. They mean that the -th road connects city and city .
Output
Print to standard output one integer, the maximum number of buildings that can be used for decoration.
Constraints
(number of cities)
(height of the building in city )