# Terrorist’s destroy

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1212    Accepted Submission(s): 358

Problem Description
There is a city which is built like a tree.A terrorist wants to destroy the city's roads. But now he is alone, he can only destroy one road, then the city will be divided into two cities. Impression of the city is a number defined as the distance between the farthest two houses (As it relates to the fare).When the terrorist destroyed a road, he needs to spend some energy, assuming that the number is a.At the same time,he will get a number b which is maximum of the Impression of two cities. The terrorist wants to know which road to destroy so that the product of a and b will be minimized.You should find the road's id.
Note that the length of each road is one.

Input
The first line contains integer T(1<=T<=20), denote the number of the test cases.
For each test cases,the first line contains a integer n(1 < n <= 100000);denote the number of the houses;
Each of the following (n-1) lines contains third integers u,v,w, indicating there is a road between house u and houses v,and will cost terrorist w energy to destroy it.The id of these road is number from 1 to n-1.(1<=u<=n , 1<=v<=n , 1<=w<=10000)

Output
For each test case, output the case number first,and then output the id of the road which the terrorist should destroy.If the answer is not unique,output the smallest id.

Sample Input
2
5
4 5 1
1 5 1
2 1 1
3 5 1
5
1 4 1
1 3 1
5 1 1
2 5 1

Sample Output
Case #1: 2
Case #2: 3

Source

1.去掉非直径上的边，那么花费为 d*w；

2.去掉直径上的边，那么需要求生成的两颗子树中的直径的最大值，树中有个结论就是去掉一条边后生成的两颗子树最大直径肯定是原来的树中一个端点出发的路径，那么就可以考虑用对原来树的直径的两个端点两次dfs用树形dp来解决问题了。

(1).从v出发的最大边+从v出发的次大边。

(2).v的子树的最大直径。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define maxn 100005
#define MAXN 200005
#define mod 1000000007
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
typedef long long ll;
using namespace std;

int n,m,ans,cnt,anss;
int d,st,en;
int pp[maxn],vis[maxn];
int h[maxn],pre[maxn];  // 节点的深度  pre-上一个节点用于记录直径的路径
int ma[maxn],sma[maxn],mm[maxn],res[maxn];  // ma-最长边 sma-次长边 mm-最大直径 res-答案
struct Node
{
int v,w,id,next;
}edge[MAXN];

void addedge(int u,int v,int w,int id)
{
cnt++;
edge[cnt].v=v;
edge[cnt].w=w;
edge[cnt].id=id;
edge[cnt].next=pp[u];
pp[u]=cnt;
}
void dfs1(int u,int fa)
{
int i,j,v;
h[u]=h[fa]+1;
if(d<h[u])
{
st=u;
d=h[u];
}
for(i=pp[u];i;i=edge[i].next)
{
v=edge[i].v;
if(v!=fa) dfs1(v,u);
}
}
void dfs2(int u,int fa)
{
int i,j,v;
h[u]=h[fa]+1;
pre[u]=fa;
if(d<h[u])
{
en=u;
d=h[u];
}
for(i=pp[u];i;i=edge[i].next)
{
v=edge[i].v;
if(v!=fa) dfs2(v,u);
}
}
void dfs(int u,int fa)
{
int i,j,t,v;
ma[u]=sma[u]=mm[u]=0;
for(i=pp[u];i;i=edge[i].next)
{
v=edge[i].v;
if(v!=fa)
{
dfs(v,u);
if(sma[u]<ma[v]+1)
{
sma[u]=ma[v]+1;
if(sma[u]>ma[u]) swap(sma[u],ma[u]);
mm[u]=max(mm[u],sma[u]+ma[u]);  // 用自己的最长边+次长边更新
}
if(mm[u]<mm[v]) mm[u]=mm[v];  // 用儿子节点的最大直径更新
}
}
}
void solve(int u,int fa)
{
int i,j,t,v,w,id;
for(i=pp[u];i;i=edge[i].next)
{
v=edge[i].v;
id=edge[i].id;
w=edge[i].w;
if(v!=fa)
{
if(vis[v]) res[id]=max(res[id],mm[v]*w);
else res[id]=d*w;
solve(v,u);
}
}
}
int main()
{
int i,j,t,u,v,w,x,y,test=0;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
cnt=0;
memset(pp,0,sizeof(pp));
for(i=1;i<n;i++)
{
scanf("%d%d%d",&u,&v,&w);
}
d=h[0]=0;
dfs1(1,0); // dfs1、dfs2找树的直径
d=0;
dfs2(st,0);
d--;      // 直径大小
x=en;
memset(vis,0,sizeof(vis));
while(x)  // 标记直径路径上的点
{
vis[x]=1;
x=pre[x];
}
memset(res,0,sizeof(res));
dfs(st,0);  // 树形dp  求ma、sma 更新每个点的mm
solve(st,0); // 更新res
dfs(en,0);
solve(en,0);
ans=INF;
for(i=1;i<n;i++)
{
if(ans>res[i])
{
ans=res[i];
anss=i;
}
}
printf("Case #%d: %d\n",++test,anss);
}
return 0;
}
/*
1
16
1 7 1
3 7 1
3 4 1
4 5 1
4 15 1
6 4 1
6 2 1
2 13 1
2 11 1
11 9 1
6 8 1
8 10 1
10 14 1
10 12 1
12 16 1
ans:7 anss:1
*/


• 本文已收录于以下专栏：

## hdu4679 Terrorist’s destroy 解题报告

• ayecsz
• 2013年08月18日 15:50
• 370

## hdu 2196 Computer 树形DP或者树的直径

Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total...
• FTQOOO
• 2016年08月15日 13:00
• 198

## HDU 4612 and 4607 （tarjan求桥+树形dp求树的直径）

HDU 4612 题意：给你一个无向图，里面有桥，问你连给它加一条边，桥变为多少 题解：很明显是先求出桥的数量，然后缩点成一棵树，然后求树的直径，答案就是桥-直径 但是这题有20W点100W边，而且有...

## hdu2196(求树的直径或树形dp)

Computer Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total...

## 【poj1985】Cow Marathon 树形DP求树的直径

DescriptionAfter hearing about the epidemic of obesity in the USA, Farmer John wants his cows to get...
• LOI_DQS
• 2015年10月25日 20:27
• 763

## #bzoj2404#最长链(树的直径/树形DP)

2404: 最长链 时间限制: 1 Sec  内存限制: 128 MB 题目描述 给定一棵有n个节点的树，求每个节点到其他节点的最大距离 输入 输入第一行是一个自然数n(n≤10000), 接下来 (...

举报原因： 您举报文章：hdu 4679 Terrorist’s destroy ( 树形dp 树的直径 ) 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)