【题目描述】给定一颗树,每个点有各自的权值,任意选取两个点,要求算出这两个点路径上所有点的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的更多相关文章

  1. 牛客挑战赛30 小G砍树 树形dp

    小G砍树 dfs两次, dp出每个点作为最后一个点的方案数. #include<bits/stdc++.h> #define LL long long #define fi first # ...

  2. 【BZOJ5072】[Lydsy十月月赛]小A的树 树形DP

    [BZOJ5072][Lydsy十月月赛]小A的树 题解:考虑我们从一个联通块中替换掉一个点,导致黑点数量的变化最多为1.所以我们考虑维护对于所有的x,y的最大值和最小值是多少.如果询问的y在最大值和 ...

  3. bzoj 5072 小A的树 —— 树形DP

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=5072 由于对于一个子树,固定有 j 个黑点,连通块大小是一个连续的范围: 所以记 f[i][ ...

  4. 小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 <= ...

  5. bzoj 5072 [Lydsy1710月赛]小A的树——树形dp

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=5072 发现对于每个子树,黑点个数确定时,连通块的大小取值范围一定是一段区间:所以考虑只最小化 ...

  6. BZOJ5072:[Lydsy1710月赛]小A的树(树形DP)

    Description BZOJ只是扔了个下载链接 Solution 设$f[x][i]$表示$x$点选中$i$个黑点的最小连通块. 设$g[x][i]$表示$x$点选中$i$个黑点的最大连通块. 转 ...

  7. 洛谷P4426 毒瘤 [HNOI/AHOI2018] 虚树+树上dp

    正解:虚树+树上dp 解题报告: 传送门! 首先解释一下题意趴,,,语文70pts选手已经开始看不懂题辣QAQ 大概就是个给一个图,求独立集方案,且保证图是联通的,边的数量最多只比点多10 首先思考如 ...

  8. 2018.07.08 hdu4521 小明系列问题——小明序列(线段树+简单dp)

    小明系列问题--小明序列 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others) Proble ...

  9. HDU 5905 Black White Tree(树型DP)

    题目链接  Black White Tree 树型DP,设$f[i][j]$为以$i$为根的子树中大小为$j$的连通块中可以包含的最小黑点数目. $g[i][j]$为以$i$为根的子树中大小为$j$的 ...

  10. 【POJ 2486】 Apple Tree(树型dp)

    [POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8981   Acce ...

随机推荐

  1. Nautilus-Share-Message: Called "net usershare info" but it failed: Failed to

    See what nautilus processes are running : ps aux | grep nautilus Kill all nautilus processes you see ...

  2. 简单名称值对节点类NameValuePair

    本类位于System.Data.dll中,名为:System.Data.Common.NameValuePair.主要用途是在DBConnectionString类中,解析ConnectionStri ...

  3. vs中编译连接时的警告屏蔽

    编译警告类型为warning C4996的形式 可以采用的#pragma warning(disable:4996)屏蔽掉这种编译警告 连接警告类型为warning LNK4049的形式 这是由于不同 ...

  4. [剑指Offer] 53.表示数值的字符串

    题目描述 请实现一个函数用来判断字符串是否表示数值(包括整数和小数).例如,字符串"+100","5e2","-123","3.1 ...

  5. NOIP1998 提高组

    [NOIP2002] 提高组 T2.联接数 算法:贪心+字符串处理 [问题分析]: 按整数对应的字符串大到小连接,因为题目的例子都符合,但是不难找到反例:12   121 应该组成12121而非121 ...

  6. Mysql 基本语句练习

    一.怎样查看数据库信息? desc 数据库名; 二.怎样查看数据表信息? desc 表名:          //查看表的属性和属性值 或者用select语句: //查看表的行记录信息 select ...

  7. CentOS 文本搜索grep

    grep 用于在文本中执行关键词搜索, 用法: grep [选项]... PATTERN [FILE]... [root@bigdata-senior01 ~]# grep "ftp&quo ...

  8. POJ2549:Sumsets——题解

    http://poj.org/problem?id=2549 题目大意:从集合中找到四个不相同的数,满足a+b+c=d,输出最大的d. —————————————————————————— 该式子变为 ...

  9. 【简单算法】17.字符串转整数(atoi)

    题目: 实现 atoi,将字符串转为整数. 在找到第一个非空字符之前,需要移除掉字符串中的空格字符.如果第一个非空字符是正号或负号,选取该符号,并将其与后面尽可能多的连续的数字组合起来,这部分字符即为 ...

  10. 使用openssl进行文件加密

    #include <iostream> #include <string> #include <stdlib.h> using namespace std; int ...