Codeforces 490F. Treeland Tour brute force + LIS, codeforces490f

Source: Internet
Author: User

Codeforces 490F. Treeland Tour brute force + LIS, codeforces490f


Enumerate root + dfs. I don't know what the positive solution is ......

F. Treeland Tourtime limit per test5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

The "Road Accident" band is planning an unprecedented tour around Treeland. The RA fans are looking forward to the event and making bets on how many concerts their favorite group will have.

Treeland consistsNCities, some pairs of cities are connected by bidirectional roads. Overall the country hasNTables-Tables 1 roads. We know that it is possible to get to any city from any other one. The cities are numbered by integers from 1N. For every city we know its valueRI-The number of people in it.

We know that the band will travel along some path, having concerts in some cities along the path. the band's path will not pass one city twice, each time they move to the city that hasn't been previusly visited. thus, the musicians will travel along some path (without visiting any city twice) and in some (not necessarily all) cities along the way they will have concerts.

The band plans to gather all the big stadiums and concert hallduring the tour, so every time they will perform in a city which population islarger than the population of the previusly visited with concert city. in other words, the sequence of population in the cities where the concerts will be held is strictly increasing.

In a recent interview with the leader of the "road accident" band promised to the fans that the band will give concert in the largest possible number of cities! Thus the band will travel along some chain of cities of Treeland and have concerts in some of these cities, so that the population number will increase, and the number of concerts will be the largest possible.

The fans of Treeland are frantically trying to figure out how many concerts the group will have in Treeland. Looks like they can't manage without some help from a real programmer! Help the fans find the sought number of concerts.

Input

The first line of the input contains integerN(2 cores ≤ CoresNLimit ≤ limit 6000)-the number of cities in Treeland. The next line containsNIntegersR1, bytes,R2, middle..., middle ,...,RN(1 digit ≤ DigitRILimit ≤ limit 106), whereRIIs the population ofI-Th city. The nextNLimits-limits 1 lines contain the descriptions of the roads, one road per line. Each road is defined by a pair of integersAJ,BJ(1 digit ≤ DigitAJ, Bytes,BJLimit ≤ limitN)-The pair of the numbers of the cities that are connected byJ-Th road. All numbers in the lines are separated by spaces.

Output

Print the number of cities where the "Road Accident" band will have concerts.

Sample test (s) input
61 2 3 4 5 11 22 33 43 53 6
Output
4
Input
51 2 3 4 51 21 32 43 5
Output
3


/*************************************** * ******** Author: CKbossCreated Time:, Saturday, March 14, 2015 File Name: CF490F. cpp *************************************** * ********/# include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <string> # include <cmath> # include <cstdlib> # include <vector> # include <queue> # include <set> # include <map> using namespace std; const int maxn = 6600; Int n, val [maxn]; int Adj [maxn], Size; struct Edge {int to, next;} edge [maxn * 2]; void init_edge () {memset (Adj,-1, sizeof (Adj); Size = 0;} void add_edge (int u, int v) {edge [Size]. to = v; edge [Size]. next = Adj [u]; Adj [u] = Size ++;} int range [maxn], rn; int dp [maxn], ans = 1; void dfs (int u, int fa) {for (int I = Adj [u]; ~ I; I = edge [I]. next) {int v = edge [I]. to; if (v = fa) continue; // getLISint oldV; bool oldRn = false; int POS = lower_bound (range, range + rn, val [v]) -range; oldV = range [POS]; range [POS] = val [v]; dp [v] = max (dp [v], POS + 1 ); if (POS = rn) {oldRn = true; rn ++;} dfs (v, u); if (oldRn) rn --; range [POS] = oldV ;}} int main () {// freopen ("in.txt", "r", stdin); // freopen ("out.txt", "w", stdout); init_edge (); scanf ("% d", & n); for (int I = 1; I <= n; I ++) scanf ("% d", val + I ); for (int I = 1; I <= n-1; I ++) {int u, v; scanf ("% d", & u, & v ); add_edge (u, v); add_edge (v, u) ;}// enum rootfor (int rt = 1; rt <= n; rt ++) {rn = 0; range [rn ++] = val [rt]; dp [rt] = max (dp [rt], 1); dfs (rt, 0 );} for (int I = 1; I <= n; I ++) ans = max (ans, dp [I]); cout <ans <endl; return 0 ;}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.