题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4679

  题意:给一颗树,每个边有一个权值,要你去掉一条边权值w剩下的两颗子树中分别的最长链a,b,使得w*Min(a,b)最小。。

  说白了就是要枚举每条边,然后在O(1)的时间内求出两颗子树的最长链。因此我们可以考虑用树形DP,首先一遍DFS,对于每个节点维护两个信息,hign[u]:u为根节点的子树的深度,f[u]:u为根节点的子树的最长链。然后还要维护一个hige[i][0]和hige[i][1],分别表示u为根节点的子树,不包括边 i 的深度和最长链。然后再一遍DFS,根据上一节点的信息递推过去就可以在O(1)的时间内求出来了,总复杂度O(E)。有些细节要考虑,开始把全局变量搞混,wa了几次T^T。。

 //STATUS:C++_AC_875MS_11012KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=1;
const int INF=0x3f3f3f3f;
const LL MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e50;
const int dx[]={-,-,,,,,,-};
const int dy[]={,,,,,-,-,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End struct Edge{
int u,v,w,id;
}e[N<<];
int first[N],next[N<<],hign[N],hige[N<<][];
int f[N],maxl1[N],maxr1[N],maxl2[N],maxr2[N],maxlf[N],maxrf[N],d[N],w[N];
int n,mt;
int T,ans,ansid; void adde(int a,int b,int c,int id)
{
e[mt].u=a,e[mt].v=b,e[mt].w=c,e[mt].id=id;
next[mt]=first[a];first[a]=mt++;
e[mt].u=b,e[mt].v=a,e[mt].w=c,e[mt].id=id;
next[mt]=first[b];first[b]=mt++;
} void dfs1(int u,int fa)
{
int i,j,v,cnt=,flag=;
f[u]=;
for(i=first[u];i!=-;i=next[i]){
if((v=e[i].v)==fa)continue;
dfs1(v,u);
f[u]=Max(f[u],f[v]);
flag=;
}
if(!flag){f[u]=hign[u]=;return;}
for(i=first[u];i!=-;i=next[i]){
if((v=e[i].v)==fa)continue;
w[cnt]=v;
d[cnt++]=hign[v]+;
}
maxl1[]=maxr1[cnt]=maxl2[]=maxr2[cnt]=maxlf[]=maxrf[cnt]=;
for(i=;i<cnt;i++){
maxlf[i]=Max(maxlf[i-],f[w[i]]);
maxl1[i]=maxl1[i-],maxl2[i]=maxl2[i-];
if(d[i]>maxl1[i])maxl2[i]=maxl1[i],maxl1[i]=d[i];
else if(d[i]>maxl2[i])maxl2[i]=d[i];
}
for(i=cnt-;i>;i--){
maxrf[i]=Max(maxrf[i+],f[w[i]]);
maxr1[i]=maxr1[i+],maxr2[i]=maxr2[i+];
if(d[i]>maxr1[i])maxr2[i]=maxr1[i],maxr1[i]=d[i];
else if(d[i]>maxr2[i])maxr2[i]=d[i];
}
for(j=,i=first[u];i!=-;i=next[i]){
if(e[i].v==fa)continue;
hige[i][]=Max(maxl1[j-],maxr1[j+]);
hige[i][]=Max(maxl1[j-]+maxr1[j+],
maxl1[j-]+maxl2[j-],maxr1[j+]+maxr2[j+]);
hige[i][]=Max(hige[i][],maxlf[j-],maxrf[j+]);
j++;
}
f[u]=Max(f[u],maxr1[]+maxr2[]);
hign[u]=maxr1[];
} void dfs2(int u,int fa,int max1,int max2)
{
int i,j,v,t1,t2,nt;
for(i=first[u];i!=-;i=next[i]){
if((v=e[i].v)==fa)continue;
t1=Max(hige[i][],max2,max1+hige[i][]);
nt=Max(f[v],t1)*e[i].w;
if(ans>nt || (ans==nt && e[i].id<ansid)){
ans=nt;
ansid=e[i].id;
}
t2=Max(max1,hige[i][])+;
dfs2(v,u,t2,Max(t1,t2));
}
} int main(){
// freopen("in.txt","r",stdin);
int i,j,a,b,c,ca=;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
mem(first,-);mt=;
for(i=;i<n;i++){
scanf("%d%d%d",&a,&b,&c);
adde(a,b,c,i);
} dfs1(,);
ans=INF;
dfs2(,,,); printf("Case #%d: %d\n",ca++,ansid);
}
return ;
}

