hihoCoder #1238 Total Highway Distance
2015-10-12 12:19
239 查看
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:
#include <cstdio> #include <cstdlib> #include <vector> #include <cstring> using namespace std; #define MAXN 100010 vector<int> edges[MAXN]; vector<long long> d[MAXN]; int child[MAXN]; int fa[MAXN]; long long thd = 0; int N, M; void addEdge(int u, int v, int w) { edges[u].push_back(v); d[u].push_back(w); } void addDEdge(int u, int v, int w) { addEdge(u, v, w); addEdge(v, u, w); } void dfs(int u, vector<int> &vis) { vis[u] = 1; child[u] = 0; for (int i = 0; i < edges[u].size(); ++i) { int v = edges[u][i]; if (!vis[v]) { fa[v] = u; dfs(v, vis); child[u] += child[v] + 1; thd += d[u][i] * (child[v] + 1)*(N - 1 - child[v]); } } } int update(int u, int v, int k) { int old = 0; for (int i = 0; i < edges[u].size(); ++i) { if (edges[u][i] == v) { old = d[u][i]; d[u][i] = k; } } for (int i = 0; i < edges[v].size(); ++i) { if (edges[v][i] == u) { d[v][i] = k; } } return old; } int main() { scanf("%d%d", &N, &M); for (int i = 0; i < N - 1; ++i) { int u, v, w; scanf("%d%d%d", &u, &v, &w); addDEdge(u, v, w); } vector<int> vis(N+1, 0); fa[1] = 0; dfs(1, vis); for (int k = 1; k <= M; ++k) { char op[10]; scanf("%s", op); if (strcmp(op, "EDIT") == 0) { int u, v, k; scanf("%d%d%d", &u, &v, &k); int old = update(u, v, k); if (fa[v] == u) { thd += (long long)(k - old) * (child[v] + 1) * (N - 1 - child[v]); } else { thd += (long long)(k - old) * (child[u] + 1) * (N - 1 - child[u]); } } else { printf("%lld\n", thd); } } }
相关文章推荐
- Android LayoutInflater原理分析,带你一步步深入了解View(一)
- android社交第三方应用分享
- C++STL之Set集合容器
- PHPCMS V9自定义分页函数
- 重写TEdit文本编辑框
- 使用火狐的restclient发送http接口post及get请求
- linux的lsof命令
- Linux 系统挂载数据盘
- Docker 学习笔记(三)--Docker镜像与仓库
- 数据库基础
- 导出excel 利用xml文件
- 指针的指针
- 两个字符串是变位词
- poj_1236 强连通分支
- BFS && DFS
- iOS 处理服务器返回的浮点数数据
- Poj.1775 Sum of Factorials【略坑】 2015/10/12
- iOS9适配必看指南
- iOS-Git使用详解
- 正则后台验证、抓包