寻找树上最大权值和的两条不相交的路径。

树形DP题。挺难的,对于我……

定义三个变量ma[MAXN], t[MAXN], sum[MAXN]

其中,ma[i]代表i子树中,最长的路径和

t[i]代表i子树中,用来维护已有一条路径,而且还有一条链从叶子节点到i,则可以从根节点i向上扩展。如下图,维护红色部分

sum[i]维护从某叶子节点到根节点i的最长路径。

转移方程可以看代码,很容易明白

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <set>
#define LL long long using namespace std; const int MAXN = 100050;
const int MOD = 1e9 + 7; LL ans;
LL ma[MAXN], t[MAXN], sum[MAXN];
LL l[MAXN], ml[MAXN], r[MAXN], mr[MAXN];
bool leaf[MAXN];
LL mm[10];
bool vis[MAXN];
int head[MAXN];
struct Edge{
int u, v;
int next;
}edge[MAXN * 2];
int weight[MAXN], par[MAXN];
int n, tot; void addedge(int u, int v){
edge[tot].u = u;
edge[tot].v = v;
edge[tot].next = head[u];
head[u] = tot++;
} void dfs(int u){
vis[u] = true;
leaf[u] = true;
for(int e = head[u]; e != -1; e = edge[e].next){
int v = edge[e].v;
if(vis[v]) continue;
par[v] = u;
dfs(v);
leaf[u] = false;
}
} void slove(int u){
if(leaf[u]){
ma[u] = t[u] = sum[u] = weight[u];
return ;
} LL m1, m2, M1, M2;
m1 = m2 = M1 = M2 = 0;
for(int e = head[u]; e != -1; e = edge[e].next){
int v = edge[e].v;
if(v != par[u]){
slove(v);
if(ma[v] >= M1){
M2 = M1, M1 = ma[v];
}
else if(ma[v] > M2){
M2 = ma[v];
}
if(sum[v] >= m1){
m2 = m1, m1 = sum[v];
}
else if(sum[v] > m2){
m2 = sum[v];
}
t[u] = max(t[u], t[v] + weight[u]);
}
}
ma[u] = max(M1, m1 + m2 + weight[u]);
sum[u] = m1 + weight[u];
ans = max(ans, M1 + M2); int counts = 0;
for(int e = head[u]; e != -1; e = edge[e].next){
int v = edge[e].v;
if(v != par[u]){
l[++counts] = sum[v];
r[counts] = sum[v];
}
}
l[0] = ml[0] = r[counts + 1] = mr[counts + 1] = 0; //从左往右寻找最大的两个sum for(int i = 1; i <= counts ; i++){
if(l[i] > l[i - 1]) ml[i] = l[i - 1];
else if(l[i] > ml[i - 1]){
ml[i] = l[i];
l[i] = l[i - 1];
}
else{
l[i] = l[i - 1], ml[i] = ml[i - 1];
}
} //从右往左。。。。 for(int i = counts; i >= 1; i--){
if(r[i] > r[i + 1]) mr[i] = r[i + 1];
else if(r[i] > mr[i + 1]){
mr[i] = r[i];
r[i] = r[i + 1];
}
else{
r[i] = r[i + 1], mr[i] = mr[i + 1];
}
} counts = 0;
for(int e = head[u]; e != -1; e = edge[e].next){
int v = edge[e].v;
if(v == par[u]) continue;
counts ++;
mm[0] = l[counts - 1], mm[1] = ml[counts - 1];
mm[2] = r[counts + 1], mm[3] = mr[counts + 1]; sort(mm, mm + 4); ans = max(ans, weight[u] + ma[v] + mm[3] + mm[2]);
ans = max(ans, weight[u] + mm[3] + t[v]);
t[u] = max(t[u], ma[v] + weight[u] + mm[3]);
} } int main(){
scanf("%d", &n);
memset(head, -1, sizeof(head));
// memset(vis, false, sizeof(vis));
// memset(leaf, false, sizeof(leaf));
tot = 0;
memset(t, 0, sizeof(t));
for(int i = 1; i <= n; i++){
scanf("%d", &weight[i]);
}
int u, v;
memset(par, -1, sizeof(par));
for(int i = 0; i < n - 1; i++){
scanf("%d%d", &u, &v);
addedge(u, v);
addedge(v, u);
}
dfs(1);
ans = 0;
slove(1); cout << ans << endl;
}

  

