概述
https://cn.vjudge.net/contest/312854#problem/G
Master magician `eom` is searching for his naughty squirrel. The squirrel hides at a node of a tree, while its owner `eom` has no idea which node it hides at. As a magician, `eom` can choose a node and release many magical creatures to search his squirrel in all directions at the speed of 1. Besides, he can choose a pair of adjacent nodes on the tree and reduce the distance between them to 0 before releasing magical creatures. Please help him to find the start node to release magical creatures, so that `eom` can find his squirrel in the shortest time even in the worst condition. Note that `eom` is full of wisdom, so that he can optimally make decision to reduce the distance after the start node is determined.
Input
The first line contains a integer T(1≤T≤20)(1≤T≤20), indicating there are T test cases followed.
For each test case, the first line contains one integers nn: the number of nodes of the tree(1≤n≤200,000)(1≤n≤200,000).
In the following n−1n−1 lines, each line has three integers uu,vv and ww, indicating there exists a line between node uu and vv, which length equals to ww(1≤u,v≤n,1≤w≤200)(1≤u,v≤n,1≤w≤200).
It is guaranteed that (1≤∑n≤2,000,000)(1≤∑n≤2,000,000).
(1≤T≤20)(1≤T≤20)
(1≤n≤200,000)(1≤n≤200,000)
(1≤u,v≤n,1≤w≤200)(1≤u,v≤n,1≤w≤200)
(1≤∑n≤2000000)(1≤∑n≤2000000)
Output
Output two integers. The first one is the index of the start node you should choose and the second one is the least time required in the worst condition. If there are multiple possible ways to choose the start node to minimize the time, the index of start node chosen should be minimum.
Sample Input
1 5 1 5 1 1 2 1 2 3 2 3 4 1
Sample Output
1 2
题意:给你一棵无根树,要你寻找一个根节点使得在将一条边权变为0后,离树根最远的点到根节点的距离最小。
思路:和computer那道题差不多,考虑从以u为根节点的子树中获取信息和从u的父亲那边获取信息
但是这道题要删边,那么在开一维数组表示删没删
fi[u][0]表示在u这个结点不删边沿着子树方向能到达的最远距离,se[u][0]为第二远,th[u][0]为第三远,fa[u][0]表示沿着父亲方向能到达的最远距离,第二维为1表示删一条边能到达的距离。
状态转移自己想或者看代码应该就明白了,毕竟这题思路不是很难,就是写起来很繁琐
大佬的博客https://www.cnblogs.com/Dillonh/p/11269111.html
#include<bits/stdc++.h>
#pragma GCC optimize(3)
//#define max(a,b) a>b?a:b
using namespace std;
typedef long long ll;
const int N=2e5+5;
inline int read()
{
int X=0,w=0; char ch=0;
while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
return w?-X:X;
}
inline int max(int a,int b){
return a>b?a:b;
}
inline int min(int a,int b){
return a<b?a:b;
}
struct Edge{
int v,w,next;
}e[N<<1];
int head[N],tot=0;
void init(int n){
tot=0;
for(int i=1;i<=n;i++) head[i]=-1;
}
void add(int u,int v,int w){
e[tot].v=v;
e[tot].w=w;
e[tot].next=head[u];
head[u]=tot++;
}
int fi[N][2],se[N][2],th[N][2],fa[N][2],id[N][3];
void dfs1(int u,int pre){ //处理以u为根节点,从子树中获得的信息
fi[u][0]=se[u][0]=th[u][0]=fi[u][1]=se[u][1]=0;
int w1,w2;
for(int i=head[u];~i;i=e[i].next){
int &v=e[i].v;
int &w=e[i].w;
if(v==pre) continue;
dfs1(v,u);
if(fi[v][0]+w>fi[u][0]){
fi[u][0]=fi[v][0]+w;
id[u][0]=v;
w1=w;
}
}
for(int i=head[u];~i;i=e[i].next){
int &v=e[i].v;
int &w=e[i].w;
if(v==pre) continue;
if(v==id[u][0]) continue;
if(fi[v][0]+w>se[u][0]){
se[u][0]=fi[v][0]+w;
id[u][1]=v;
w2=w;
}
}
for(int i=head[u];~i;i=e[i].next){
int &v=e[i].v;
int &w=e[i].w;
if(v==pre) continue;
if(v==id[u][0]) continue;
if(v==id[u][1]) continue;
if(fi[v][0]+w>th[u][0]){
th[u][0]=fi[v][0]+w;
id[u][2]=v;
}
}
int &v1=id[u][0];
int &v2=id[u][1];
fi[u][1]=min(fi[v1][0],max(fi[v1][1],se[v1][0])+w1);
se[u][1]=min(fi[v2][0],max(fi[v2][1],se[v2][0])+w2);
//th[u][1]用不到
}
void dfs2(int u,int pre){//处理从父亲那边获得的信息
for(int i=head[u];~i;i=e[i].next){
int &v=e[i].v;
int &w=e[i].w;
if(v==pre) continue;
if(v==id[u][0]){
fa[v][0]=max(fa[u][0],se[u][0])+w;
int k1=max(fa[u][0],se[u][0]);//砍uv
int k2=max(fa[u][1],se[u][0])+w;
int k3=max(se[u][1],max(fa[u][0],th[u][0]))+w;
fa[v][1]=min(k1,min(k2,k3));
}
else if(v==id[u][1]){
fa[v][0]=max(fa[u][0],fi[u][0])+w;
int k1=max(fa[u][0],fi[u][0]);
int k2=max(fa[u][1],fi[u][0])+w;
int k3=max(fi[u][1],max(fa[u][0],th[u][0]))+w;
fa[v][1]=min(k1,min(k2,k3));
}
else{
fa[v][0]=max(fa[u][0],fi[u][0])+w;
int k1=max(fa[u][0],fi[u][0]);
int k2=max(fa[u][1],fi[u][0])+w;
int k3=max(fi[u][1],max(fa[u][0],se[u][0]))+w;
fa[v][1]=min(k1,min(k2,k3));
}
dfs2(v,u);
}
}
int main(){
int T;
T=read();
while(T--){
int n;
n=read();
init(n);
for(int i=1;i<n;i++){
int u,v,w;
u=read();
v=read();
w=read();
add(u,v,w);
add(v,u,w);
}
dfs1(1,0);
fa[1][0]=fa[1][1]=0;
dfs2(1,0);
int ans=0x3f3f3f3f;
int idx=0;
for(int i=1;i<=n;i++){
int k1=max(fa[i][1],fi[i][0]);
int k2=max(fi[i][1],max(fa[i][0],se[i][0]));
if(ans>min(k1,k2)){
ans=min(k1,k2);
idx=i;
}
}
printf("%d %dn",idx,ans);
}
return 0;
}
最后
以上就是粗犷哈密瓜为你收集整理的HDU6613 Squrirrel (树形dp)的全部内容,希望文章能够帮你解决HDU6613 Squrirrel (树形dp)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复