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. Error: failed to execute 'C:\Keil\ARM\ARMCC'的解决办法

    在KEIL新建工程时,容易出现该问题,我查了一些资料,最终找到该问题解决方法: 第一步:在keil里的菜单栏依次选择Project->Manage->Components,Environm ...

  2. Bulma - 基于 Flexbox 的现代化的 CSS 框架

    Bulma 是一个基于 Flexbox 的现代化的 CSS 框架,设计的初衷就是移动优先(Mobile First),模块化设计,可以轻松用来实现各种简单或者复制的内容布局,浏览器支持:浏览器支持:C ...

  3. python自带的调试器

    python是自带调试器的. 比如你写了一个python程序,名叫test.py. 你想调试一下这个程序,你可以执行 python -m pdb test.py,就会进入test.py的调试. 想查看 ...

  4. android 生成、pull解析xml文件

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...

  5. 第16月第5天 performSelector afterDelay cancel dispatch_semaphore_wait

    1. //不延时,可能会导致界面黑屏并卡住一会 [self performSelector:@selector(startScan) withObject:nil afterDelay:0.3]; - ...

  6. mongodb系列~mongodb慢语句(2)

    一简介:今天遇到一个慢日志的排查和解决过程 二 版本:3.0.6 三 架构:分片集群 四 具体过程 1 程序响应很慢,具体日志寻找定点sql(mongodb慢日志记录在log日志里) awk '$NF ...

  7. Pytorch 各种奇葩古怪的使用方法

    h1 { counter-reset: h2counter; } h2 { counter-reset: h3counter; } h3 { counter-reset: h4counter; } h ...

  8. Weblogic的安装与卸载

    一.下载weblogic 到Oracle官网https://www.oracle.com/downloads/index.html,我在这里下载的是weblogic12C进行安装:https://ww ...

  9. CentOS 6.8 部署django项目二

    CentOS 6.8 部署django项目一 1.项目部署后发现部分页面的样式丢失,是因为在nginx中配置的static路径中未包含. 解决:在settinfs.py中添加: STATIC_ROOT ...

  10. SLAM学习资料汇总

    转自 http://www.cnblogs.com/wenhust/   书籍: 1.必读经典 Thrun S, Burgard W, Fox D. <Probabilistic robotic ...