博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hihoCoder #1238 Total Highway Distance
阅读量:4967 次
发布时间:2019-06-12

本文共 3438 字,大约阅读时间需要 11 分钟。

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 51 2 22 3 3QUERYEDIT 1 2 4QUERYEDIT 2 3 2QUERY

Sample Output

101412

Solution:

1 #include 
2 #include
3 #include
4 #include
5 using namespace std; 6 7 #define MAXN 100010 8 9 10 vector
edges[MAXN];11 vector
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
&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
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 }

 

转载于:https://www.cnblogs.com/liew/p/4871271.html

你可能感兴趣的文章
【ASP.NET】从服务器端注册客户端脚本
查看>>
Infix to Postfix Expression
查看>>
SELECT LOCK IN SHARE MODE and FOR UPDATE
查看>>
Perl/Nagios – Can’t locate utils.pm in @INC
查看>>
目录导航「深入浅出ASP.NET Core系列」
查看>>
简易爬虫(爬取本地数据)
查看>>
python 进程间通信
查看>>
深拷贝 vs 浅拷贝 释放多次
查看>>
Javascript 有用参考函数
查看>>
点群的判别(三)
查看>>
GNSS 使用DFT算法 能量损耗仿真
查看>>
【转】Simulink模型架构指导
查看>>
MYSQL数据库的导出的几种方法
查看>>
SQL Server-5种常见的约束
查看>>
硬件之美
查看>>
[转载]java开发中的23种设计模式
查看>>
表格的拖拽功能
查看>>
函数的形参和实参
查看>>
文字过长 用 ... 表示 CSS实现单行、多行文本溢出显示省略号
查看>>
1Caesar加密
查看>>