hihocoder 1238 Total Highway Distance

6 篇文章 0 订阅

时间限制: 10000ms
单点时限: 1000ms
内存限制: 256MB

描述

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.

样例输入
3 5
1 2 2
2 3 3
QUERY
EDIT 1 2 4
QUERY
EDIT 2 3 2
QUERY
样例输出
10
14
12
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
const int mx=100000+100;
int n,m,far[mx];
ll ans=0,cnt[mx];
map<int,int> mp[mx];
struct edge
{
    int to,cost;
};
vector<edge> g[mx];
void init()
{
    for(int i=0;i<=n;i++)
    {
        g[i].clear();
        mp[i].clear();
    }
    memset(cnt,0,sizeof(cnt));
    ans=0;
}
ll dfs(int u,int fa)
{
    far[u]=fa;
    vector<int> res;
    vector<ll> res0;
    ll sum=0;
    for(int i=0;i<g[u].size();i++)
    {
        int v=g[u][i].to;
        if(v==fa) continue;
        res0.push_back(dfs(v,u)+cnt[v]*g[u][i].cost);
        res.push_back(cnt[v]);
    }
    for(int i=0;i<res.size();i++)
    {
        cnt[u]+=res[i];
        sum+=res0[i];
    }
    cnt[u]++;
    for(int i=0;i<res.size();i++)
    {
        ll sum0=sum-res0[i],s=cnt[u]-res[i];
        ans+=sum0*res[i]+s*res0[i];
    }
    ans+=sum;
    return sum;
}
int main()
{
    while(cin>>n>>m)
    {
        init();
        for(int i=1;i<n;i++)
        {
            int u,v,k;
            scanf("%d%d%d",&u,&v,&k);
            g[u].push_back(edge{v,k});
            g[v].push_back(edge{u,k});
            mp[u][v]=k;mp[v][u]=k;
        }
        dfs(1,-1);
        ans>>=1;
        char s[1000];
        for(int i=0;i<m;i++)
        {
            scanf("%s",s);
            if(s[0]=='Q')
                printf("%lld\n",ans);
            else
            {
                int u,v,k;
                scanf("%d%d%d",&u,&v,&k);
                int dt=k-mp[u][v];
                mp[u][v]=mp[v][u]=k;
                if(far[v]==u) swap(u,v);
                ans+=cnt[u]*dt*(n-cnt[u]);
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值