Total Highway Distance(贪心)
来源:互联网 发布:百年孤独中的女性 知乎 编辑:程序博客网 时间:2024/05/21 06:46
题目1 : Total Highway Distance
- 样例输入
3 51 2 22 3 3QUERYEDIT 1 2 4QUERYEDIT 2 3 2QUERY
- 样例输出
101412
描述
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.
普通暴力肯定超时,我们有一种特殊的技巧。
对于树中任意一条边e,一定满足这样一个情况:
a
假设A集合有x个节点,则B集合有N-x个节点。
而任何两个节点i,j,若i属于A,j属于B,则其连接路径中一定包含有边e。
i,j的组合有x*(N-x)
种,因此可以确定一共有x*(N-x)
条路径中包含有边e。换句话说,在所有点对的距离和之中,边e一共计算了x*(N-x)
次。
下面就是维护的问题,对于一条边的修改,我们定义delta=key-path[a](对于一组输入a,b,key。key为修改后这条边的权值,我们判读a是不是b的儿子,如果不是交换a,b。)最后的结果的变化量就为delta*(path[a])*(n-path[a])。随后path[a]=key(修改路径信息)。要注意用long long。
具体的操作看代码
#include<iostream>#include<cstdio>#include<string.h>#include<string>#include<algorithm>#include<vector>using namespace std;int path[100005];//每个节点到他父节点的长度int level[100005];int node[100005];int n,m;long long tot;vector<int>to[100005];vector<int>val[100005];void dfs(int rt){node[rt]=1;for(int i=0;i<to[rt].size();i++){if(level[to[rt][i]]==0){level[to[rt][i]]=level[rt]+1;path[to[rt][i]]=val[rt][i];dfs(to[rt][i]);node[rt]=node[rt]+node[to[rt][i]];tot+=(long long)((long long)node[to[rt][i]])*(path[to[rt][i]])*(n-node[to[rt][i]]);}}}int main(){while(~scanf("%d%d",&n,&m)){tot=0;for(int i=1;i<=n;i++){to[i].clear();val[i].clear();}memset(level,0,sizeof(level));memset(node,0,sizeof(node));int u,v,k;for(int i=0;i<n-1;i++){scanf("%d%d%d",&u,&v,&k);to[u].push_back(v);val[u].push_back(k);to[v].push_back(u);val[v].push_back(k);}level[1]=1;dfs(1);char ask[6];int a,b,c;for(int i=0;i<m;i++){scanf("%s",ask);if(ask[0]=='Q')printf("%lld\n",tot);else {scanf("%d%d%d",&a,&b,&c);int temp;if(level[a]<level[b]){temp=a;a=b;b=temp;}long long delta=c-path[a];tot+=(long long) (delta*node[a]*(n-node[a]));path[a]=c;}}}}
- Total Highway Distance(贪心)
- hiho Total Highway Distance
- hihocoder 1238 Total Highway Distance
- hihoCoder 1238 Total Highway Distance
- HiHo 112 Total Highway Distance
- [微软]题目2 : Total Highway Distance
- hihocoder1238 Total Highway Distance(DFS+二分查找/map)
- [hihocoder 1238] Total Highway Distance(微软2016校园招聘9月在线笔试)
- hiho一下 第112周 Total Highway Distance (树的所有路径)
- hihocoder # Total Hamming Distance
- 477. Total Hamming Distance
- 477. Total Hamming Distance
- 477. Total Hamming Distance
- 477. Total Hamming Distance**
- LeetCode: Total Hamming Distance
- LeetCode Total Hamming Distance
- Total Hamming Distance
- 477. Total Hamming Distance
- 项目开发步骤
- 移植Qt之更改分辨率问题
- 【Android进阶学习】监听EditText的变化
- tp 中的I()方法
- 如何用C语言实现转转相除法求最大公约数和最小公倍数
- Total Highway Distance(贪心)
- Linux操作系统——Windows 10 下的 Linux(Ubuntu)
- Hadoop集群集成kerberos
- 正则表达式
- 各种分布式文件系统简介
- mongodb的启动与关闭
- Java集合中那些类是线程安全的
- H - Counterfeit Dollar
- sublime记录