Terrorist’s destroy

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

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
       给出一颗树,每条边具有一个权值wi,定义一颗树的得分是这颗树上距离最远的两个点的距离,边的长度都是1.现在请选择一条边拆除,使得这条边上的权值与拆掉边后形成的两颗子树中最大的得分的乘积达到最小,输出这条边。
    我们要知道每颗子树的直径,双dfs处理出 fm[u],sm[u],tm[u]记录以u为起点的前三大的路径和连接自那个连接点,然后枚举所有边,注意查找子树直径的时不可以通过这条边了。还是要注意前三大的直径必须来自于不同的临界点,否则可能出现非单链的情况。
    第一次处理时没有计算出直径,,结果竟然能AC...,数据好弱啊,,后来改了改依然能AC.

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
#define inf 0x3f3f3f3f
#pragma comment(linker, "/STACK:102400000,102400000")
int first[],tot;
int baba[];
int fm[],sm[],tm[];
int fid[],sid[],tid[];
struct Edge
{
int u,v,w,next;
}e[];
void add(int u,int v,int w){
e[tot].u=u;
e[tot].v=v;
e[tot].w=w;
e[tot].next=first[u];
first[u]=tot++;
}
void dfs1(int u,int fa)
{
fm[u]=sm[u]=tm[u]=;
fid[u]=sid[u]=tid[u]=;
baba[u]=fa;
for(int i=first[u];i+;i=e[i].next){
int v=e[i].v;
if(v==fa) continue;
dfs1(v,u);
if(fm[v]+>tm[u]&&v!=fid[u]&&v!=sid[u]){
tm[u]=fm[v]+;
tid[u]=v;
if(tm[u]>sm[u]){
swap(tm[u],sm[u]);
swap(tid[u],sid[u]);
}
if(sm[u]>fm[u]){
swap(sm[u],fm[u]);
swap(sid[u],fid[u]);
}
}
if(sm[v]+>tm[u]&&v!=fid[u]&&v!=sid[u]){
tm[u]=sm[v]+;
tid[u]=v;
if(tm[u]>sm[u]){
swap(tm[u],sm[u]);
swap(tid[u],sid[u]);
}
if(sm[u]>fm[u]){
swap(sm[u],fm[u]);
swap(sid[u],fid[u]);
}
}
if(tm[v]+>tm[u]&&v!=fid[u]&&v!=sid[u]){
tm[u]=tm[v]+;
tid[u]=v;
if(tm[u]>sm[u]){
swap(tm[u],sm[u]);
swap(tid[u],sid[u]);
}
if(sm[u]>fm[u]){
swap(sm[u],fm[u]);
swap(sid[u],fid[u]);
}
}
}
}
void dfs2(int u,int fa)
{
if(fid[fa]!=u&&fid[u]!=fa&&sid[u]!=fa&&fm[fa]+>tm[u]){
tm[u]=fm[fa]+;
tid[u]=fa;
if(tm[u]>sm[u]){
swap(tm[u],sm[u]);
swap(tid[u],sid[u]);
}
if(sm[u]>fm[u]){
swap(sm[u],fm[u]);
swap(sid[u],fid[u]);
}
}
if(sid[fa]!=u&&fid[u]!=fa&&sid[u]!=fa&&sm[fa]+>tm[u]){
tm[u]=sm[fa]+;
tid[u]=fa;
if(tm[u]>sm[u]){
swap(tm[u],sm[u]);
swap(tid[u],sid[u]);
}
if(sm[u]>fm[u]){
swap(sm[u],fm[u]);
swap(sid[u],fid[u]);
}
}
if(tid[fa]!=u&&fid[u]!=fa&&sid[u]!=fa&&tm[fa]+>tm[u]){
tm[u]=tm[fa]+;
tid[u]=fa;
if(tm[u]>sm[u]){
swap(tm[u],sm[u]);
swap(tid[u],sid[u]);
}
if(sm[u]>fm[u]){
swap(sm[u],fm[u]);
swap(sid[u],fid[u]);
}
}
for(int i=first[u];~i;i=e[i].next){
int v=e[i].v;
if(v==fa) continue;
dfs2(v,u);
}
}
int main()
{
int n,i,j,k,u,v,w;
int t,cas=;
cin>>t;
while(t--){
memset(first,-,sizeof(first));
tot=;
scanf("%d",&n);
for(i=;i<n;++i){
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
dfs1(,);
dfs2(,);
/*for(i=1;i<=n;++i)
cout<<fm[i]<<' '<<sm[i]<<' '<<tm[i]<<endl;*/
int ans=inf,id=;
for(i=;i<tot;i+=){
u=e[i].u,v=e[i].v,w=e[i].w;
int t1=,t2=;
if(baba[v]==u){
t1=,t2=;
if(fid[u]!=v&&sid[u]!=v){
t1=max(t1,fm[u]+sm[u]);
}
else if(sid[u]!=v&&tid[u]!=v){
t1=max(t1,sm[u]+tm[u]);
}
else t1=max(t1,fm[u]+tm[u]); if(fid[v]!=u&&sid[v]!=u){
t2=max(t2,fm[v]+sm[v]);
}
else if(sid[v]!=u&&tid[v]!=u){
t2=max(t2,sm[v]+tm[v]);
}
else t2=max(t2,fm[v]+tm[v]); }
else{
if(fid[u]!=v&&sid[u]!=v){
t2=max(t2,fm[u]+sm[u]);
}
else if(sid[u]!=v&&tid[u]!=v){
t2=max(t2,sm[u]+tm[u]);
}
else t2=max(t2,fm[u]+tm[u]); if(fid[v]!=u&&sid[v]!=u){
t1=max(t1,fm[v]+sm[v]);
}
else if(sid[v]!=u&&tid[v]!=u){
t1=max(t1,sm[v]+tm[v]);
}
else t1=max(t1,fm[v]+tm[v]);
}
if(w*max(t1,t2)<ans){
ans=w*max(t1,t2);
id=i;
}
}
printf("Case #%d: %d\n",++cas,(id+)/);
}
return ;
}

HDU-4679-树的直径(树形dp)的更多相关文章

  1. 算法笔记--树的直径 && 树形dp && 虚树 && 树分治 && 树上差分 && 树链剖分

    树的直径: 利用了树的直径的一个性质:距某个点最远的叶子节点一定是树的某一条直径的端点. 先从任意一顶点a出发,bfs找到离它最远的一个叶子顶点b,然后再从b出发bfs找到离b最远的顶点c,那么b和c ...

  2. Codeforces 633F 树的直径/树形DP

    题意:有两个小孩玩游戏,每个小孩可以选择一个起始点,并且下一个选择的点必须和自己选择的上一个点相邻,问两个选的点权和的最大值是多少? 思路:首先这个问题可以转化为求树上两不相交路径的点权和的最大值,对 ...

  3. 2014 Super Training #9 E Destroy --树的直径+树形DP

    原题: ZOJ 3684 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3684 题意: 给你一棵树,树的根是树的中心(到其 ...

  4. hdu 4679 Terrorist’s destroy 树形DP

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

  5. [10.12模拟赛] 老大 (二分/树的直径/树形dp)

    [10.12模拟赛] 老大 题目描述 因为 OB 今年拿下 4 块金牌,学校赞助扩建劳模办公室为劳模办公室群,为了体现 OI 的特色,办公室群被设计成了树形(n 个点 n − 1 条边的无向连通图), ...

  6. HDU4514 湫湫系列故事——设计风景线 ——树的直径/树形dp+判环

    中文题面,给出一个图,问能不能成环,如果可以就输出YES.否则输出该树的直径. 这里的判环我们用路径压缩的并查集就能很快的判断出来,可以在输入的同时进行判断.这题重点就是求树的直径. 树直径的性质可以 ...

  7. hdu 4679 树的直径

    /* 题目大意:给n个点n-1条边的树,求删除哪条边时两个树中最大的直径与边权的乘积最小. 树的直径(Diameter)是指树上的最长简单路. 直径的求法:两遍BFS (or DFS) 若删除的边不是 ...

  8. POJ 1849 Two(树的直径--树形DP)(好题)

    大致题意:在某个点派出两个点去遍历全部的边,花费为边的权值,求最少的花费 思路:这题关键好在这个模型和最长路模型之间的转换.能够转换得到,全部边遍历了两遍的总花费减去最长路的花费就是本题的答案,要思考 ...

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

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

  10. hdu 4514 并查集+树形dp

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

随机推荐

  1. Linux环境下Netstat与PS的使用

    Linux下用netstat查看网络状态.端口状态 在linux一般使用netstat 来查看系统端口使用情况步. netstat命令是一个监控TCP/IP网络的非常有用的工具,它可以显示路由表.实际 ...

  2. WebUploader 上传插件结合bootstrap的模态框使用时选择上传文件按钮无效问题的解决方法

    由于种种原因(工作忙,要锻炼健身,要看书,要学习其他兴趣爱好,谈恋爱等),博客已经好久没有更新,为这个内心一直感觉很愧疚,今天开始决定继续更新博客,每周至少一篇,最多不限篇幅. 今天说一下,下午在工作 ...

  3. spark streaming基础知识1

    1.怎么理解spark streaming中的dstream? 它是spark streaming的基础数据结构,代表着(time,RDD)序列,有两种生成方式,一种是基于流数据创建(kafka,so ...

  4. oracle过程书写规范

    ORACLE存储过程编码规范 1.1         变量规范 Ø        变量名全部采用小写,局部变量名使用“v_”开头,输入参数以“i_开头,输出参数以“o_”开头,输入输出参数用io_开头 ...

  5. 对于JVM中方法区,永久代,元空间以及字符串常量池的迁移和string.intern方法

    在Java虚拟机(以下简称JVM)中,类包含其对应的元数据,比如类的层级信息,方法数据和方法信息(如字节码,栈和变量大小),运行时常量池,已确定的符号引用和虚方法表. 在过去(当自定义类加载器使用不普 ...

  6. 优秀 H5 案例收集 vol.4(不定期更新)

    重返木叶村 http://hyrz.qq.com/act/a20160113muyecun/index.html 飞越淘宝奇市 https://g.alicdn.com/fdilab/flyover- ...

  7. 20145307《信息安全系统设计基础》第五周学习总结PT2

    20145307<信息安全系统设计基础>第五周学习总结PT2: 教材学习内容总结 之前有第一部分学习总结: http://www.cnblogs.com/Jclemo/p/5962219. ...

  8. 20145314郑凯杰《信息安全系统设计基础》第9周学习总结 PART B

    20145314郑凯杰<信息安全系统设计基础>第9周学习总结 PART B 明确教材学习目标 注意每个系统调用的参数.返回值,会查帮助文档 阅读教材,完成课后练习(书中有参考答案),考核: ...

  9. Spring MVC工作流程图

    图一   图二    Spring工作流程描述       1. 用户向服务器发送请求,请求被Spring 前端控制Servelt DispatcherServlet捕获:       2. Disp ...

  10. 【转载】showModalDialog returnValue is undefined in Google Chrome

    showModalDialog returnValue is undefined in Google Chrome Posted on August 22, 2012by briancaos For ...