【题目链接】:http://codeforces.com/problemset/problem/500/D

【题意】



有n个节点构成一棵树;

让你随机地选取3个不同的点a,b,c;

然后计算dis(a,b)+dis(b,c)+dis(a,c)的期望;

不止如此;

这里边还会减小;

要求你动态维护这个期望

【题解】



我们在选取这3个点的时候;

从最后的结果出发;

先取其中两个点(a,b);

则第三个点必然是从剩余的n-2个点中取出的;

也就是说有n-2条(a,b)路径最后需要算进答案;

而对于任选的a,b都是这样;

可知所有的dis(a,b)+dis(b,c)+dis(a,c);

最后就为任意两点之间的距离的和的n-2倍;

这个当成分子;

然后分母是C(N,3);

就是它的期望了;

任意两点之间的距离和很容易算出来的;

然后改变的话和算的时候类似的方法;

都是考虑这条改变的边的两边有多少个点;

然后相乘再加起来;减掉这个改变量就好;



【Number Of WA】



0



【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)
#define Open() freopen("F:\\rush.txt","r",stdin)
#define Close() ios::sync_with_stdio(0),cin.tie(0) typedef pair<int,int> pii;
typedef pair<LL,LL> pll; const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e5+100; struct abc
{
int x,y,z;
}; LL n,siz[N],fz;
vector <pii> G[N];
abc bian[N]; void dfs(int x,int fa)
{
siz[x] = 1;
for (pii temp:G[x])
{
int y = temp.fi;
LL w = temp.se;
if (y==fa) continue;
dfs(y,x);
fz = fz + (n-siz[y])*siz[y]*w;
siz[x] = siz[x]+siz[y];
}
} int main()
{
//Open();
Close();//scanf,puts,printf not use
//init??????
cin >> n;
rep1(i,1,n-1)
{
int x,y,z;
cin >> x >> y >> z;
bian[i] = {x,y,z};
G[x].pb(mp(y,z));
G[y].pb(mp(x,z));
}
dfs(1,0);
//cout << fz << endl;
int q;
cin >> q;
while (q--)
{
LL x,y;
cin >>x>>y;
LL d = bian[x].z-y;
bian[x].z = y;
int u = bian[x].x,v = bian[x].y;
int k;
if (siz[u]<siz[v])
k = u;
else
k = v;
fz-=siz[k]*(n-siz[k])*d;
//cout << fz<<endl;
//return 0;
double q = 6*fz,w = n*(n-1);
cout << fixed << setprecision(10) << q/w << endl;
}
return 0;
}

【codeforces 500D】New Year Santa Network的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 【codeforces 752F】Santa Clauses and a Soccer Championship

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  3. 【50.00%】【codeforces 602C】The Two Routes

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  4. 【30.49%】【codeforces 569A】Music

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  6. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  7. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  8. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  9. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

随机推荐

  1. expect离线安装

    expect5.45.4.tar.gz和tcl8.4.11-src.tar.gz压缩包请前往以下链接下载: https://download.csdn.net/download/gangzi221/1 ...

  2. 大数据之hadoop小文件存档

    hadoop小文件存档1.HDFS存档小文件弊端 每个文件均按块存储,每个块的元数据存储在NameNode的内存中,因此HDFS存储小文件会非常低效.因为大量的小文件会耗尽NameNode中的大部分内 ...

  3. 8 包含min函数的栈

    0 引言 题目:定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的min函数.在该栈中,调用min.push及pop的时间复杂度都是O(). 1 抽象问题具体化 2 具体问题抽象分析 需要解 ...

  4. 更改网卡名称以及重启网卡提示Determining if ip address x.x.x.x is already in use for device eth0

    安装系统完成后,在CentOS6.6下网卡名称变为em1,有些不太方便,还是改回eth0 修改grub配置文件,vi /boot/grub/grub.conf,增加如下红色字体 kernel /vml ...

  5. POJ-1976-A Mini Locomotive-dp

    A train has a locomotive that pulls the train with its many passenger coaches. If the locomotive bre ...

  6. [kuangbin带你飞]专题一 简单搜索 - C - Catch That Cow

    #include<iostream> #include<cstdio> #include<string> #include<vector> #inclu ...

  7. JavaScript开发人员必知的10个关键习惯

    还在一味没有目的的编写JavaScript代码吗?那么你就OUT了!让我们一起来看看小编为大家搜罗的JavaScript开发人员应该具备的十大关键习惯吧! 随着新技术的不断发展,JavaScript已 ...

  8. 11_springmvc之RESTful支持

    一.理解RESTful RESTful架构,就是一种互联网软件架构.它结构清晰.符合标准.易于理解.扩展方便,所以正得到越来越多网站的采用. RESTful(即Representational Sta ...

  9. Requests+Excel接口自动化测试(Python)

    一.框架结构:  工程目录 二.Case文件设计 三.基础包 base 3.1 封装get/post请求(runmethon.py) import requests import json class ...

  10. springcloud系列12 config的使用

    config组件分为server端和client端 config的原理: 就是当我们将配置文件放置在git上面,那么configserver就会去拉取相关配置文件至本地: 可以看到我本地是拉去了配置文 ...