SPOJ6717 Two Paths 树形dp

首先有朴素的\(O(n^2)\)想法
首先枚举断边,之后对于断边之后的两棵子树求出直径
考虑优化这个朴素的想法
考虑换根\(dp\)
具体而言,首先求出\(f[i], fs[i]\)表示\(i\)号点向下的最长链以及\(i\)号子树内部最长的直径
并且在求出\(g[i]\)表示\(fa[i]\)在\(i\)号节点子树外的最长链
\(gs[i]\)表示\(i\)号节点子树外的直径
对于所有的\(fs[i] * gs[i]\)取\(max\)即为答案
首先一遍\(dfs\)把\(f, fs\)求出来
考虑怎么求\(gs[o]\),有以下几种可能
一条\(fa[o]\)引申出去的链 + \(fa[o]\) 除了\(o\)子树以外的最长链
\(gs[fa]\)
两条\(fa[o]\)除了\(o\)子树以外的链的和
用前缀和后缀来做到删除
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define ll long long
#define ri register int
#define rep(io, st, ed) for(ri io = st; io <= ed; io ++)
#define drep(io, ed, st) for(ri io = ed; io >= st; io --)
#define tpr template <typename ra>
tpr inline void cmin(ra &a, ra b) { if(a > b) a = b; }
tpr inline void cmax(ra &a, ra b) { if(a < b) a = b; }
#define gc getchar
inline int read() {
int p = 0, w = 1; char c = gc();
while(c > '9' || c < '0') { if(c == '-') w = -1; c = gc(); }
while(c >= '0' && c <= '9') p = p * 10 + c - '0', c = gc();
return p * w;
}
const int sid = 200050;
ll ans;
int n, cnp;
int cap[sid], nxt[sid], node[sid];
inline void addedge(int u, int v) {
nxt[++ cnp] = cap[u]; cap[u] = cnp; node[cnp] = v;
}
int f[sid], fs[sid];
int g[sid], gs[sid], q[sid], ps[sid], ss[sid];
#define cur node[i]
void dfs1(int o, int fa) {
for(int i = cap[o]; i; i = nxt[i])
if(cur != fa) {
dfs1(cur, o);
cmax(fs[o], fs[cur]);
cmax(fs[o], f[cur] + f[o] + 1);
cmax(f[o], f[cur] + 1);
}
}
void dfs2(int o, int fa) {
if(!fa) g[o] = -1;
int tot = 0;
int sx = 0, mx = 0, px = 0;
for(int i = cap[o]; i; i = nxt[i])
if(cur != fa) q[++ tot] = cur;
rep(i, 1, tot + 1) ps[i] = ss[i] = 0;
rep(i, 1, tot) {
int p = q[i];
cmax(g[p], g[o] + 1);
cmax(g[p], ps[i - 1]);
ps[i] = max(ps[i - 1], f[p] + 1);
}
drep(i, tot, 1) {
int p = q[i];
cmax(g[p], ss[i + 1]);
ss[i] = max(ss[i + 1], f[p] + 1);
}
rep(i, 1, tot) {
int p = q[i];
cmax(gs[p], ps[i - 1] + ss[i + 1]);
cmax(gs[p], gs[o]);
cmax(gs[p], g[o] + 1 + ps[i - 1]);
cmax(gs[p], g[o] + 1 + ss[i + 1]);
}
rep(i, 1, tot) {
int p = q[i];
cmax(gs[p], mx + sx);
cmax(gs[p], px); cmax(px, fs[p]);
if(f[p] + 1 >= mx) sx = mx, mx = f[p] + 1;
else if(f[p] + 1 >= sx) sx = f[p] + 1;
}
mx = sx = px = 0;
drep(i, tot, 1) {
int p = q[i];
cmax(gs[p], mx + sx);
cmax(gs[p], px); cmax(px, fs[p]);
if(f[p] + 1 >= mx) sx = mx, mx = f[p] + 1;
else if(f[p] + 1 >= sx) sx = f[p] + 1;
}
for(int i = cap[o]; i; i = nxt[i])
if(cur != fa) {
cmax(ans, 1ll * fs[cur] * gs[cur]);
dfs2(cur, o);
}
}
int main() {
n = read();
rep(i, 2, n) {
int u = read(), v =read();
addedge(u, v); addedge(v, u);
}
dfs1(1, 0); dfs2(1, 0);
printf("%lld\n", ans);
return 0;
}
SPOJ6717 Two Paths 树形dp的更多相关文章
- Codeforces Beta Round #14 (Div. 2) D. Two Paths 树形dp
D. Two Paths 题目连接: http://codeforces.com/contest/14/problem/D Description As you know, Bob's brother ...
- 牛客第八场 C-counting paths 树形dp计数
题目地址 题意 给你一颗树 初始点颜色全部为白色 对于每一个满足要求一的点集s f(s)的定义为先把点集内的点染黑 满足要求二的路径集合数量 要求一为两两黑点之间不能出现白色的点 要求二为将这个路径集 ...
- Codeforces Beta Round #14 (Div. 2) Two Paths (树形DP)
Two Paths time limit per test 2 seconds memory limit per test 64 megabytes input standard input outp ...
- HDU4003Find Metal Mineral[树形DP 分组背包]
Find Metal Mineral Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Other ...
- CF 337D Book of Evil 树形DP 好题
Paladin Manao caught the trail of the ancient Book of Evil in a swampy area. This area contains n se ...
- hdu 4003 Find Metal Mineral 树形DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4003 Humans have discovered a kind of new metal miner ...
- poj3162(树形dp+优先队列)
Walking Race Time Limit: 10000MS Memory Limit: 131072K Total Submissions: 5409 Accepted: 1371 Ca ...
- POJ 3162.Walking Race 树形dp 树的直径
Walking Race Time Limit: 10000MS Memory Limit: 131072K Total Submissions: 4123 Accepted: 1029 Ca ...
- HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca
Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...
随机推荐
- 在window 8 或windows2012 上用命令行安装framework3.5 方法
找到对应操作系统安装目录的sources文件夹下的sxs文件夹,拷贝到本地电脑,如F:盘 根目录下 CMD(管理员身份)命令: dism.exe /online /enable-feature /fe ...
- opencv 摄像头
VideoCapture cap(); if(!cap.isOpened()) ; Mat frame, edges; namedWindow(); for(;;) { cap >> fr ...
- Java并发编程(4)--生产者与消费者模式介绍
一.前言 这种模式在生活是最常见的,那么它的场景是什么样的呢? 下面是我假象的,假设有一个仓库,仓库有一个生产者和一个消费者,消费者过来消费的时候会检测仓库中是否有库存,如果没有了则等待生产,如果有就 ...
- [转]CNN目标检测(一):Faster RCNN详解
https://blog.csdn.net/a8039974/article/details/77592389 Faster RCNN github : https://github.com/rbgi ...
- SpringMVC控制器 跳转到jsp页面 css img js等文件不起作用 不显示
今天在SpringMVC转发页面的时候发现跳转页面确实成功,但是JS,CSS等静态资源不起作用: 控制层代码: /** * 转发到查看培养方案详情的页面 * @return */ @RequestMa ...
- 大规模实时流处理平台架构-zz
随着不同网络质量下接入终端设备种类的增多,服务端转码已经成为视频点播和直播产品中必备的能力之一.直播产品讲究时效性,希望在一定的时间内让所有终端看到不同尺寸甚至是不同质量的视频,因此对转码的实时性要求 ...
- 洛谷 P4093: bzoj 4553: [HEOI2016/TJOI2016]序列
题目传送门:洛谷P4093. 题意简述: 给定一个长度为 \(n\) 的序列 \(a\). 同时这个序列还可能发生变化,每一种变化 \((x_i,y_i)\) 对应着 \(a_{x_i}\) 可能变成 ...
- 87.在ModelSim中添加Xilinx ISE仿真库
在ModelSim中添加Xilinx ISE仿真库 说明: l ModelSim一定要安装在不带空格的目录下,即不要安装在“Program Files”目录下.如作者是安装在D:\softwares\ ...
- ADB常用命令(二)
参考 http://adbshell.com/commands 常用命令 查看adb 版本 adb version 打印所有附加模拟器/设备的列表 adb devices 设备序列号 adb get ...
- servlet Filter过滤javascript
新建HttpServletRequestWrapper子类XssHttpServletRequestWrapper import javax.servlet.http.HttpServletReque ...