【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 [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...
随机推荐
- Html标签学习笔记二
1.常用标签 <a></a>超链接 功能 做链接 :在href属性里面写明指向的地方 做下载:href指向文件(注意:不能下载的文件是因为浏览器可以直 ...
- thinkphp 应用编译
应用编译机制作为ThinkPHP独创的功能特色,从1.0版本就延续至今,3.2版本的编译机制更加具有特色. 应用编译缓存 编译缓存的基础原理是第一次运行的时候把核心需要加载的文件去掉空白和注释后合并到 ...
- Divide by Zero 2018 and Codeforces Round #474 (Div. 1 + Div. 2, combined)G - Bandit Blues
题意:求满足条件的排列,1:从左往右会遇到a个比当前数大的数,(每次遇到更大的数会更换当前数)2.从右往左会遇到b个比当前数大的数. 题解:1-n的排列,n肯定是从左往右和从右往左的最后一个数. 考虑 ...
- 解决Delphi 2010启动时卡死并报displayNotification堆栈溢出错误
1. 清理IE的历史记录,删除浏览器缓存(不需要清cookie) 2. 禁用startpage 2.1 从 Delphi 2010 启动菜单上点右键 -> 查看属性->快捷方式->目 ...
- SCRIPT7002: XMLHttpRequest: 网络错误 0x2ef3的解决方法
最近在使用jquery easyui datagrid 对页面布局,发现有时在IE下会接收不到数据并报错: SCRIPT7002: XMLHttpRequest: 网络错误 0x2ef3, 由于出现错 ...
- Spring的refresh()方法相关异常
如果是经常使用Spring,特别有自己新建ApplicationContext对象的经历的人,肯定见过这么几条异常消息:1.LifecycleProcessor not initialized - c ...
- python语句结构(控制语句与pass语句)
python语句结构(控制语句和pass语句) break-跳出循环:语句可以跳出for和while语句的循环体.如果你从for和while循环中终止,任何对应循环的else语块均终止 continu ...
- Amazon AWS
- vc 获取窗口标题GetWindowText
今天在写一个模块,具体功能是想时刻监控用户当前活动窗口,需要获取窗口标题以及其它相关信息,记得API GetWindowText就是用来做这个的,结果试了半天,有的获取成功了有的获取失败了,而且有关汉 ...
- gnome3 修改桌面背景图片模式
修改背景图片,可以在 桌面右键 选择"修改壁纸",选择"background"(背景),这里没有设定背景图片模式. 可以在 “应用程序”->"工 ...