题目链接:http://hihocoder.com/contest/mstest2015sept2/problem/2
题目:
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
分析:
举个栗子:
题目中的1--2--3,其中以1为根,计算它的子节点有2个,剩余的结点是3 - 2 = 1,说明1和它直接的子节点2的道路1--2这条路一边的城市有2个,一边的城市有1个,它们在THD中占2 * 1 * 2(cost) = 4比重。
再计算1的子树2为根的情况,它的子节点有1个,剩余的结点是3 - 1 = 2个,说明2和它直接的子节点3的道路2--3这一条路一边的城市有1个,一边的城市有2个,它们在THD中占1 * 2 * 3(cost) = 6比重。
所以结果是4 + 6 为10。
当EDIT 1 2 4后,原来的1--2这条路的2 * 1 * 2要变为 2 * 1 * 4,所以大小变化是2 * 1 * (4 - 2)为4,所以10 + 4 = 14。
当EDIT2 3 2 后,原来2--3这条路的1 * 2 * 3变成了1 * 2 * 2,大小变化是1 * 2 * (2 - 3)为-1,所以14 - 2 = 12。
扩展一下:
对于如下的图
对于城市1来说,它的子树包括2这棵子树和其他子树,其中2这棵子书(包括2)有(2 + c2 + c3)个城市,那么对于1--2这条路来说,道路两旁的城市数为(2 + c2 + c3)和(n - 2 - c2 - c3)个(或者是1 + c1个也是一样的),对于THD的定义来说,它们占(2 + c2 + c3) * (n - 2 - c2 - c3) * c大小。
代码(未提交测试过):
#include<stdio.h> #include<iostream> #include<vector> #include<algorithm> #include<string> #include<math.h> #include<climits> #include<map> #include<set> using namespace std; int DFS(int root, int n,int** cost, int** count){ int sum = 0; for (int i = 1; i <= n; ++i){ if (cost[root][i] != 0){ cost[i][root] = 0;//路只能找一遍,有方向的,找了这个方向就关闭另一个方向 count[root][i] ++; count[root][i] += DFS(i, n, cost, count); } sum += count[root][i]; } return sum; } int main(){ //freopen("F://Temp/input.txt", "r", stdin); int n, m; cin >> n >> m; int **cost = new int*[n + 1];//注意这里二维数组的new的方法 for (int i = 0; i <= n; ++i){ cost[i] = new int[n + 1]; } int **count = new int*[n + 1]; for (int i = 0; i <= n; ++i){ count[i] = new int[n + 1]; } for (int i = 0; i <= n; ++i){ for (int j = 0; j <= n; ++j){ cost[i][j] = count[i][j] = 0; } } for (int i = 0; i < n - 1; ++i){ int s, t, c; cin >> s >> t >> c; cost[s][t] = cost[t][s] = c;//为了让1能遍历所有的路 } DFS(1,n ,cost, count);//point1 int sum = 0; for (int i = 1; i <= n; ++i){ for (int j = 1; j <= n; ++j){ sum += cost[i][j] * count[i][j] * (n - count[i][j]);//point2 } } for (int i = 0; i < m; ++i){ string kind; cin >> kind; if (kind == "QUERY") cout << sum << endl; else if (kind == "EDIT"){ int s, t, c; cin >> s >> t >> c; sum += (c - cost[s][t]) * count[s][t] * (n - count[s][t]);//point3 } } return 0; }其中,只对形成的树DFS进行了一遍遍历,复杂度为O(n * n),把所有的路会走的遍数存在了count[][]数组后,而后m次的查询都是O(1)时间能够解决的。
——Apie陈小旭