【题目链接】: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. SQL Server install

    { https://www.cnblogs.com/ios9/p/9527939.html https://www.cnblogs.com/ios9/p/9527815.html //在安装工具中 安 ...

  2. 用连接池链接redis

    package com.itheima.utils; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; i ...

  3. (转)JNI入门教程之HelloWorld篇 .

    转: http://blog.csdn.net/mingjava/article/details/180946 本文讲述如何使用JNI技术实现HelloWorld,目的是让读者熟悉JNI的机制并编写第 ...

  4. Vuex听说很难?

    Vuex 是什么? Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件状态,并以相应的规则保证状态以一种可预测的方式发生变化.   什么鬼东西 看完这段 ...

  5. idea运行tomcat,控制台中文乱码

    加入参数:-Dfile.encoding=UTF-8

  6. AtCoder ABC 130E Common Subsequence

    题目链接:https://atcoder.jp/contests/abc130/tasks/abc130_e 题目大意 给定一个长度为 N 的序列 S 和一个长度为 M 的序列 T,问 S 和 T 中 ...

  7. java中字符数组与字符串之间互相转换的方法

    public static void main(String[] args) { //1.字符数组 转换成 字符串 //(1)直接在构造String时转换 char[] array = new cha ...

  8. day3:python测试题

    1.Python的怎么单行注释和多行注释: 单行注释:# 多行注释: '''     '''      或者  “”“        ”“”    . 2.布尔值分别是什么 ? True    /Fa ...

  9. [SNOI2017]遗失的答案

    题目 首先\(G,L\)肯定会满足\(G|L\),否则直接全部输出\(0\) 之后我们考虑一下能用到的质因数最多只有\(8\)个 同时我们能选择的数\(x\)肯定是\(L\)的约数,还得是\(G\)的 ...

  10. <转>http协议 文件下载原理详解

    最近研究了一下关于文件下载的相关内容,觉得还是写些东西记下来比较好.起初只是想研究研究,但后来发现写个可重用性比较高的模块还是很有必要的,我想这也是大多数开发人员的习惯吧. 对于HTTP协议,向服务器 ...