【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 ...
随机推荐
- 201621044079 韩烨 week11-作业11-多线程
作业11-多线程 参考资料 多线程参考文件 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多线程相关内容. 2. 书面作业 本次PTA作业题集多线程 1. 源代码阅读:多线程程序 ...
- ZOJ 2072 K-Recursive Survival
https://vjudge.net/contest/67836#problem/K n people numbered 1 to n around a circle, we eliminate ev ...
- node中的path.resolve
path.resolve([arg1,arg2,...])根据参数的不同,返回值存在两种情况. 以下为参数的两种情况: 1.每个参数都不带'/',比如path.resolve(),或者path.res ...
- CodeChef LEMOVIE
题意:给你n个数字(下标不同数值相同的数字应当被认为是不同的数字),有n!种排列方式.每种排列方式的价值定义为:第一次出现时比前面的所有数字都大的数值个数. 比如1,2,2,3这个排列中,1,2,3这 ...
- 封装一个jquery库
现在Javascript库海量,流行的也多,比如jQuery,YUI等,虽然功能强大,但也是不万能的,功能不可能涉及方方面面,自己写一个的JS库是对这些的补充,很多也比较实用,把应用到项目中中去也比较 ...
- [NOI.AC省选模拟赛3.31] 星辰大海 [半平面交]
题面 传送门 思路 懒得解释了......也是比较简单的结论 但是自己看到几何就退缩了...... 下周之内写一个计算几何的学习笔记! Code #include<iostream> #i ...
- POJ.3172 Scales (DFS)
POJ.3172 Scales (DFS) 题意分析 一开始没看数据范围,上来直接01背包写的.RE后看数据范围吓死了.然后写了个2^1000的DFS,妥妥的T. 后来想到了预处理前缀和的方法.细节以 ...
- mysql 读写分离实现资料
以下很多链接需要 FQ才能看到,稍后会整理翻译成中文! Easy Read/Write Splitting with PHP’s MySQLnd https://blog.engineyard.com ...
- javascript forEach无法break,使用every代替
every的入口参数是一个返回bool值的函数,在需要break的地方return false,其他均return true,即可达到和break相同的效果 function find(arr2, e ...
- TYVJ2032 升降梯上
Description: 开启了升降梯的动力之后,探险队员们进入了升降梯运行的那条竖直的隧道,映入眼帘的是一条直通塔顶的轨道.一辆停在轨道底部的电梯.和电梯内一杆控制电梯升降的巨大手柄.Nescafe ...