Codeforces 629 E. Famil Door and Roads】的更多相关文章

题目链接:http://codeforces.com/problemset/problem/629/E 询问这个简单环的期望.考虑将这个环拆成两部分. 令${deep[x]>=deep[y]}$,${size[x]}$表示以$x$为根的子树大小,${sdown[x]}$示以$x$为根的子树的所有节点到$x$的距离和,${sall[x]}$所有点到$x$的距离和.${ne}$表示从${y-->x}$路径上${y}$的儿子. 1.${dis(x,y)}$这是一个环肯定要经过的,算入答案. 2.分情…
E. Famil Door and Roads 题目连接: http://www.codeforces.com/contest/629/problem/E Description Famil Door's City map looks like a tree (undirected connected acyclic graph) so other people call it Treeland. There are n intersections in the city connected b…
题目链接: http://www.codeforces.com/contest/629/problem/E 题解: 树形dp. siz[x]为x这颗子树的节点个数(包括x自己) dep[x]表示x这个节点的深度,从1开始(其实从什么开始都可以,我们这里用到的只是相对距离) 对于查询u,v,总共有三种情况: 1.u为公共祖先 设x为(u,v)链上u的儿子,则我们知道新边只能从非x子树的点(n-siz[x]连到以v为根的子树上的点(siz[v]) 则新边的总条数为(n-siz[x])*siz[v]…
Famil Door's City map looks like a tree (undirected connected acyclic graph) so other people call it Treeland. There are n intersections in the city connected by n - 1 bidirectional roads. There are m friends of Famil Door living in the city. The i-t…
一棵树,n个节点,边长为1,有q个询问,每个询问给出u,v(u != v),问在树上等概率加一条边,如果使得u,v在一个环内,则这种加边方式是合法的,此时的值为环的长度,所有合法的加边方式出现的概率相等,问值的期望. 2 <= n,m <= 10^5 对于u,v原来路径上的边一定在环内,贡献为1,新加的边也一定在环内,贡献为1,求其余的边的贡献就行了 分2种情况考虑: 1.lca(u,v) 不等于u 和 v 2.lca(u,v) 为u 或者 v 代码: //File Name: cf629E.…
New Year and Rainbow Roads 思路:我们考虑两个绿点之间的红点和蓝点, 首先把这些红点和蓝点接到绿点上面绝对不会超过绿点距离的两倍. 然后我们先把两个绿点连上, 再把绿点经过蓝点到绿点的线连上, 绿点经过红点到绿点的线连上, 这时距离为3倍的绿点间距离, 然后我们可以在第二条路径和第三条路径上断开一段最长的, 和两个的绿点距离取个最小值,就是这段的贡献. #include<bits/stdc++.h> #define LL long long #define fi fi…
  B. Far Relative’s Problem   time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Famil Door wants to celebrate his birthday with his friends from Far Far Away. He has n friends and each of th…
  A. Far Relative’s Birthday Cake   time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Door's family is going celebrate Famil Doors's birthday party. They love Famil Door so they are planning…
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output As Famil Door's birthday is coming, some of his friends (like Gabi) decided to buy a present for him. His friends are going to buy a string cons…
题意:在一条数轴上有若干'R','B','G'点,连接两个点的代价是位置差,要求使用最小代价使得除去所有'R'点后剩下的所有点联通,除去所有'B'点后剩下的所有点联通. 还以为会是什么最小生成树,结果是脑洞题啊 因为G点总是要保留下来的,所以考虑按照G分成若干块,对于每个以G开头以G结尾的块,考虑如下两种操作: 1.分别依次连接 左G-中间所有的R-右G.左G-中间所有的B-右G,代价为 $ 2*len $ 2.连接两端的G,再由两端的G依次分别连接中间的B和R,即,把两端的G和R.B分别依次连…