【HNOI】 小A的树 tree-dp
【题目描述】给定一颗树,每个点有各自的权值,任意选取两个点,要求算出这两个点路径上所有点的and,or,xor的期望值。
【数据范围】n<=10^5
首先期望可以转化为求树上所有点对的and,or,xor值的和,然后在除以n*n就可以了,对于权值,我们可以按位做,这样问题就转化为了给定一棵树,树上的点的权值为0或者1,我们需要求出来点对的and,or,xor值,这个问题我们可以取任意节点当做根,然后用tree-dp来解决
and:这个比较好处理,我们只需要记录每个节点x,p为x子树中一点,x到p的路径上全部为1,这样的p的点的数量,这个比较容易转移,记录这个为sum,那么
sum[x]=Σsum[p] col[x]==1
sum[x]=0 col[x]==0 col为颜色。
维护了这个东西之后我们就可以处理答案了,对于所有x的点对,有两种情况,第一种是一端点是x,另一端点是x子树中的点,对于这样的点,我们只需要累加sum[son of x]就可以了,因为x颜色可能是0,所以我们不能直接加sum[x],还有一种情况是这个点对在x的子树中,且路径经过x,那么这样的点对我们只需要记录一个tot代表Σsum[son of x]然后对于x的每一个子节点p,我们需要累加答案sum[p]*(tot-sum[p]),这样就可以了。
or:维护一个num数组,设p为x的子树中一点,那么num[x]为所有x到p的路径上存在一个1的p的个数,设size[x]表示以x为根节点那么
num[x]=size[x] col[x]==1
num[x]=Σnum[p] col[x]==0
这样我们就可以求出来num[x]了,对于答案的累加类似于and的累加,对于跨根的,我们只需要使路径的一部分有1就好了,也就是ans+=num[p]*(size[x]-size[p]-1)。
xor:我们可以维护一个a0[x]和a1[x]代表以x为根的子树中,p为其中一点,x到p的路径上1的个数为奇/偶的p的点的数量,那么比较显然的是
a0[x]=Σa1[p] a1[x]=Σa0[p] col[x]==1
a0[x]=Σa0[p] a1[x]=Σa1[p] col[x]==0
这个的累加答案也类似于上面,我们需要用a0[p]和a1[p]来累加答案。
反思:比赛的时候没考虑到然后栈溢出了,后来改成bfs就好了。
//By BLADEVIL
#include <cstdio>
#include <cstring>
#define maxn 100010
#define LL long long
#pragma comment(linker,"/STACK:102400000,102400000") using namespace std; LL n,l;
LL pre[maxn<<],other[maxn<<],last[maxn],key[maxn],color[maxn],sum[maxn],size[maxn],num[maxn],a0[maxn],a1[maxn],que[maxn],flag[maxn],father[maxn];
LL ans,ANS,ANSor,ansor,ANSx,ansx; void connect(LL x,LL y) {
//printf("%d %d %d\n",x,y,l);
pre[++l]=last[x];
last[x]=l;
other[l]=y;
} void update(LL x){
//printf("%d ",x);
sum[x]=color[x]; size[x]=; num[x]=;
if (color[x]) a1[x]=,a0[x]=; else a0[x]=,a1[x]=;
for (LL p=last[x];p;p=pre[p]) {
if (other[p]==father[x]) continue;
size[x]+=size[other[p]];
}
if (sum[x]) {
LL tot=;
for (LL p=last[x];p;p=pre[p]) if (other[p]!=father[x]) ans+=*sum[other[p]],tot+=sum[other[p]];
for (LL p=last[x];p;p=pre[p]) if (other[p]!=father[x]) ans+=sum[other[p]]*(tot-sum[other[p]]);
sum[x]+=tot;
}
if (color[x]) {
for (LL p=last[x];p;p=pre[p])
if (other[p]!=father[x]) ansor+=size[other[p]]*(size[x]-size[other[p]]);
num[x]=size[x];ansor+=num[x];
} else {
LL tot=size[x];
for (LL p=last[x];p;p=pre[p])
if (other[p]!=father[x]) ansor+=*num[other[p]]*(tot-size[other[p]]),num[x]+=num[other[p]],tot-=num[other[p]];
}
if (color[x]) {
LL tot0=,tot1=;
for (LL p=last[x];p;p=pre[p])
if (other[p]!=father[x]) tot0+=a0[other[p]],tot1+=a1[other[p]];
ansx+=*tot0+;
for (LL p=last[x];p;p=pre[p])
if (other[p]!=father[x]) ansx+=*a0[other[p]]*(tot0-a0[other[p]]),tot0-=a0[other[p]];
for (LL p=last[x];p;p=pre[p])
if (other[p]!=father[x]) ansx+=*a1[other[p]]*(tot1-a1[other[p]]),tot1-=a1[other[p]];
for (LL p=last[x];p;p=pre[p])
if(other[p]!=father[x]) a1[x]+=a0[other[p]],a0[x]+=a1[other[p]];
} else {
LL tot0=,tot1=;
for (LL p=last[x];p;p=pre[p])
if (other[p]!=father[x]) tot0+=a0[other[p]],tot1+=a1[other[p]];
ansx+=*tot1;
for (LL p=last[x];p;p=pre[p])
if (other[p]!=father[x]) ansx+=*a1[other[p]]*(tot0-a0[other[p]]);
for (LL p=last[x];p;p=pre[p])
if (other[p]!=father[x]) a1[x]+=a1[other[p]],a0[x]+=a0[other[p]];
}
//printf("%d %d\n",x,ansor);
//printf("%d %d\n",x,ansx);
} int main() {
freopen("tree.in","r",stdin); freopen("tree.out","w",stdout);
LL task,x,y; scanf("%lld",&task);
while (task--) {
memset(last,,sizeof last);
memset(flag,,sizeof flag); l=;
ANS=ANSor=ANSx=;
scanf("%lld",&n);
for (LL i=;i<=n;i++) scanf("%lld",&key[i]);
for (LL i=;i<n;i++) {
scanf("%lld%lld",&x,&y);
connect(x,y); connect(y,x);
}
//printf("fuck\n");
LL h=,t=;
que[]=; flag[]=;
while (h<t) {
LL cur=que[++h];
flag[cur]=;
for (LL p=last[cur];p;p=pre[p]) {
if (flag[other[p]]) continue;
que[++t]=other[p];
father[other[p]]=cur;
}
}
//for (LL i=1;i<=n;i++) printf("%d ",que[i]);
LL cur=;
for (LL i=;i<=;i++) {
for (LL j=;j<=n;j++) color[j]=(key[j]&cur)?:;
ans=ansor=ansx=; //work(1,-1);
for (LL j=n;j;j--) update(que[j]);
for (LL j=;j<=n;j++) ans+=color[j]; //printf("%d\n",ansor);//printf("%d\n",ans);
ANS+=ans*cur;
ANSor+=ansor*cur;
ANSx+=ansx*cur;
//for (LL j=1;j<=n;j++) printf("%d %d %d\n",j,size[j],sum[j]); printf("\n");
//for (LL j=1;j<=n;j++) printf("%d %d %d\n",j,size[j],num[j]); printf("\n");
//for (LL j=1;j<=n;j++) printf("%d %d %d\n",j,a0[j],a1[j]); printf("\n");
cur<<=;
}
double ans1=double(ANS)/double(n*n);
double ans2=double(ANSor)/double(n*n);
double ans3=double(ANSx)/double(n*n);
printf("%.3f %.3f %.3f\n",ans1,ans2,ans3);
}
fclose(stdin); fclose(stdout);
return ;
}
【HNOI】 小A的树 tree-dp的更多相关文章
- 牛客挑战赛30 小G砍树 树形dp
小G砍树 dfs两次, dp出每个点作为最后一个点的方案数. #include<bits/stdc++.h> #define LL long long #define fi first # ...
- 【BZOJ5072】[Lydsy十月月赛]小A的树 树形DP
[BZOJ5072][Lydsy十月月赛]小A的树 题解:考虑我们从一个联通块中替换掉一个点,导致黑点数量的变化最多为1.所以我们考虑维护对于所有的x,y的最大值和最小值是多少.如果询问的y在最大值和 ...
- bzoj 5072 小A的树 —— 树形DP
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=5072 由于对于一个子树,固定有 j 个黑点,连通块大小是一个连续的范围: 所以记 f[i][ ...
- 小A的树 - 树形DP
题面 1 9 4 4 1 1 5 1 2 3 2 3 6 6 7 6 8 9 6 0 1 0 1 0 0 1 0 1 3 2 7 3 4 0 9 5 YES YES NO NO 题解 n <= ...
- bzoj 5072 [Lydsy1710月赛]小A的树——树形dp
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=5072 发现对于每个子树,黑点个数确定时,连通块的大小取值范围一定是一段区间:所以考虑只最小化 ...
- BZOJ5072:[Lydsy1710月赛]小A的树(树形DP)
Description BZOJ只是扔了个下载链接 Solution 设$f[x][i]$表示$x$点选中$i$个黑点的最小连通块. 设$g[x][i]$表示$x$点选中$i$个黑点的最大连通块. 转 ...
- 洛谷P4426 毒瘤 [HNOI/AHOI2018] 虚树+树上dp
正解:虚树+树上dp 解题报告: 传送门! 首先解释一下题意趴,,,语文70pts选手已经开始看不懂题辣QAQ 大概就是个给一个图,求独立集方案,且保证图是联通的,边的数量最多只比点多10 首先思考如 ...
- 2018.07.08 hdu4521 小明系列问题——小明序列(线段树+简单dp)
小明系列问题--小明序列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Proble ...
- HDU 5905 Black White Tree(树型DP)
题目链接 Black White Tree 树型DP,设$f[i][j]$为以$i$为根的子树中大小为$j$的连通块中可以包含的最小黑点数目. $g[i][j]$为以$i$为根的子树中大小为$j$的 ...
- 【POJ 2486】 Apple Tree(树型dp)
[POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8981 Acce ...
随机推荐
- html中图片自适应浏览器和屏幕,宽度高度自适应
1.(宽度自适应):在网页代码的头部,加入一行viewport元标签. <meta name="viewport" content="width=device-wi ...
- [Leetcode] 1.Two Sum(unordered_map)
1.首先想到的方法就是两个for循环全部遍历,代码如下,可通过,但效率太低 class Solution { public: vector<int> twoSum(vector<in ...
- Ajax在jQuery中的应用($.ajax()方法)
Ajax() 方法 $.ajax() 中的参数及使用方法 在jQuery中,$.ajax() 是最底层的方法,也是功能最强的方法.$.get().$.post().$.getScript().getJ ...
- 【Python】python文件名和文件路径操作
Readme: 在日常工作中,我们常常涉及到有关文件名和文件路径的操作,在python里的os标准模块为我们提供了文件操作的各类函数,本文将分别介绍“获得当前路径”“获得当前路径下的所有文件和文件夹, ...
- MATLAB中的randi函数
randi Pseudorandom integers from a uniform discrete distribution.来自一个均匀离散分布的伪随机整数 R = randi(IMAX,N) ...
- (转)python 搭建libsvm方法。python版本和libsvm版本匹配很重要!
<集体智慧编程>关于婚介数据集的SVM分类 转自:http://muilpin.blog.163.com/blog/static/165382936201131875249123/ 作 ...
- kafka搭建笔记
环境CentOS7.0,JDK1.8 一.下载安装 在kafka官网 http://kafka.apache.org/downloads下载到最新的kafka安装包 下载 2.0.0 release, ...
- [BZOJ5463] [APIO2018] 铁人两项
题目链接 LOJ. BZOJ. Solution 先建圆方树. 我们考虑暴力,枚举一个点对,我们枚举的点都是圆点,然后统计中间那个点可以取的位置的数量,加起来就是答案. 那么怎么统计呢,我们对于每个点 ...
- LOJ6354 & 洛谷4366:[Code+#4]最短路——题解
https://loj.ac/problem/6354 https://www.luogu.org/problemnew/show/P4366 题面见上面. 这题很妙,且可能是我傻,感觉这题不太好想. ...
- [BZOJ3523][Poi2014]KLO-Bricks——全网唯一 一篇O(n)题解+bzoj最优解
Description 有n种颜色的砖块,第i种颜色的砖块有a[i]个,你需要把他们放成一排,使得相邻两个砖块的颜色不相同,限定第一个砖块的颜色是start,最后一个砖块的颜色是end,请构造出一种合 ...