Manthan, Codefest 16 F的更多相关文章

  1. Manthan, Codefest 16

    暴力 A - Ebony and Ivory import java.util.*; import java.io.*; public class Main { public static void ...

  2. CF Manthan, Codefest 16 G. Yash And Trees 线段树+bitset

    题目链接:http://codeforces.com/problemset/problem/633/G 大意是一棵树两种操作,第一种是某一节点子树所有值+v,第二种问子树中节点模m出现了多少种m以内的 ...

  3. Manthan, Codefest 16 H. Fibonacci-ish II 大力出奇迹 莫队 线段树 矩阵

    H. Fibonacci-ish II 题目连接: http://codeforces.com/contest/633/problem/H Description Yash is finally ti ...

  4. Manthan, Codefest 16 C. Spy Syndrome 2 字典树 + dp

    C. Spy Syndrome 2 题目连接: http://www.codeforces.com/contest/633/problem/C Description After observing ...

  5. Manthan, Codefest 16 C

    建trie树,刚好字符串是反向的,直接在原图上向前搜索就OK了……………… 可怜的我竟然用了RK来hash,在test67那里T了…… 贴个RK的 #include <iostream> ...

  6. Manthan, Codefest 16 D. Fibonacci-ish

    D. Fibonacci-ish time limit per test 3 seconds memory limit per test 512 megabytes input standard in ...

  7. Manthan, Codefest 16(B--A Trivial Problem)

    B. A Trivial Problem time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  8. Manthan, Codefest 16 -C. Spy Syndrome 2

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

  9. Manthan, Codefest 16 -A Ebony and Ivory

    time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standa ...

随机推荐

  1. 260 Single Number III 数组中除了两个数外,其他的数都出现了两次,找出这两个只出现一次的数

    给定一个整数数组 nums,其中恰好有两个元素只出现一次,其他所有元素均出现两次. 找出只出现一次的那两个元素.示例:给定 nums = [1, 2, 1, 3, 2, 5], 返回 [3, 5].注 ...

  2. LN : leetcode 338 Counting Bits

    lc 338 Counting Bits 338 Counting Bits Given a non negative integer number num. For every numbers i ...

  3. Linux学习日记之crontab使用notify-send实现每小时通知提醒

    crontab命令用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于“crontab”文件中,以供之后读取和执行 通过crontab -e 可以打开编辑文件添加新的命令 notif ...

  4. C++学习笔记(三)之函数库

    1.标准库函数 begin end begin 返回数组首地址 end   返回数组尾地址 2.const 在声明变量时对变量限制为只读,不允许修改 const int i = 5; 单个const作 ...

  5. WebView浅谈

    课程Demo public class MainActivity extends Activity { private String url = "http://baidu.com/&quo ...

  6. 隐藏win10任务栏输入法M图标

    在任务栏右键=>任务栏设置=>打开或关闭系统图标=>(关闭)输入指示

  7. codeforces_304C_数学题

    C. Lucky Permutation Triple time limit per test 2 seconds memory limit per test 256 megabytes input ...

  8. Jmeter的属性和变量

    jmeter的属性和变量可以简单理解为编程里面的全局变量和局部变量.属性是全局可见,可以跨线程组传递调用,而变量基本上只能存在于一个线程组中(在测试计划定义的变量也是可以跨线程组传递的).同线程组内的 ...

  9. 梦想CAD控件打印相关

    一.打印设置 在顶部快速访问工具栏单击打印按钮或者直接输入PLOT命令或者点击打印控制的打印设置按钮打开打印对话框.c#代码实现如下: //打印设置 private void Print1() {   ...

  10. 【转载】原 IntelliJ IDEA (idea)引入eclipse web项目

    原文地址:http://my.oschina.net/u/1170781/blog/192731 摘要 概述IntelliJ IDEA,以后都简称为idea,鼓捣了很久,看了很多例子才搞出来,希望对其 ...