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

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

  1 //STATUS:C++_AC_875MS_11012KB

  2 #include <functional>

  3 #include <algorithm>

  4 #include <iostream>

  5 //#include <ext/rope>

  6 #include <fstream>

  7 #include <sstream>

  8 #include <iomanip>

  9 #include <numeric>

 10 #include <cstring>

 11 #include <cassert>

 12 #include <cstdio>

 13 #include <string>

 14 #include <vector>

 15 #include <bitset>

 16 #include <queue>

 17 #include <stack>

 18 #include <cmath>

 19 #include <ctime>

 20 #include <list>

 21 #include <set>

 22 #include <map>

 23 using namespace std;

 24 #pragma comment(linker,"/STACK:102400000,102400000")

 25 //using namespace __gnu_cxx;

 26 //define

 27 #define pii pair<int,int>

 28 #define mem(a,b) memset(a,b,sizeof(a))

 29 #define lson l,mid,rt<<1

 30 #define rson mid+1,r,rt<<1|1

 31 #define PI acos(-1.0)

 32 //typedef

 33 typedef __int64 LL;

 34 typedef unsigned __int64 ULL;

 35 //const

 36 const int N=100010;

 37 const int INF=0x3f3f3f3f;

 38 const LL MOD=1000000007,STA=8000010;

 39 const LL LNF=1LL<<55;

 40 const double EPS=1e-9;

 41 const double OO=1e50;

 42 const int dx[8]={-1,-1,0,1,1,1,0,-1};

 43 const int dy[8]={0,1,1,1,0,-1,-1,-1};

 44 const int day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

 45 //Daily Use ...

 46 inline int sign(double x){return (x>EPS)-(x<-EPS);}

 47 template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}

 48 template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}

 49 template<class T> inline T lcm(T a,T b,T d){return a/d*b;}

 50 template<class T> inline T Min(T a,T b){return a<b?a:b;}

 51 template<class T> inline T Max(T a,T b){return a>b?a:b;}

 52 template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}

 53 template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}

 54 template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}

 55 template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}

 56 //End

 57 

 58 struct Edge{

 59     int u,v,w,id;

 60 }e[N<<1];

 61 int first[N],next[N<<1],hign[N],hige[N<<1][2];

 62 int f[N],maxl1[N],maxr1[N],maxl2[N],maxr2[N],maxlf[N],maxrf[N],d[N],w[N];

 63 int n,mt;

 64 int T,ans,ansid;

 65 

 66 void adde(int a,int b,int c,int id)

 67 {

 68     e[mt].u=a,e[mt].v=b,e[mt].w=c,e[mt].id=id;

 69     next[mt]=first[a];first[a]=mt++;

 70     e[mt].u=b,e[mt].v=a,e[mt].w=c,e[mt].id=id;

 71     next[mt]=first[b];first[b]=mt++;

 72 }

 73 

 74 void dfs1(int u,int fa)

 75 {

 76     int i,j,v,cnt=1,flag=0;

 77     f[u]=0;

 78     for(i=first[u];i!=-1;i=next[i]){

 79         if((v=e[i].v)==fa)continue;

 80         dfs1(v,u);

 81         f[u]=Max(f[u],f[v]);

 82         flag=1;

 83     }

 84     if(!flag){f[u]=hign[u]=0;return;}

 85     for(i=first[u];i!=-1;i=next[i]){

 86         if((v=e[i].v)==fa)continue;

 87         w[cnt]=v;

 88         d[cnt++]=hign[v]+1;

 89     }

 90     maxl1[0]=maxr1[cnt]=maxl2[0]=maxr2[cnt]=maxlf[0]=maxrf[cnt]=0;

 91     for(i=1;i<cnt;i++){

 92         maxlf[i]=Max(maxlf[i-1],f[w[i]]);

 93         maxl1[i]=maxl1[i-1],maxl2[i]=maxl2[i-1];

 94         if(d[i]>maxl1[i])maxl2[i]=maxl1[i],maxl1[i]=d[i];

 95         else if(d[i]>maxl2[i])maxl2[i]=d[i];

 96     }

 97     for(i=cnt-1;i>0;i--){

 98         maxrf[i]=Max(maxrf[i+1],f[w[i]]);

 99         maxr1[i]=maxr1[i+1],maxr2[i]=maxr2[i+1];

100         if(d[i]>maxr1[i])maxr2[i]=maxr1[i],maxr1[i]=d[i];

101         else if(d[i]>maxr2[i])maxr2[i]=d[i];

102     }

103     for(j=1,i=first[u];i!=-1;i=next[i]){

104         if(e[i].v==fa)continue;

105         hige[i][0]=Max(maxl1[j-1],maxr1[j+1]);

106         hige[i][1]=Max(maxl1[j-1]+maxr1[j+1],

107                     maxl1[j-1]+maxl2[j-1],maxr1[j+1]+maxr2[j+1]);

108         hige[i][1]=Max(hige[i][1],maxlf[j-1],maxrf[j+1]);

109         j++;

110     }

111     f[u]=Max(f[u],maxr1[1]+maxr2[1]);

112     hign[u]=maxr1[1];

113 }

114 

115 void dfs2(int u,int fa,int max1,int max2)

116 {

117     int i,j,v,t1,t2,nt;

118     for(i=first[u];i!=-1;i=next[i]){

119         if((v=e[i].v)==fa)continue;

120         t1=Max(hige[i][1],max2,max1+hige[i][0]);

121         nt=Max(f[v],t1)*e[i].w;

122         if(ans>nt || (ans==nt && e[i].id<ansid)){

123             ans=nt;

124             ansid=e[i].id;

125         }

126         t2=Max(max1,hige[i][0])+1;

127         dfs2(v,u,t2,Max(t1,t2));

128     }

129 }

130 

131 int main(){

132  //   freopen("in.txt","r",stdin);

133     int i,j,a,b,c,ca=1;

134     scanf("%d",&T);

135     while(T--)

136     {

137         scanf("%d",&n);

138         mem(first,-1);mt=0;

139         for(i=1;i<n;i++){

140             scanf("%d%d%d",&a,&b,&c);

141             adde(a,b,c,i);

142         }

143 

144         dfs1(1,0);

145         ans=INF;

146         dfs2(1,0,0,0);

147 

148         printf("Case #%d: %d\n",ca++,ansid);

149     }

150     return 0;

151 }

 

你可能感兴趣的:(error)