【codeforces 500D】New Year Santa Network
【题目链接】: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的更多相关文章
- 【codeforces 415D】Mashmokh and ACM(普通dp)
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...
- 【codeforces 752F】Santa Clauses and a Soccer Championship
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【50.00%】【codeforces 602C】The Two Routes
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【30.49%】【codeforces 569A】Music
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...
- 【codeforces 707E】Garlands
[题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- 【codeforces 709D】Recover the String
[题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...
- 【codeforces 709B】Checkpoints
[题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...
- 【codeforces 709C】Letters Cyclic Shift
[题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
随机推荐
- [JZOJ2679] 跨时代
题目 题目大意 给你一堆边,你要将它们围成面积最大的矩形. 边不一定要用完,而且围成的矩形不能凸出一块. \(n\leq 16\) \(l_i \leq 15\) 思考历程 看到这题的第一眼,就会立马 ...
- JMeter 返回Json数据提取方法
JMeter中,对response返回JSON格式的数据进行处理,格式如下: { "code":2000, "message":"success&qu ...
- 尚学linux课程---8、rpm软件包安装
尚学linux课程---8.rpm软件包安装 一.总结 一句话总结: rpm安装软件包的话要解决依赖问题,推荐使用yum安装软件包 1.比如cd /home中的斜线表示什么意思? 表示根目录,linu ...
- 763 Hex Conversion
原题网址:http://www.lintcode.com/zh-cn/problem/hex-conversion/ Given a decimal number n and an integer k ...
- ASP.NET的底层体系2
文章引导 1.ASP.NET的底层体系1 2.ASP.NET的底层体系2 引言 接着上一篇ASP.NET的底层体系1我们继续往下走 一.System.Web.HttpRuntime.ProcessRe ...
- mavlink 笔记1
Packet Anatomy This is the anatomy of one packet. It is inspired by the CAN and SAE AS-4 standards. ...
- day3:python运算符及数据类型(str)(int)
运算符 算数运算 :a = 10 * 10赋值运算:a = a + 1 a+=1 比较运算:a = 1 > 5 逻辑运算: a = 1>6 or 1==1 a = 1 and b = ...
- Spring - 整合MyBatis
目的: 使用 Spring 容器用单例模式管理 MyBatis 的 sqlSessionFactory : 使用 Spring 管理连接池.数据源等: 将 Dao / Mapper 动态代理对象注入到 ...
- 初识OpenCV-Python - 009: 图像梯度
本节学习找到图像的梯度和边界.只要用到的函数为: cv2.Sobel(), cv2.Scharr(), cv2.Laplacian() 1. Laplacian 和 Sobel的对比 import c ...
- 滑雪 矩阵中的最长上升路径 /// 记忆化DFS || DP oj22919
大致题意: Description 难怪Michael喜欢滑雪,因为滑雪确实很刺激.为了获得加速度,滑雪道必须向下倾斜,而且当滑到坡底,你不得不再次走上坡或者等待升降机来载你.Michael想知道在一 ...