【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 [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
随机推荐
- AmqpException: No method found for class java.lang.String
amqpTemplate发送消息用的String,接收消息用的Message,统一消息类型就可以
- Python编码decode和encode
常见编码介绍: GB2312编码:适用于汉字处理.汉字通信等系统之间的信息交换;GBK编码:是汉字编码标准之一,是在 GB2312-80 标准基础上的内码扩展规范,使用了双字节编码ASCII编码:是对 ...
- 动态调整Log4j日志级别
log4j2.xml配置文件中支持配置monitorInterval参数,检测到配置改变后重新加载,达到动态调整日志级别的效果. 故调整日志级别无须手动重启服务. log4j2.xml配置文件示意: ...
- MySQL 调优/优化的 101 个建议!
原文:http://www.monitis.com/blog/101-tips-to-mysql-tuning-and-optimization/ MySQL是一个强大的开源数据库.随着MySQL上的 ...
- 18.scrapy_maitian
ershoufang.py # -*- coding: utf-8 -*- import scrapy class ErshoufangSpider(scrapy.Spider): name = 'e ...
- Python操作三大主流数据库✍✍✍
Python操作三大主流数据库 Python 标准数据库接口为 Python DB-API,Python DB-API为开发人员提供了数据库应用编程接口. Python 数据库接口支持非常多的数据库, ...
- springcloud系列15 bus的使用
可以结合wekbbok进行通知: 将更新的代码进行点击就好:
- js 实时监听textarea输入
html: <textarea class="area" name="" id="text1" maxlength="100 ...
- java_新特性未整理
得到的.className method.isAnnotationPresent:判断是否有指定的注解,注解.class method.invoke:执行 */}
- java_IO流(输出流)
** * io流: * 输入流:硬盘输入到内存 字节/字符输入流 * 输出流:内存输出到硬盘 字节/字符输入流 * 字节流:一切数据都是字节存储(二进制) * 字节输出流(OutputStream): ...