Home Web Front-end HTML Tutorial Codeforces Round #279 (Div. 2) F. Treeland Tour(lis dfs)_html/css_WEB-ITnose

Codeforces Round #279 (Div. 2) F. Treeland Tour(lis dfs)_html/css_WEB-ITnose

Jun 24, 2016 am 11:53 AM

Question link:

huangjing


Question meaning: Tell an undirected acyclic graph, and then find the lis on the road.

Idea: Enumerate the points to find lis. The complexity is n^2logn. It seems that this complexity is a bit mysterious for time. It is estimated that the data is a bit weak. . .

First enumerate the points, and then find lis, mainly using dfs. The idea of ​​backtracking is used. I think as long as it is updated, the operation must be saved, and then once the search is completed this time, Then the value of the g array must be restored immediately, because there are many different routes, so after one path is completed, the previous state must be restored to avoid affecting the other path. . I think even though they all say it’s violence, I think this question is pretty good. . .

Title:

F. Treeland Tour

time limit per test

5 seconds

memory limit per test

256 megabytes

input

standard input

output

standard 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 consists of n cities, some pairs of cities are connected by bidirectional roads. Overall the country has n?-?1 roads. We know that it is possible to get to any city from any other one. The cities are numbered by integers from 1 to n. 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 previously 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 halls during the tour, so every time they will perform in a city which population is larger than the population of the previously 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 integer n (2?≤?n?≤?6000) ? the number of cities in Treeland. The next line contains n integersr1,?r2,?...,?rn (1?≤?ri?≤?106), where ri is the population of the i-th city. The next n?-?1 lines contain the descriptions of the roads, one road per line. Each road is defined by a pair of integers aj, bj (1?≤?aj,?bj?≤?n) ? the pair of the numbers of the cities that are connected by thej-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
Copy after login

output

input

51 2 3 4 51 21 32 43 5
Copy after login

output


Code:

#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<map>#include<vector>#include<cmath>#include<string>#include<queue>#define eps 1e-9#define ll long long#define INF 0x3f3f3f3fusing namespace std;priority_queue<int,vector<int>,greater<int> >Q;const int maxn=6000+10;struct Egde{    int next,to;}edge[maxn<<1];int g[maxn],val[maxn],head[maxn],cnt,n,ans,top;void add_edge(int x,int y){    edge[++cnt].to=y;    edge[cnt].next=head[x];    head[x]=cnt;}void dfs(int u,int top=0,int fa=0){     int tmp,pos;     if(top==0||val[u]>g[top])     {         pos=++top;         tmp=g[top];         g[top]=val[u];         ans=max(ans,top);     }     else     {         pos=lower_bound(g+1,g+1+top,val[u])-g;         tmp=g[pos];         g[pos]=val[u];     }     for(int i=head[u];~i;i=edge[i].next)     {         int v=edge[i].to;         if(v!=fa)            dfs(v,top,u);     }     g[pos]=tmp;}int main(){    int x,y;    while(~scanf("%d",&n))    {        ans=cnt=0;        memset(head,-1,sizeof(head));        memset(g,-1,sizeof(g));        for(int i=1;i<=n;i++)            scanf("%d",&val[i]);        for(int i=1;i<=n-1;i++)        {            scanf("%d%d",&x,&y);            add_edge(x,y);            add_edge(y,x);        }        for(int i=1;i<=n;i++)            dfs(i);        printf("%d\n",ans);    }    return 0;}
Copy after login


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the purpose of the <datalist> element? What is the purpose of the <datalist> element? Mar 21, 2025 pm 12:33 PM

The article discusses the HTML &lt;datalist&gt; element, which enhances forms by providing autocomplete suggestions, improving user experience and reducing errors.Character count: 159

What is the purpose of the <progress> element? What is the purpose of the <progress> element? Mar 21, 2025 pm 12:34 PM

The article discusses the HTML &lt;progress&gt; element, its purpose, styling, and differences from the &lt;meter&gt; element. The main focus is on using &lt;progress&gt; for task completion and &lt;meter&gt; for stati

What is the purpose of the <meter> element? What is the purpose of the <meter> element? Mar 21, 2025 pm 12:35 PM

The article discusses the HTML &lt;meter&gt; element, used for displaying scalar or fractional values within a range, and its common applications in web development. It differentiates &lt;meter&gt; from &lt;progress&gt; and ex

What is the purpose of the <iframe> tag? What are the security considerations when using it? What is the purpose of the <iframe> tag? What are the security considerations when using it? Mar 20, 2025 pm 06:05 PM

The article discusses the &lt;iframe&gt; tag's purpose in embedding external content into webpages, its common uses, security risks, and alternatives like object tags and APIs.

What is the viewport meta tag? Why is it important for responsive design? What is the viewport meta tag? Why is it important for responsive design? Mar 20, 2025 pm 05:56 PM

The article discusses the viewport meta tag, essential for responsive web design on mobile devices. It explains how proper use ensures optimal content scaling and user interaction, while misuse can lead to design and accessibility issues.

How do I use HTML5 form validation attributes to validate user input? How do I use HTML5 form validation attributes to validate user input? Mar 17, 2025 pm 12:27 PM

The article discusses using HTML5 form validation attributes like required, pattern, min, max, and length limits to validate user input directly in the browser.

What are the best practices for cross-browser compatibility in HTML5? What are the best practices for cross-browser compatibility in HTML5? Mar 17, 2025 pm 12:20 PM

Article discusses best practices for ensuring HTML5 cross-browser compatibility, focusing on feature detection, progressive enhancement, and testing methods.

How do I use the HTML5 <time> element to represent dates and times semantically? How do I use the HTML5 <time> element to represent dates and times semantically? Mar 12, 2025 pm 04:05 PM

This article explains the HTML5 &lt;time&gt; element for semantic date/time representation. It emphasizes the importance of the datetime attribute for machine readability (ISO 8601 format) alongside human-readable text, boosting accessibilit

See all articles