HDU-4679 Terrorist’s destroy 树形DP,维护

时间:2024-01-11 12:02:32

  题目链接: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 ;
}