hihoCoder #1238 Total Highway Distance

Description

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?

Input

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.

Output

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

Sample Input

3 5
1 2 2
2 3 3
QUERY
EDIT 1 2 4
QUERY
EDIT 2 3 2
QUERY

Sample Output

10
14
12

Solution:

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <vector>
 4 #include <cstring>
 5 using namespace std;
 6
 7 #define MAXN 100010
 8
 9
10 vector<int> edges[MAXN];
11 vector<long long> d[MAXN];
12 int child[MAXN];
13 int fa[MAXN];
14 long long thd = 0;
15
16 int N, M;
17
18
19 void addEdge(int u, int v, int w) {
20     edges[u].push_back(v);
21     d[u].push_back(w);
22 }
23
24 void addDEdge(int u, int v, int w) {
25     addEdge(u, v, w);
26     addEdge(v, u, w);
27 }
28
29 void dfs(int u, vector<int> &vis) {
30     vis[u] = 1;
31     child[u] = 0;
32     for (int i = 0; i < edges[u].size(); ++i) {
33         int v = edges[u][i];
34         if (!vis[v]) {
35             fa[v] = u;
36             dfs(v, vis);
37             child[u] += child[v] + 1;
38             thd += d[u][i] * (child[v] + 1)*(N - 1 - child[v]);
39         }
40     }
41 }
42
43 int update(int u, int v, int k) {
44     int old = 0;
45     for (int i = 0; i < edges[u].size(); ++i) {
46         if (edges[u][i] == v) {
47             old = d[u][i];
48             d[u][i] = k;
49         }
50     }
51
52     for (int i = 0; i < edges[v].size(); ++i) {
53         if (edges[v][i] == u) {
54             d[v][i] = k;
55         }
56     }
57
58     return old;
59 }
60
61 int main() {
62
63     scanf("%d%d", &N, &M);
64     for (int i = 0; i < N - 1; ++i) {
65         int u, v, w;
66         scanf("%d%d%d", &u, &v, &w);
67         addDEdge(u, v, w);
68     }
69     vector<int> vis(N+1, 0);
70     fa[1] = 0;
71     dfs(1, vis);
72
73     for (int k = 1; k <= M; ++k) {
74         char op[10];
75         scanf("%s", op);
76         if (strcmp(op, "EDIT") == 0) {
77             int u, v, k;
78             scanf("%d%d%d", &u, &v, &k);
79             int old = update(u, v, k);
80             if (fa[v] == u) {
81                 thd += (long long)(k - old) * (child[v] + 1) * (N - 1 - child[v]);
82             }
83             else {
84                 thd += (long long)(k - old) * (child[u] + 1) * (N - 1 - child[u]);
85             }
86         }
87         else {
88             printf("%lld\n", thd);
89         }
90     }
91 }
时间: 2024-09-30 04:44:37

hihoCoder #1238 Total Highway Distance的相关文章

hihoCoder 1238 : Total Highway Distance(dfs + 二分)

题目连接 题意:给出n个城市,n-1条道路,求出两两路径的和. 思路:题意等价于求每天道路的使用次数,如下图所示 红色路径的使用度为以节点2为根节点的子树的节点数x * (n-x),此处为2 * 2 = 4.先按u<v的规则保存好道路,然后dfs一遍处理处每天道路的使用度,dfs过程中需要知道当前的边是哪条道路,此过程用二分查找,这中双变量的二分之前也没怎么写过. code: #include <iostream> #include <cstring> #include &l

hihocoder1238 Total Highway Distance(树形dp)

题意: 给定一颗有N个节点的带权树,之后进行M次操作: Q操作:询问树上所有点对之间的距离之和 E操作:修改树上某一条边的权值 思路: 树形dp求出每条边被利用的次数并统计 然后修改的时候就把这个边权修改并将改变值乘上利用次数更改ans /* *********************************************** Author :devil ************************************************ */ #include <cstd

[LeetCode] 477. Total Hamming Distance(位操作)

传送门 Description The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Now your job is to find the total Hamming distance between all pairs of the given numbers. Example: Input: 4, 14, 2 Ou

477. Total Hamming Distance 总的汉明距离

The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Now your job is to find the total Hamming distance between all pairs of the given numbers. Example: Input: 4, 14, 2 Output: 6 Explanat

477. Total Hamming Distance

问题描述: The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Now your job is to find the total Hamming distance between all pairs of the given numbers. Example: Input: 4, 14, 2 Output: 6 Ex

[Swift]LeetCode477. 汉明距离总和 | Total Hamming Distance

The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Now your job is to find the total Hamming distance between all pairs of the given numbers. Example: Input: 4, 14, 2 Output: 6 Explanat

477. Total Hamming Distance - Medium

The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Now your job is to find the total Hamming distance between all pairs of the given numbers. Example: Input: 4, 14, 2 Output: 6 Explanat

LeetCode 477: Total Hamming Distance

Note: 1. Very smart way of calculating how many difference from bits: https://en.wikipedia.org/wiki/Hamming_distance#Algorithm_example 2. Another way is counting how many 1s and 0s per bits. Then the contribution of that bit will bt C(1, k) * C(1, n

Total Hamming Distance

1 class Solution(object): 2 def totalHammingDistance(self, nums): 3 """ 4 :type nums: List[int] 5 :rtype: int 6 """ 7 bin_num = ["{0:b}".format(num) for num in nums] 8 zip_list = [] 9 for i in range(len(bin_num)): 1