HDU-4679 Terrorist’s destroy 树形DP,维护的更多相关文章

  1. hdu 4679 Terrorist’s destroy 树形DP

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4679 题意:给定一颗树,每条边有一个权值w,问切掉哪条边之后,分成的两颗树的较大的直径*切掉边的权值最小? ...

  2. HDU 4679 Terrorist’s destroy (2013多校8 1004题 树形DP)

    Terrorist’s destroy Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Othe ...

  3. hdu 4679 Terrorist’s destroy 树的直径+dp

    题意:给你一棵树,每条边都有值W,然后问你去掉一条边,令val = w*max(两颗新树的直径),求val最小值~ 做法,先求树的直径,然后算出直径上每个点的最长枝条长度.这样对于每一条边,假如是枝条 ...

  4. HDU 4679 Terrorist’s destroy

    如果不在最长路的边,那么肯定是w*最长路. 如果在最长路,那么把最长路分成两段,左边树的最长路就是左段+左边点的次短路(不包含最长路上的点的最长路) ,右边同理. 还有就是更新,经过左端点的最长路,不 ...

  5. [HDU 5293]Tree chain problem(树形dp+树链剖分)

    [HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...

  6. hdu 4514 并查集+树形dp

    湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  7. HDU 4123 (2011 Asia FZU contest)(树形DP + 维护最长子序列)(bfs + 尺取法)

    题意:告诉一张带权图,不存在环,存下每个点能够到的最大的距离,就是一个长度为n的序列,然后求出最大值-最小值不大于Q的最长子序列的长度. 做法1:两步,第一步是根据图计算出这个序列,大姐头用了树形DP ...

  8. HDU 4123 Bob’s Race 树形dp+单调队列

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4123 Time Limit: 5000/2000 MS (Java/Others) Memory L ...

  9. HDU 5293 Tree chain problem 树形dp+dfs序+树状数组+LCA

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5293 题意: 给你一些链,每条链都有自己的价值,求不相交不重合的链能够组成的最大价值. 题解: 树形 ...

随机推荐

  1. CURL与PHP-CLI的应用【CLI篇】

    CLI的普通应用 什么是PHP-CLI php-cli是php Command Line Interface的简称,即PHP命令行接口,在windows和linux下都是支持PHP-CLI模式的; 为 ...

  2. SQLite入门与分析(三)---内核概述(1)

    写在前面:从本章开始,我们开始进入SQLite的内核.为了能更好的理解SQLite,我先从总的结构上讨论一下内核,从全局把握SQLite很重要.SQLite的内核实现不是很难,但是也不是很简单.总的来 ...

  3. iOS开发--TableView详细解释

    -.建立 UITableView  DataTable = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 420)];  [Data ...

  4. 二维图形的矩阵变换(三)——在WPF中的应用矩阵变换

    原文:二维图形的矩阵变换(三)--在WPF中的应用矩阵变换 UIElement和RenderTransform 首先,我们来看看什么样的对象可以进行变换.在WPF中,用于呈现给用户的对象的基类为Vis ...

  5. MyBatis的动态SQL操作--插入

    需求:向数据库中插入一条数据 //id,name,sal非空,三个字段都插入 insert into student(id,name,sal) values (?,?,?) //id,name非空,只 ...

  6. Git教程之分支管理之二

    分支管理策略 通常,合并分支时,如果可能,Git会用Fast forward模式,但这种模式下,删除分支后,会丢掉分支信息.如果要强制禁用Fast forward模式,Git就会在merge时生成一个 ...

  7. php 23种设计模式的趣味解释

    http://wenku.baidu.com/link?url=GwvuvSOdJneZQc-DSKoGmPcxTtzn3cdtIp3fRaCNbkg1zJDZZZTx2NwEK5IsqU996fG3 ...

  8. Lua从入门到精通

    1. 入门指南 http://www.cnblogs.com/linbc/archive/2009/06/02/1494622.html

  9. JsonPath详解

    JsonPath is to JSON what XPATH is to XML, a simple way to extract parts of a given document. JsonPat ...

  10. jquery网页倒计时效果,秒杀,限时抢购!

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...