Codeforces Round #279 (Div. 2) F. Treeland Tour(lis+dfs)

题目链接:

huangjing

题意:告诉一个无向无环图,然后求在联通的路上的lis。

思路:枚举起点求lis 复杂度是n^2logn,貌似这复杂度对时间有点玄,估计是数据有点弱。。。

首先枚举起点,然后求lis,主要是用dfs求的,要用到回溯的思想,我觉得是只要更新了,就要保存那个操作,然后一旦这一次的搜索完成,那么就要立即回复g数组的值,因为有很多不同的路线,所以一条路走完后,就要回复以前的状态哦,免得影响另外一条路。。我觉得尽管他们都说是暴力,但是我觉得这个题还是蛮好的。。。

题目:

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 ajbj (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

6
1 2 3 4 5 1
1 2
2 3
3 4
3 5
3 6

output

4

input

5
1 2 3 4 5
1 2
1 3
2 4
3 5

output

3

代码:

#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 0x3f3f3f3f
using 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;
}
时间: 2024-12-14 18:17:15

Codeforces Round #279 (Div. 2) F. Treeland Tour(lis+dfs)的相关文章

Codeforces Round #FF(255) C. DZY Loves Sequences (LIS升级)

题目:C. DZY Loves Sequences (LIS升级) 题意: 在n个数中,最多改变一个数字,并求能够达到的最长严格上升子序列(连续)长度 分析: 考虑第i个数,能否改变后拼接前后两个字串,并维护当前最大值 状态: left[i]:  表示以i为终点的最长严格上升子序列长度 right[i]: 表示以i为起点的最长严格上升子序列长度 dp[i]:   表示改变第i个数后,拼接前后字串的长度 转移方程:       dp[i] = max{left[i-1] + right[i+1] 

Codeforces Round #531 (Div. 3) F. Elongated Matrix(状压DP)

F. Elongated Matrix 题目链接:https://codeforces.com/contest/1102/problem/F 题意: 给出一个n*m的矩阵,现在可以随意交换任意的两行,最后从上到下,从左到右形成一个序列s1,s2.....snm,满足对于任意相邻的两个数,它们差的绝对值的最大值为k. 现在问怎么交换行与行,可以使得最后的这个k最大. 题解: 人生中第一道状压dp~其实还是参考了这篇博客:https://blog.csdn.net/CSDNjiangshan/art

Codeforces Round #511 (Div. 2)-C - Enlarge GCD (素数筛)

传送门:http://codeforces.com/contest/1047/problem/C 题意: 给定n个数,问最少要去掉几个数,使得剩下的数gcd 大于原来n个数的gcd值. 思路: 自己一开始想把每个数的因子都找出来,找到这些因子中出现次数最多且因子大于n个数的最大公约数的,(n - 次数 )就是答案.但是复杂度是1e9,差那么一点. 自己还是对素数筛理解的不够深.这道题可以枚举素数x,对于每个x,找到所有(a[i]/gcd(all)) 是x倍数的个数,就是一个次数.找这个次数的过程

Codeforces Round #279 (Div. 2)f

树形最大上升子序列 这里面的上生子序列logn的地方能当模板使  good #include<iostream> #include<string.h> #include<stdio.h> #include<algorithm> #include<vector> using namespace std; const int maxa = 6006; vector<int>e[maxa]; int d[maxa]; int a[maxa]

Codeforces Round #256 (Div. 2) C. Painting Fence(分治贪心)

题目链接:http://codeforces.com/problemset/problem/448/C ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943

Codeforces Round #256 (Div. 2) D. Multiplication Table(二分查找)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://codeforces.com/contest/448/problem/D -----------------------------------------------------------------------------------------------------------------------------------------------

Codeforces Round #629 (Div. 3) E. Tree Queries(lca题)

https://codeforces.com/contest/1328/problem/E E. Tree Queries You are given a rooted tree consisting of nn vertices numbered from 11 to nn. The root of the tree is a vertex number 11. A tree is a connected undirected graph with n−1n−1 edges. You are

Codeforces Round #124 (Div. 1) C. Paint Tree(极角排序)

C. Paint Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a tree with n vertexes and n points on a plane, no three points lie on one straight line. Your task is to paint

Codeforces Round #360 (Div. 2) D. Remainders Game(中国剩余定理)

D. Remainders Game Today Pari and Arya are playing a game called Remainders. Pari chooses two positive integer x and k, and tells Arya k but not x. Arya have to find the value . There are n ancient numbers c1, c2, ..., cn and Pari has to tell Arya  i