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. keeplived + mysql双主复制部署 --原创

    环境: master 1: 192.168.100.10  oracle  linux 7.4  mysql 5.7.1 master 2: 192.168.100.11 oracle  linux ...

  2. Cookie , Session ,Session 劫持简单总结

    cookie 机制: Cookies 是 服务器 在 本地机器 上存储的 小段文本,并伴随着 每一个请求,发送到 同一台 服务器. 网络服务器 用 HTTP头 向客户端发送 Cookies.在客户端, ...

  3. hdu6127 Hard challenge

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6127 题目: Hard challenge Time Limit: 4000/2000 MS ...

  4. spark学习(基础篇)--(第三节)Spark几种运行模式

    spark应用执行机制分析 前段时间一直在编写指标代码,一直采用的是--deploy-mode client方式开发测试,因此执行没遇到什么问题,但是放到生产上采用--master yarn-clus ...

  5. Spring AOP (事务管理)

    一.声明式事务管理的概括 声明式事务(declarative transaction management)是Spring提供的对程序事务管理的方式之一. Spring的声明式事务顾名思义就是采用声明 ...

  6. doc命令下查看java安装路径

    在doc窗口下使用命令:set  java_home 即可查看.

  7. Dijkstra+优先队列

    /* Dijkstra的算法思想: 在所有没有访问过的结点中选出dis(s,x)值最小的x 对从x出发的所有边(x,y),更新 dis(s,y)=min(dis(s,y),dis(s,x)+dis(x ...

  8. Secure a Web API with Individual Accounts and Local Login in ASP.NET Web API 2.2

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/individual-accounts-in-web-api Ind ...

  9. 微服务(一)eureka

    Eureka概述 一个简单的服务注册,服务发现架构 在CAP理论中,eureka选择了ap,作为注册中心,数据可用比数据不一致更重要 逻辑架构图 Eureka特性 1.当注册中心挂了,客户端之间依然可 ...

  10. SQL优化的若干原则

    SQL语句:是对数据库(数据)进行操作的惟一途径:消耗了70%~90%的数据库资源:独立于程序设计逻辑,相对于对程序源代码的优化,对SQL语句的优化在时间成本和风险上的代价都很低:可以有不同的写法:易 ...