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. 工作笔记——js与文件上传下载

    1 js判断上传文件的后缀名,文件大小 //判断照片大小 function getPhotoSize(obj){ photoExt=obj.value.substr(obj.value.lastInd ...

  2. 微信小程序组件action-sheet

    操作反馈action-sheet:官方文档 Demo Code: Page({ data: { actionSheetHidden: true, actionSheetItems: ['item1', ...

  3. Leetcode 357

    没用过Leetcode刷题,只能按照自己的想法随便写写了 思路:1.第一位数有9种(除了0)可能,第二位数有9种(除了第一位)可能,第三位数有8种(除了前两位)可能,以此类推...9*8*7*...( ...

  4. XDU 1098 (欧拉函数模板题)

    原题链接,点击此处 欧拉函数:φ(N)表示对一个正整数N,欧拉函数是小于N且与N互质的数的个数 通式:φ(x) = x(1-1/p1)(1-1/p2)(1-1/p3)(1-1/p4)…..(1-1/p ...

  5. Restful概念

    文章节选自: http://www.ruanyifeng.com/blog/2011/09/restful https://www.zhihu.com/question/28557115/answer ...

  6. 如何在idea中设置 jsp 内容修改以后,立即生效而不用重新启动服务?

    点击 run---->edit configuration--->

  7. $Linux vi/vim编辑器常用命令与用法总结 (Markdown编辑版)

    vi/vim是什么? Linux世界几乎所有的配置文件都是以纯文本形式存在的,而在所有的Linux发行版系统上都有vi编辑器,因此利用简单的文字编辑软件就能够轻松地修改系统的各种配置了,非常方便.vi ...

  8. ABP官方文档翻译 2.4 日志

    日志 服务端 获取记录器 基类日志记录器 配置 Abp.Castle.Log4Net包 客户端 服务端 ABP使用Castle Windsor`s 日志设备.它可以使用不同的日志类库:Log4Net, ...

  9. matlab基本操作总结

    1.clear;//清除工作平台变量2.close all;//关闭打开的图形窗口3.I = imread('C:\Users\dell\Desktop\rice.jpg');//读取图像,存储在I数 ...

  10. SQL条件!=null查不出数据

    今天有一条sql需要某两个字段不能为空,当然是不能为null也不能为空字符串啦. 然后就开始写 WHERE ( order_amount != null and order_amount != '' ...