hihocoder1238 Total Highway Distance(树形dp)

题意:

给定一颗有N个节点的带权树,之后进行M次操作:

Q操作:询问树上所有点对之间的距离之和

E操作:修改树上某一条边的权值

思路:

树形dp求出每条边被利用的次数并统计

然后修改的时候就把这个边权修改并将改变值乘上利用次数更改ans

/* ***********************************************
Author        :devil
************************************************ */
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <stdlib.h>
#define inf 0x3f3f3f3f
#define LL long long
#define rep(a,b) for(int i=a;i<b;i++)
#define dec(a,b) for(int i=a;i>=b;i--)
#define ou(a) printf("%d\n",a)
#define pb push_back
#define mkp make_pair
template<class T>inline void rd(T &x){char c=getchar();x=0;while(!isdigit(c))c=getchar();while(isdigit(c)){x=x*10+c-‘0‘;c=getchar();}}
#define IN freopen("in.txt","r",stdin);
#define OUT freopen("out.txt","w",stdout);
using namespace std;
const int mod=1e9+7;
const int N=1e5+10;
int n,m,x,y,w,sum[N],val[N],level[N];
LL ans;
char s[10];
vector<pair<int,int> >eg[N];
void dfs(int u,int lev,int pre)
{
    sum[u]=1;
    level[u]=lev;
    rep(0,eg[u].size())
    {
        int v=eg[u][i].first;
        if(v==pre) continue;
        val[v]=eg[u][i].second;
        dfs(v,lev+1,u);
        sum[u]+=sum[v];
        ans+=(LL)val[v]*sum[v]*(n-sum[v]);
    }
}
int main()
{
    //IN
    rd(n);rd(m);
    rep(1,n)
    {
        rd(x),rd(y),rd(w);
        eg[x].pb(mkp(y,w));
        eg[y].pb(mkp(x,w));
    }
    dfs(1,1,1);
    while(m--)
    {
        scanf("%s",s);
        if(s[0]==‘Q‘) printf("%lld\n",ans);
        else
        {
            rd(x),rd(y),rd(w);
            if(level[x]<level[y]) swap(x,y);
            ans+=(LL)(w-val[x])*sum[x]*(n-sum[x]);
            val[x]=w;
        }
    }
    return 0;
}

时间: 2024-11-05 11:04:34

hihocoder1238 Total Highway Distance(树形dp)的相关文章

hihoCoder #1238 Total Highway Distance

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

HDU 2376 Average distance (树形dp)

Average distance Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 588    Accepted Submission(s): 213 Special Judge Problem Description Given a tree, calculate the average distance between two ve

HDU2376Average distance(树形dp|树上任意两点距离和的平均值)

思路: 引:如果暴力枚举两点再求距离是显然会超时的.转换一下思路,我们可以对每条边,求所有可能的路径经过此边的次数:设这条边两端的点数分别为A和B,那 么这条边被经过的次数就是A*B,它对总的距离和的贡献就是(A*B*此边长度).我们把所有边的贡献求总和,再除以总路径数N*(N-1)/2,即为最 后所求. 每条边两端的点数的计算,实际上是可以用一次dfs解决的.任取一点为根,在dfs的过程中,对每个点k记录其子树包含的点数(包括其自身),设点数为a[k],则k的父亲一侧的点数即为N-a[k].这

hihoCoder 1238 : Total Highway Distance(dfs + 二分)

题目连接 题意:给出n个城市,n-1条道路,求出两两路径的和. 思路:题意等价于求每天道路的使用次数,如下图所示 红色路径的使用度为以节点2为根节点的子树的节点数x * (n-x),此处为2 * 2 = 4.先按u<v的规则保存好道路,然后dfs一遍处理处每天道路的使用度,dfs过程中需要知道当前的边是哪条道路,此过程用二分查找,这中双变量的二分之前也没怎么写过. code: #include <iostream> #include <cstring> #include &l

codeforces161D - Distance in Tree 树形dp

题意:给你一棵树,问你树中距离为k的有多少种情况. 解题思路:树形dp  维护每个节点(1-K)深度的情况, 解题代码: 1 // File Name: 161d.cpp 2 // Author: darkdream 3 // Created Time: 2014年08月03日 星期日 19时20分10秒 4 5 #include<vector> 6 #include<list> 7 #include<map> 8 #include<set> 9 #incl

codeforces 161D - Distance in Tree(树形dp)

题目大意: 求出树上距离为k的点对有多少个. 思路分析: dp[i][j] 表示 i 的子树中和 i 的距离为 j 的点数有多少个.注意dp[i] [0] 永远是1的. 然后在处理完一颗子树后,就把自身的dp 更新. 更新之前更新答案. 如果这颗子树到 i 有 x 个距离为j的.那么答案就要加上 dp[i] [ k-j-1] * x; #include <iostream> #include <cstdio> #include <cstring> #include &l

codeforces 161 D. Distance in Tree(树形dp)

题目链接:http://codeforces.com/problemset/problem/161/D 题意:给出一个树,问树上点到点的距离为k的一共有几个. 一道简单的树形dp,算是一个基础题. 设dp[i][len]表示i为根距离为len的一共有几个点. 一般的树形dp都是先dfs然后再更新dp的值,注意按这样写就行了.而且一般的树形dp都是设dp[i][k]i为根,k为条件. void dfs(int u , int pre) { int len = vc[u].size(); dp[u]

hdu4118 树形dp

http://acm.hdu.edu.cn/showproblem.php?pid=4118 Problem Description Nowadays, people have many ways to save money on accommodation when they are on vacation. One of these ways is exchanging houses with other people. Here is a group of N people who wan

【poj2152】【Fire】【树形dp】

Fire Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1161 Accepted: 595 Description Country Z has N cities, which are numbered from 1 to N. Cities are connected by highways, and there is exact one path between two different cities. Recentl