Codeforces 490F Treeland Tour 树形dp
离散化之后, 每个节点维护上升链和下降链, 感觉复杂度有点高, 为啥跑这么快。。
#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的更多相关文章
- Codeforces 490F. Treeland Tour 暴力+LIS
		
枚举根+dfs 它可以活 , 我不知道有什么解决的办法是积极的 ...... F. Treeland Tour time limit per test 5 seconds memory limit p ...
 - Codeforces 490F Treeland Tour(离散化 + 线段树合并)
		
题目链接 Treeland Tour 题目就是让你求树上LIS 先离散化,然后再线段树上操作.一些细节需要注意一下. #include <bits/stdc++.h> using name ...
 - Codeforces 490F Treeland Tour 树上的最长上升子序列
		
题目链接:点击打开链接 题意: 给定n个点的树. 以下n个数表示点权. 以下n-1行给出树. 找一条链,然后找出这条链中的点权组成的最长上升子序列. 求:最长上升子序列的长度. 思路: 首先是维护一条 ...
 - Codeforces 219D Choosing Capital for Treeland(树形DP)
		
题目是给一张边有向的树形图.要选出首都的点,首都要都能走到其他点,因此要反转一些边的方向.问可以选哪几个点作为首都,使它们所需反转边的数量最少. 这题挺好想的,因为做过HDU2196. 首先就不妨设正 ...
 - CF 219 D:Choosing Capital for Treeland(树形dp)
		
D. Choosing Capital for Treeland 链接:http://codeforces.com/problemset/problem/219/D The country Tre ...
 - codeforces 212E IT Restaurants(树形dp+背包思想)
		
题目链接:http://codeforces.com/problemset/problem/212/E 题目大意:给你一个无向树,现在用两种颜色去给这颗树上的节点染色.用(a,b)表示两种颜色分别染的 ...
 - Codeforces 123E Maze(树形DP+期望)
		
[题目链接] http://codeforces.com/problemset/problem/123/E [题目大意] 给出一棵,给出从每个点出发的概率和以每个点为终点的概率,求出每次按照dfs序从 ...
 - codeforces 709E E. Centroids(树形dp)
		
题目链接: E. Centroids time limit per test 4 seconds memory limit per test 512 megabytes input standard ...
 - ZOJ 3188 ZOJ 3188 Treeland Exhibition(树形DP)
		
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3278 题意:给出一棵树,找出一个不大于长度为m的链,使得其他点到该链 ...
 
随机推荐
- Phalcon的Mvc结构及启动流程(部分源码分析)
			
Phalcon本身有支持创建多种形式的Web应用项目以应对不同场景,包括迷你应用.单模块标准应用.以及较复杂的多模块应用 创建项目 Phalcon环境配置安装后,可以通过命令行生成一个标准的Phalc ...
 - 一个简单的使用Quartz和Oozie调度作业给大数据计算平台执行
			
一,介绍 Oozie是一个基于Hadoop的工作流调度器,它可以通过Oozie Client 以编程的形式提交不同类型的作业,如MapReduce作业和Spark作业给底层的计算平台(如 Cloude ...
 - LINQ to SQL 实现 GROUP BY、聚合、ORDER BY
			
Ø 前言 本示例主要实现 LINQ 查询,先分组,再聚合,最后在排序.示例很简单,但是使用 LINQ 却生成了不同的 SQL 实现. 1) 采用手动编写 SQL 实现 SELECT ROW_NU ...
 - C# 批量修改文件名
			
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
 - pip 报错
			
pip 安装 初始化系统 安装PiP 问题? 依赖包:yun install wget gcc gcc-c++ -y python 环境 wget http://www.python.org/ftp/ ...
 - Maven打包编译找不到com.sun.crypto.provider.SunJCE类
			
Maven配置 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>mav ...
 - tidb 架构~tidb 理论学习(1)
			
一 简介:介绍新型NEW SQL数据库tidb 二 目的: tidb出现的目的,就是代替mysql+中间件,实现横向水平扩展 三 核心理论观点 1 MySQL 是单机数据库,只能通过 XA 来满足跨数 ...
 - mongodb系列~mongodb的副本集(1)
			
一 简介: mongodb副本集 二 复制方式: 1 全量复制 2 增量复制三 同步检测过程: 一 正常情况下: 1 master执行语句,并将所有的修改数据库的操作以日志Oplog ...
 - 用accessKey设置快捷键
			
<!DOCTYPE html> <html> <body> <a href="http://www.w3school.com.cn/html/&qu ...
 - java读取视频文件时长
			
1.下载jar包:http://www.sauronsoftware.it/projects/jave/index.php 2.上代码 @RequestMapping(value = "am ...