标签:
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.
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 }
hihoCoder #1238 Total Highway Distance
标签:
原文地址:http://www.cnblogs.com/liew/p/4871271.html