hdu 5886 Tower Defence 2016ACM/ICPC青岛赛区网络赛1009

Problem Description
There was a civil war between two factions in Skyrim, a province of the Empire on the continent of Tamriel. The Stormcloaks, led by Ulfric Stormcloak, are made up of Skyrim's native Nord race. Their goal is an independent Skyrim free from Imperial interference. The Imperial Legion, led by General Tullius, is the military of the Empire that opposes the Stormcloaks and seeks to reunite and pacify the province.

The current target of General Tullius is to defend Whiterun City. Near by this city there are  N  towers under the Empire's control. There are  N1  roads link these tower, so solders can move from any tower to another one through these roads.

In military affairs, tactical depth means the longest path between two towers of all. Larger the tactical depth is, more stable these towers are.

According to the message sent by spies, General Tullius believe that Stormcloaks is planning to attack one of these roads, and his towers would be divided into two parts. However, Tullius does not know which one, so he supposes the possibility that Stormcloaks attack these roads are the same. Now, General Tullius ask for your help, to calculate the expectation of tactical depth after this attack.

To avoid the issue of precision, you need to calculate  expectationoftacticaldepth×(N1) .
 

Input
The first line of input contains an integer  t , the number of test cases.  t  test cases follow.
For each test case, in the first line there is an integer  N(N100000) .
The  i -th line of the next  N1  lines describes the  i -th edge. Three integers  u,v,w (0w1000)  describe an edge between  u  and  v  of length  w .
 

Output
For each test cases, output  expectationoftacticaldepth×(N1) .
 

Sample Input
  
  
2 3 2 1 2 3 2 5 5 2 1 7 3 1 7 4 2 5 5 2 6
 

Sample Output
  
  
7 63


给你一棵树,枚举删除每一条边,求剩下的直径之和

首先如果删除的边不在直径上,那贡献就是直径

如果删除的在直径上。我们假设一条直径的起点是L终点是R

那么我们分别以L和R为根处理每个子树的内部最大直径

然后统计一下就可以了

#include<map>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
struct line
{
	int s,t;
	int x;
	int next;
}a[200001];
int head[100001];
int edge;
inline void add(int s,int t,int x)
{
	a[edge].next=head[s];
	head[s]=edge;
	a[edge].s=s;
	a[edge].t=t;
	a[edge].x=x;
}
int fa[100001];
long long son1[100001],son2[100001];
int sonx1[100001],sonx2[100001];
int dep[100001][3];
inline void dfs1(int d)
{
	son1[d]=0;
	sonx1[d]=0;
	son2[d]=0;
	sonx2[d]=0;
	int i;
	for(i=head[d];i!=0;i=a[i].next)
	{
		int t=a[i].t;
		if(t!=fa[d])
		{
			fa[t]=d;
			dfs1(t);
			if(son1[t]+a[i].x>=son1[d])
			{
				son2[d]=son1[d];
				sonx2[d]=sonx1[d];
				son1[d]=son1[t]+a[i].x;
				sonx1[d]=t;
			}
			else if(son1[t]+a[i].x>=son2[d])
			{
				son2[d]=son1[t]+a[i].x;
				sonx2[d]=t;
			}
		}
	}
}
bool v[100001];
bool vx[100001];
inline int findline(int d,int x)
{
	vx[d]=true;
	if(sonx1[d]==0)
		return d;
	dep[sonx1[d]][x]=dep[d][x]+1;
	return findline(sonx1[d],x);
}
long long ans[3][100001];
inline void trdp(int d,int x)
{
	son1[d]=0;
	sonx1[d]=0;
	son2[d]=0;
	sonx2[d]=0;
	v[d]=true;
	int i;
	long long maxx=0;
	for(i=head[d];i!=0;i=a[i].next)
	{
		int t=a[i].t;
		if(!v[t])
		{
			trdp(t,x);
			maxx=max(maxx,ans[x][t]);
			if(son1[t]+a[i].x>=son1[d])
			{
				son2[d]=son1[d];
				sonx2[d]=sonx1[d];
				son1[d]=son1[t]+a[i].x;
				sonx1[d]=t;
			}
			else if(son1[t]+a[i].x>=son2[d])
			{
				son2[d]=son1[t]+a[i].x;
				sonx2[d]=t;
			}
		}
	}
	ans[x][d]=max(son1[d]+son2[d],maxx);
}
int main()
{
//    freopen("1009.in", "r", stdin);
//    freopen("1009.out", "w", stdout);
	int T;
	scanf("%d",&T);
	while(T>0)
	{
		T--;
		edge=0;
		memset(head,0,sizeof(head)); 
		int n;
		scanf("%d",&n);
		int i;
		int s,t,x;
		for(i=1;i<=n-1;i++)
		{
			scanf("%d%d%d",&s,&t,&x);
			edge++;
			add(s,t,x);
			edge++;
			add(t,s,x);
		}
		memset(fa,0,sizeof(fa));
		dfs1(1);
		long long maxx=0,maxi=0;
		for(i=1;i<=n;i++)
		{
			if(son1[i]+son2[i]>maxx)
			{
				maxx=son1[i]+son2[i];
				maxi=i;
			}
		}
		int l,r;
		memset(dep,0,sizeof(dep));
		memset(vx,false,sizeof(vx));
		dep[sonx1[maxi]][1]=1;
		dep[sonx2[maxi]][2]=1;
		vx[maxi]=true;
		l=findline(sonx1[maxi],1);
		r=findline(sonx2[maxi],2);
		if(r==0)
			r=maxi;
		memset(ans,0,sizeof(ans));
		memset(v,false,sizeof(v));
		trdp(l,1);
		memset(v,false,sizeof(v));
		trdp(r,2);
		long long ansx=0;
		for(i=1;i<=edge;i+=2)
		{
			s=a[i].s;
			t=a[i].t;
			if(vx[s]&&vx[t])
			{
				if(dep[s][1]<dep[t][1])
					ansx+=max(ans[1][s],ans[2][t]);
				else if(dep[s][1]>dep[t][1])
					ansx+=max(ans[2][s],ans[1][t]);
				else if(dep[s][2]<dep[t][2])
					ansx+=max(ans[2][s],ans[1][t]);
				else if(dep[s][2]>dep[t][2])
					ansx+=max(ans[1][s],ans[2][t]);
			}
			else
				ansx+=maxx;
		}
		printf("%I64d\n",ansx);
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值