HDU6613 Squrirrel (树形dp)

本文介绍了一种使用树形动态规划方法解决特定问题的算法:在给定的无根树中寻找一个根节点,通过将一条边权变为0来最小化离树根最远点的距离。文章详细解释了算法思路,包括从子树和父节点方向获取信息的过程,以及如何通过状态转移方程实现。最后,提供了一份完整的C++代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 %d\n",idx,ans);
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值