HDU-4679-树的直径(树形dp)
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
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.
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)
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.
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
Case #2: 3
#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)的更多相关文章
- 算法笔记--树的直径 && 树形dp && 虚树 && 树分治 && 树上差分 && 树链剖分
树的直径: 利用了树的直径的一个性质:距某个点最远的叶子节点一定是树的某一条直径的端点. 先从任意一顶点a出发,bfs找到离它最远的一个叶子顶点b,然后再从b出发bfs找到离b最远的顶点c,那么b和c ...
- Codeforces 633F 树的直径/树形DP
题意:有两个小孩玩游戏,每个小孩可以选择一个起始点,并且下一个选择的点必须和自己选择的上一个点相邻,问两个选的点权和的最大值是多少? 思路:首先这个问题可以转化为求树上两不相交路径的点权和的最大值,对 ...
- 2014 Super Training #9 E Destroy --树的直径+树形DP
原题: ZOJ 3684 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3684 题意: 给你一棵树,树的根是树的中心(到其 ...
- hdu 4679 Terrorist’s destroy 树形DP
链接:http://acm.hdu.edu.cn/showproblem.php?pid=4679 题意:给定一颗树,每条边有一个权值w,问切掉哪条边之后,分成的两颗树的较大的直径*切掉边的权值最小? ...
- [10.12模拟赛] 老大 (二分/树的直径/树形dp)
[10.12模拟赛] 老大 题目描述 因为 OB 今年拿下 4 块金牌,学校赞助扩建劳模办公室为劳模办公室群,为了体现 OI 的特色,办公室群被设计成了树形(n 个点 n − 1 条边的无向连通图), ...
- HDU4514 湫湫系列故事——设计风景线 ——树的直径/树形dp+判环
中文题面,给出一个图,问能不能成环,如果可以就输出YES.否则输出该树的直径. 这里的判环我们用路径压缩的并查集就能很快的判断出来,可以在输入的同时进行判断.这题重点就是求树的直径. 树直径的性质可以 ...
- hdu 4679 树的直径
/* 题目大意:给n个点n-1条边的树,求删除哪条边时两个树中最大的直径与边权的乘积最小. 树的直径(Diameter)是指树上的最长简单路. 直径的求法:两遍BFS (or DFS) 若删除的边不是 ...
- POJ 1849 Two(树的直径--树形DP)(好题)
大致题意:在某个点派出两个点去遍历全部的边,花费为边的权值,求最少的花费 思路:这题关键好在这个模型和最长路模型之间的转换.能够转换得到,全部边遍历了两遍的总花费减去最长路的花费就是本题的答案,要思考 ...
- [HDU 5293]Tree chain problem(树形dp+树链剖分)
[HDU 5293]Tree chain problem(树形dp+树链剖分) 题面 在一棵树中,给出若干条链和链的权值,求选取不相交的链使得权值和最大. 分析 考虑树形dp,dp[x]表示以x为子树 ...
- hdu 4514 并查集+树形dp
湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
随机推荐
- linux上scp远程复制的使用
一.实例展示 两台机器IP分别为:A.104.238.161.75,B.43.224.34.73. ------把别的服务器上的资源拿到自己的上边 在A服务器上操作,将B服务器上/home/lk/目录 ...
- Full List of NMD R1 Singapore 2017
The initial adidas NMD Singapore, which sparked a sneaker craze larger than the Three Stripes could' ...
- python阳历转阴历,阴历转阳历
#!/usr/bin/env python # coding:utf8 # author:Z time:2019/1/16 import sxtwl # 日历中文索引 ymc = [u"十一 ...
- 0728am thinkphp介绍
- Drools 规则引擎环境搭建
一.关于 drools 规则引擎 前面写过一篇 Drools 规则引擎相关的文章,这篇文章主要记录一下规则引擎的环境搭建和简单示例.不熟悉 drools 的朋友可以看看这篇文章: 自己写个 Drool ...
- ruby中的作用域
作用域(scope)指的是变量的可达性或可见性.不同类型的变量有不同的作用域规则.与self类似,作用域在程序的执行过程中也在不断的变化,也可以根据上下文推断出"谁在什么作用域中" ...
- linux环境上运行.net core 初探
1.安装 .net core 环境 rpm --import https://packages.microsoft.com/keys/microsoft.ascsh -c 'echo -e " ...
- 【android】来电悬浮窗
先看下效果图 说下思路: 1:监听来电广播 2:根据来电号码,和本地数据库做匹配,有记录的,则提取出头像.名字.职位,生成悬浮窗 3:监听来电广播,如果当前行为是空闲的(没有任何通话行为),则删除掉悬 ...
- Android系统机制
Android系统机制 本文主要介绍Android系统整体运行机制 Linux中的一些概念 uboot加载系统内核到内存,系统内核运行起来的后,会创建第一个用户进程叫init进程,该进程是所有用户进程 ...
- 使用自签名SSL证书配置HTTPS,解决浏览器提示不安全警告
项目测试过程中需要将应用从HTTP升级到HTTPS,浏览了网上一些帖子,参考<WebLogic11g-单双向SSL配置(以Springside3为例)>一文使用openssl工具来自建CA ...