New Year Santa Network - CodeForces 500 D 树形dp

New Year Santa Network
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

New Year is coming in Tree World! In this world, as the name implies, there are n cities connected by n - 1 roads, and for any two distinct cities there always exists a path between them. The cities are numbered by integers from 1 to n, and the roads are numbered by integers from 1 to n - 1. Let's define d(u, v) as total length of roads on the path between city u and city v.

As an annual event, people in Tree World repairs exactly one road per year. As a result, the length of one road decreases. It is already known that in the i-th year, the length of the ri-th road is going to become wi, which is shorter than its length before. Assume that the current year is year 1.

Three Santas are planning to give presents annually to all the children in Tree World. In order to do that, they need some preparation, so they are going to choose three distinct cities c1c2c3 and make exactly one warehouse in each city. The k-th (1 ≤ k ≤ 3) Santa will take charge of the warehouse in city ck.

It is really boring for the three Santas to keep a warehouse alone. So, they decided to build an only-for-Santa network! The cost needed to build this network equals to d(c1, c2) + d(c2, c3) + d(c3, c1) dollars. Santas are too busy to find the best place, so they decided to choose c1, c2, c3 randomly uniformly over all triples of distinct numbers from 1 to n. Santas would like to know the expected value of the cost needed to build the network.

However, as mentioned, each year, the length of exactly one road decreases. So, the Santas want to calculate the expected after each length change. Help them to calculate the value.

Input

The first line contains an integer n (3 ≤ n ≤ 105) — the number of cities in Tree World.

Next n - 1 lines describe the roads. The i-th line of them (1 ≤ i ≤ n - 1) contains three space-separated integers aibili (1 ≤ ai, bi ≤ nai ≠ bi1 ≤ li ≤ 103), denoting that the i-th road connects cities ai and bi, and the length of i-th road is li.

The next line contains an integer q (1 ≤ q ≤ 105) — the number of road length changes.

Next q lines describe the length changes. The j-th line of them (1 ≤ j ≤ q) contains two space-separated integers rjwj (1 ≤ rj ≤ n - 1,1 ≤ wj ≤ 103). It means that in the j-th repair, the length of the rj-th road becomes wj. It is guaranteed that wj is smaller than the current length of the rj-th road. The same road can be repaired several times.

Output

Output q numbers. For each given change, print a line containing the expected cost needed to build the network in Tree World. The answer will be considered correct if its absolute and relative error doesn't exceed 10 - 6.

Sample test(s)
input
3
2 3 5
1 3 3
5
1 4
2 2
1 2
2 1
1 1
output
14.0000000000
12.0000000000
8.0000000000
6.0000000000
4.0000000000
input
6
1 5 3
5 3 2
6 1 7
1 4 4
5 2 3
5
1 2
2 1
3 5
4 1
5 2
output
19.6000000000
18.6000000000
16.6000000000
13.6000000000
12.6000000000

题意:给你一棵树,随机在里面选三个节点,然后分别连接这三个节点,问距离的和的期望是多少。

思路:首先选法有n*(n-1)*(n-2)/6种,每条边设其两边的节点数分别为a和b,那么这条边用到的情况为a*(a-1)*b/2+b*(b-1)*a/2种,而这三个节点间的每条边都需要用两次。另外我用了非递归的形式。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
typedef long long ll;

struct node
{
    int v,pos;
};
node A;
vector<node> vc[100010];
ll len[100010],use[100010],D[100010],M;
double D2[100010],W;
int p[100010],vis[100010],point[100010],f[100010];
int main()
{
    int T,t,n,m,i,j,k,temp=0,u,v,pos;
    ll a,b;
    scanf("%d",&n);
    for(i=1;i<n;i++)
    {
        scanf("%d%d%I64d",&u,&v,&len[i]);
        A.v=v;A.pos=i;
        vc[u].push_back(A);
        A.v=u;
        vc[v].push_back(A);
    }
    p[1]=1;temp=1;vis[1]=1;
    for(i=1;i<=n;i++)
    {
        u=p[i];
        k=vc[u].size();
        for(j=0;j<k;j++)
        {
            v=vc[u][j].v;
            if(vis[v])
              continue;
            vis[v]=1;
            p[++temp]=v;
            point[temp]=vc[u][j].pos;
            f[temp]=i;
        }
    }
    for(i=n;i>=1;i--)
    {
        use[i]++;
        D[point[i]]=use[i];
        use[f[i]]+=use[i];
    }
    for(i=1;i<n;i++)
    {
        a=D[i];b=n-D[i];
        D[i]=a*(a-1)*b+b*(b-1)*a;
    }
    M=(ll)n*(n-1)*(n-2)/6;
    for(i=1;i<n;i++)
       D2[i]=1.0*D[i]/M;
    for(i=1;i<n;i++)
       W+=D2[i]*len[i];
    scanf("%d",&m);
    for(i=1;i<=m;i++)
    {
        scanf("%d%I64d",&pos,&a);
        W-=D2[pos]*(len[pos]-a);
        len[pos]=a;
        printf("%.10f\n",1.0*W);
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值