首先有朴素的\(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的更多相关文章

  1. 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 ...

  2. 牛客第八场 C-counting paths 树形dp计数

    题目地址 题意 给你一颗树 初始点颜色全部为白色 对于每一个满足要求一的点集s f(s)的定义为先把点集内的点染黑 满足要求二的路径集合数量 要求一为两两黑点之间不能出现白色的点 要求二为将这个路径集 ...

  3. 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 ...

  4. HDU4003Find Metal Mineral[树形DP 分组背包]

    Find Metal Mineral Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Other ...

  5. 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 ...

  6. hdu 4003 Find Metal Mineral 树形DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4003 Humans have discovered a kind of new metal miner ...

  7. poj3162(树形dp+优先队列)

    Walking Race Time Limit: 10000MS   Memory Limit: 131072K Total Submissions: 5409   Accepted: 1371 Ca ...

  8. POJ 3162.Walking Race 树形dp 树的直径

    Walking Race Time Limit: 10000MS   Memory Limit: 131072K Total Submissions: 4123   Accepted: 1029 Ca ...

  9. HDU 5293 Annoying problem 树形dp dfs序 树状数组 lca

    Annoying problem 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 Description Coco has a tree, w ...

随机推荐

  1. 2017 jq 总结

    $(el).parent()         获取父级元素 .parents('th')        获取祖元素th .parentsUntil("th")         找到 ...

  2. AutoCAD DevTV-AUTOCAD二次开发资源合集

    Webcast Language Date AutoCAD .Net - Session 2 English 13-Sep-12 AutoCAD .Net - Session 1 English 6- ...

  3. 【SLAM】安装 g2o_viewer

    2017年2月8日,那是一个阴天.为了完成高翔博士的<一起做RGB-D SLAM>教程,我在 Ubuntu 14.04 安装 g2o.遇到困难,怎奈我眼瞎,找错了方向,浪费时间,没有成功安 ...

  4. xss的一个tip

    其实可能不能算tip吧. 分享一下吧. unicode有四种编码方式 源文本:The &#x [Hex]:The &# [Decimal]:The \U [Hex]:\U0054\U0 ...

  5. 转载-SVN常用命令

    SVN(Subversion)是一个自由.开源的项目源代码版本控制工具.目前,绝大多数开源软件和企业代码管理,都使用SVN作为代码版本管理软件. Subversion将文件存放在中心版本库里,这个版本 ...

  6. scp加端口号

    scp -P 21110 root@192.168.0.1:/home/abc.txt root@192.168.0.2:/root 注意: 参数-P 的位置一定要紧跟在scp命令后面 参数-P 指的 ...

  7. Linux多线程的使用一:互斥锁

    多线程经常会在Linux的开发中用到,我想把平时的使用和思考记录下来,一是给自己做个备忘,二是分享给可能会用到的人. POSIX标准下互斥锁是pthread_mutex_t,与之相关的函数有: 1 i ...

  8. InfluxDB系列之一安装及简单运维(未完成,需要继续写)

    InfluxDB 是一个开源分布式时序.事件和指标数据库.使用 Go 语言编写,无需外部依赖.其设计目标是实现分布式和水平伸缩扩展. 它有三大特性: 1. Time Series (时间序列):你可以 ...

  9. sql的主键,int类型,自增,自动编号到了规定最大数,接下来数据库会怎么做

    答案:它会从1开始重新编号,但是避开已经重复的值.

  10. RabbitMQ--Publish/Subscribe(三)

    前言 在先前例子中我们创建了一个work queue,封装一个task到一个message中并发送到queue.一个worker(consumer)取出任务并执行. 像这种producer把messa ...