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. jQuery 选择城市,显示对应的即时时区时间

    因客户需要,我们CRM系统中,jQuery 弄个时区插件 如图: HTML: <div id="cityDate"> <i class="P_arrow ...

  2. VS2017的安装和配置

    VS2017专业版安装文件下载:链接:https://pan.baidu.com/s/1tJRYdj_9LzvTSDF5TkkMRg   提取码:tgh5    一些窗口:菜单栏--->视图 字 ...

  3. 信息收集之censys

    一.摘要 Censys提供了search.view.report.query.export以及data六种API接口. search接口的请求地址是https://www.censys.io/api/ ...

  4. Sqoop异常:Please set $ACCUMULO_HOME to the root of your Accumulo installation.

    shell-init: 获取当前目录时出错: getcwd: 无法访问父目录: 没有那个文件或目录chdir: 获取当前目录时出错: getcwd: 无法访问父目录: 没有那个文件或目录chdir: ...

  5. <转载>Mac下,使用sshpass让iterm2支持多ssh登录信息保存

    windows里有个Xshell非常的方便好使,因为它能保存你所有的ssh登录帐号信息.MAC下并没有xshell,有些也提供这样的功能,但效果都不好.iterm2是很好的终端,但却不能很好的支持多p ...

  6. dfs(通过控制点的编号来得出每个点的坐标)

    题目链接:https://cn.vjudge.net/contest/234497#problem/A #include<iostream> #include<string> ...

  7. mysql 案例 ~ 瘦身mysql系列(1)

    一 简介:这一系列我们要进行如何瘦身mysql 二 目的:通过提高CPU利用率和节约成本,降低数据库容量及I/O负载,从而使数据吞吐率得到显著提高 三 方法: 利用innodb的COMPRESSED ...

  8. aar jar包打包

    使用Android Studio Module的方式编译出aar和jar包: aar包:打aar包时,gradle compile依赖编译进来的包不会被打到aar包中,所以接入aar的应用仍然需要添加 ...

  9. pl/sql Devloper 快捷键__新建sql窗口

    首先,打开PLSQL,菜单栏--->首选项----->键配置 其次,点击你要增加快捷键的选项,直接键盘上输入快捷键: 比如你要修改为CTRL+N,直接在键盘上按出CTRL+N即可. ESC ...

  10. .NET CORE 1.1 迁移到.NET 2.0正式版

    以下操作参考官方文档 1:首先你需要升级到最新版的VS 2017 15.3 升级的地方在VS右上角有个黄色的更新提醒,如果没有请挂VPN或者重新下载一个新的. 2:第二步 和之前改.NET Frame ...