您的位置:首页 > 其它

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);
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: