Treeland Tour

离散化之后, 每个节点维护上升链和下降链, 感觉复杂度有点高, 为啥跑这么快。。

#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ull unsigned long long using namespace std; const int N = + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-;
const double PI = acos(-); int n, tot, ans, r[N], hs[N];
short up[N][N], dn[N][N];
vector<int> G[N]; void dfs(int u, int fa) {
for(int i = r[u]; i <= tot; i++) up[u][i] = ;
for(int i = ; i <= r[u]; i++) dn[u][i] = ;
for(auto& v : G[u]) {
if(v == fa) continue;
dfs(v, u);
int mx1 = , mx2 = ;
for(int i = ; i <= tot; i++) {
if(up[v][i] + dn[u][i + ] > ans) ans = up[v][i] + dn[u][i + ];
if(dn[v][i] + up[u][i - ] > ans) ans = dn[v][i] + up[u][i - ];
}
for(int i = ; i <= tot; i++) {
if(up[v][i] > up[u][i]) up[u][i] = up[v][i];
if(dn[v][i] > dn[u][i]) dn[u][i] = dn[v][i];
if(i < r[u] && up[v][i] > mx1) mx1 = up[v][i];
if(i > r[u] && dn[v][i] > mx2) mx2 = dn[v][i];
}
if(mx1 + > up[u][r[u]]) up[u][r[u]] = mx1 + ;
if(mx2 + > dn[u][r[u]]) dn[u][r[u]] = mx2 + ;
for(int i = ; i <= tot; i++) up[u][i] = max(up[u][i], up[u][i - ]);
for(int i = tot; i >= ; i--) dn[u][i] = max(dn[u][i], dn[u][i + ]);
}
if(up[u][tot] > ans) ans = up[u][tot];
if(dn[u][] > ans) ans = dn[u][];
} int main() {
scanf("%d", &n);
for(int i = ; i <= n; i++) {
scanf("%d", &r[i]);
hs[++tot] = r[i];
}
sort(hs + , hs + tot + );
tot = unique(hs + , hs + tot + ) - hs - ;
for(int i = ; i <= n; i++)
r[i] = lower_bound(hs, hs + tot, r[i]) - hs;
for(int i = ; i <= n; i++) {
int u, v; scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
dfs(, );
printf("%d\n", max(, ans));
return ;
} /*
*/

Codeforces 490F Treeland Tour 树形dp的更多相关文章

  1. Codeforces 490F. Treeland Tour 暴力+LIS

    枚举根+dfs 它可以活 , 我不知道有什么解决的办法是积极的 ...... F. Treeland Tour time limit per test 5 seconds memory limit p ...

  2. Codeforces 490F Treeland Tour(离散化 + 线段树合并)

    题目链接 Treeland Tour 题目就是让你求树上LIS 先离散化,然后再线段树上操作.一些细节需要注意一下. #include <bits/stdc++.h> using name ...

  3. Codeforces 490F Treeland Tour 树上的最长上升子序列

    题目链接:点击打开链接 题意: 给定n个点的树. 以下n个数表示点权. 以下n-1行给出树. 找一条链,然后找出这条链中的点权组成的最长上升子序列. 求:最长上升子序列的长度. 思路: 首先是维护一条 ...

  4. Codeforces 219D Choosing Capital for Treeland(树形DP)

    题目是给一张边有向的树形图.要选出首都的点,首都要都能走到其他点,因此要反转一些边的方向.问可以选哪几个点作为首都,使它们所需反转边的数量最少. 这题挺好想的,因为做过HDU2196. 首先就不妨设正 ...

  5. CF 219 D:Choosing Capital for Treeland(树形dp)

    D. Choosing Capital for Treeland 链接:http://codeforces.com/problemset/problem/219/D   The country Tre ...

  6. codeforces 212E IT Restaurants(树形dp+背包思想)

    题目链接:http://codeforces.com/problemset/problem/212/E 题目大意:给你一个无向树,现在用两种颜色去给这颗树上的节点染色.用(a,b)表示两种颜色分别染的 ...

  7. Codeforces 123E Maze(树形DP+期望)

    [题目链接] http://codeforces.com/problemset/problem/123/E [题目大意] 给出一棵,给出从每个点出发的概率和以每个点为终点的概率,求出每次按照dfs序从 ...

  8. codeforces 709E E. Centroids(树形dp)

    题目链接: E. Centroids time limit per test 4 seconds memory limit per test 512 megabytes input standard ...

  9. ZOJ 3188 ZOJ 3188 Treeland Exhibition(树形DP)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3278 题意:给出一棵树,找出一个不大于长度为m的链,使得其他点到该链 ...

随机推荐

  1. XQuartz简介

    这是一个类似于中转的软件,比如现在在Mac上,YY语音还没有官方版的,但其实在Mac上,有了XQuartz就可以实现运行YY了,下载这个从Windows上移植过来的软件,然后打开的时候,Mac会提醒你 ...

  2. dense prediction

    Dense prediction  fully convolutional network for sementic segmentation 先用feature extractor 提特征,然后再使 ...

  3. Docker入门02——Dockerfile详解

    基本示例 FROM MAINTAINER LABEL RUN ADD COPY CMD ENTRYPOINT ENV EXPOSE VOLUME WORKDIR USER ARG 基本示例 # Thi ...

  4. Linux cache 缓存过大

    linux cache 缓存过大 : 除重启服务 之外:直接释放内存方式之一: 修改配置释放cached内存: echo > /proc/sys/vm/drop_caches

  5. WEB 服务器 加速缓存比较

    Nginx 相对 Apache httpd 的优点: - 轻量级,同样起web 服务,比apache 占用更少的内存及资源 - 抗并发,nginx 处理请求是异步非阻塞的,而apache 则是阻塞型的 ...

  6. 记录一个PHP安装redis扩展时的问题

    安装过程:https://www.cnblogs.com/pengyunjing/p/8688320.html 由于我之前安装过该扩展,重新安装时没有执行make clean命令,所以安装好出现了下面 ...

  7. python队列queue 之优先级队列

    import queue as Q def PriorityQueue_int(): que = Q.PriorityQueue() que.put(10) que.put(1) que.put(5) ...

  8. Android中实现延时执行操作的几种方法

    1.使用线程的休眠实现延时操作 new Thread() { @Override public void run() { super.run(); Thread.sleep(3000);//休眠3秒 ...

  9. 改环境变量改出问题了,vi,ls这些命令都不能用了,怎么办

    1.出现这个问题肯定是以前的基本环境变量都用不了了(大部分情况就是多了一个空格) 解决办法: cd /usr/bin 下执行vi命令  修改环境变量  然后source /etc/profile   ...

  10. RabbitMQ运行机制

    AMQP中消息的路由过程和Java开发者熟悉的JMS存在一些差别,AMQP中增加了Exchange和Binding的角色,生产者把消息发布到Exchange上,Binding决定发布到Exchange ...