hihoCoder 1238 Total Highway Distance

#1238 : Total Highway Distance

时间限制: 10000ms
单点时限: 1000ms
内存限制: 256MB

描述

Little Hi and Little Ho are playing a construction simulation game. They build N cities (numbered from 1 to N) in the game and connect them by N-1 highways. It is guaranteed that each pair of cities are connected by the highways directly or indirectly.

The game has a very important value called Total Highway Distance (THD) which is the total distances of all pairs of cities. Suppose there are 3 cities and 2 highways. The highway between City 1 and City 2 is 200 miles and the highway between City 2 and City 3 is 300 miles. So the THD is 1000(200 + 500 + 300) miles because the distances between City 1 and City 2, City 1 and City 3, City 2 and City 3 are 200 miles, 500 miles and 300 miles respectively.

During the game Little Hi and Little Ho may change the length of some highways. They want to know the latest THD. Can you help them?

输入

Line 1: two integers N and M.

Line 2 .. N: three integers u, v, k indicating there is a highway of k miles between city u and city v.

Line N+1 .. N+M: each line describes an operation, either changing the length of a highway or querying the current THD. It is in one of the following format.

EDIT i j k, indicating change the length of the highway between city i and city j to k miles.

QUERY, for querying the THD.

For 30% of the data: 2<=N<=100, 1<=M<=20

For 60% of the data: 2<=N<=2000, 1<=M<=20

For 100% of the data: 2<=N<=100,000, 1<=M<=50,000, 1 <= u, v <= N, 0 <= k <= 1000.

输出

For each QUERY operation output one line containing the corresponding THD.

样例输入
3 5
1 2 2
2 3 3
QUERY
EDIT 1 2 4
QUERY
EDIT 2 3 2
QUERY
样例输出
10
14
12
解题思路:求树上任意两点的距离之和,转化为求树上任意一条边在所有经过这条边的路径中出现的次数*边权的和,求出现的次数=边的2个端点所在集合的点的数目相乘,为此先DFS求出树上每个点作为根节点时,子树的节点个数。
以DFS的时间戳为顺序,先搜索到的为父节点,后搜索到的为子节点,父节点的集合中的点的个数=n-子节点的集合中点的个数
用map<pair<int,int>,int>查找EDIT的边的编号,每次EDIT后要更新边权
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <set>
#include <map>
#include <list>
#include <queue>
#include <stack>
#include <deque>
#include <vector>
#include <bitset>
#include <cmath>
#include <utility>
#define Maxn 100005
#define Maxm 1000005
#define lowbit(x) x&(-x)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define PI acos(-1.0)
#define MP make_pair
#define LL long long 
#define Inf (1LL<<62)
#define inf 0x3f3f3f3f
#define re freopen("in.txt","r",stdin)
#define wr freopen("out.txt","w",stdout)
using namespace std;
int head[Maxn],k;
int p[Maxn];
struct Edge
{
	int u;
	int v;
	LL w;
	int next;
}edge[Maxm];
void add(int u,int v,LL w)
{
	edge[k].u=u;
	edge[k].v=v;
	edge[k].w=w;
	edge[k].next=head[u];
	head[u]=k++;
}
int dfs(int root,int pre)
{
	for(int i=head[root];i!=-1;i=edge[i].next)
	{
		int v=edge[i].v;
		if(v!=pre)
			p[root]+=dfs(v,root);
	}
	return p[root];
}
int main()
{
	int n,m,u,v,w;
	LL ans;
	char op[10];
	pair<int,int> mp;
	map<pair<int,int>,int> num;
	while(~scanf("%d%d",&n,&m))
	{
		num.clear();
		k=ans=0;
		for(int i=1;i<=n;i++)
			p[i]=1;
		memset(head,-1,sizeof(head));
		for(int i=1;i<n;i++)
		{
			scanf("%d%d%lld",&u,&v,&w);
			add(u,v,w);
			mp=MP(u,v);
			num[mp]=k;
			add(v,u,w);
			mp=MP(v,u);
			num[mp]=k;
		}
		dfs(1,-1);
		for(int i=0;i<k;i++)
		{
			if(p[edge[i].u]>p[edge[i].v])
				//以dfs的时间戳确定这条边上的父子关系,进而确定父集合和子集合中点的数目
			{
				LL a=n,b;
				b=p[edge[i].v];
				ans+=(a-b)*b*(edge[i].w);
			}
			else 
			{
				LL a=n,b;
				b=p[edge[i].u];
				ans+=(a-b)*b*(edge[i].w);
			}
		}
		ans/=2;
		for(int i=0;i<m;i++)
		{
			scanf("%s",op);
			if(op[0]=='Q')
				printf("%lld\n",ans);
			else
			{
				scanf("%d%d%d",&u,&v,&w);
				mp=MP(u,v);
				LL prew=edge[num[mp]].w;
				edge[num[mp]].w=w;//在EDIT后要更新边权
				if(p[u]>p[v])
				{
					LL a=n,b;
					b=p[v];
					ans+=(a-b)*b*(w-prew);
				}
				else 
				{
					LL a=n,b;
					b=p[u];
					ans+=(a-b)*b*(w-prew);
				}
			}
		}
	}
	return 0; 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值