【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 ...
随机推荐
- 某一线互联网公司前端面试题js部分总结
js部分 1,使用严格模式的优点 - 消除Javascript语法的一些不合理.不严谨之处,减少一些怪异行为; - 消除代码运行的一些不安全之处,保证代码运行的安全: - 提高编译器效率,增加运行速度 ...
- navicat for mysql 10.1.7 注册码
NAVN-LNXG-XHHX-5NOO名:组织:注册码:均为NAVN-LNXG-XHHX-5NOO 下载地址:http://www.cr173.com/soft/38153.html
- MyBatis原理系列
原理分析之一:从JDBC到Mybatis 原理分析之二:框架整体设计 原理分析之三:初始化(配置文件读取和解析) 原理分析之四:一次SQL查询的源码分析
- 【bzoj2259】[Oibh]新型计算机 堆优化Dijkstra
题目描述 Tim正在摆弄着他设计的“计算机”,他认为这台计算机原理很独特,因此利用它可以解决许多难题. 但是,有一个难题他却解决不了,是这台计算机的输入问题.新型计算机的输入也很独特,假设输入序列中有 ...
- Python 源码剖析(一)【python对象】
处于研究python内存释放问题,在阅读部分python源码,顺便记录下所得.(基于<python源码剖析>(v2.4.1)与 python源码(v2.7.6)) 先列下总结: ...
- Skills - CF613B
Lesha plays the recently published new version of the legendary game hacknet. In this version charac ...
- POJ3690:Constellations——题解
http://poj.org/problem?id=3690 题目大意:给一个图和几个子图,判断有多少种子图在原图出现过. —————————————————————— 二维哈希即可,操作看代码,我觉 ...
- BZOJ2427:[HAOI2010]软件安装——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=2427 https://www.luogu.org/problemnew/show/P2515 现在 ...
- BZOJ1023:[SHOI2008]仙人掌图——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=1023 Description 如果某个无向连通图的任意一条边至多只出现在一条简单回路(simple ...
- bzoj3157: 国王奇遇记
emmm...... 直接看题解好了: BZOJ-3157. 国王奇遇记 – Miskcoo's Space O(m)不懂扔掉 总之,给我们另一个处理复杂求和的方法: 找到函数之间的递推公式! 这里用 ...