题目链接:https://www.luogu.org/problem/P2664

题意:给定一颗带点权的树,结点数n<=1e5,点权<=1e5,用s(i,j)表示从i到j的路径上不同点权数,ans[i]=sum(s(i,j))。求ans数组。

思路:

  继续肝淀粉质,太难了。

  涉及到树上点对,且nlogn满足时,就可以考虑考虑点分治了。所以回到这题,我们需要在O(n)时间内统计出以p为根节点的子树上所有节点对p的贡献,以及对所有经过p的路径的贡献。

  

  我们通过dfs1得到这些贡献值和他们的和sum,显然子树上所有节点对p的贡献就是sum,即ans[p]+=sum。

  

  另外,sum、ans数组和color数组需要用LL,在这wa了两发。

AC代码:

#include<cstdio>
#include<algorithm>
#include<cctype>
using namespace std; inline int read(){
int x=,f=;char c=;
while(!isdigit(c)){f|=c=='-';c=getchar();}
while(isdigit(c)) x=(x<<)+(x<<)+(c^),c=getchar();
return f?-x:x;
} typedef long long LL;
const int maxn=1e5+;
const int inf=0x3f3f3f3f;
struct node{
int v,nex;
}edge[maxn<<]; int n,c[maxn],ct,head[maxn],Min,root,size,sz[maxn],mson[maxn];
int cnt[maxn],num,t,vis[maxn];
LL ans[maxn],color[maxn],sum; void adde(int u,int v){
edge[++ct].v=v;
edge[ct].nex=head[u];
head[u]=ct;
} void getroot(int u,int fa){
sz[u]=,mson[u]=;
for(int i=head[u];i;i=edge[i].nex){
int v=edge[i].v;
if(v==fa||vis[v]) continue;
getroot(v,u);
sz[u]+=sz[v];
if(sz[v]>mson[u]) mson[u]=sz[v];
}
if(size-sz[u]>mson[u]) mson[u]=size-sz[u];
if(mson[u]<Min) Min=mson[u],root=u;
} void dfs1(int u,int fa){
sz[u]=;
++cnt[c[u]];
for(int i=head[u];i;i=edge[i].nex){
int v=edge[i].v;
if(v==fa||vis[v]) continue;
dfs1(v,u);
sz[u]+=sz[v];
}
if(cnt[c[u]]==){
sum+=sz[u];
color[c[u]]+=sz[u];
}
--cnt[c[u]];
} void change(int u,int fa,int f){
++cnt[c[u]];
for(int i=head[u];i;i=edge[i].nex){
int v=edge[i].v;
if(v==fa||vis[v]) continue;
change(v,u,f);
}
if(cnt[c[u]]==){
sum+=sz[u]*f;
color[c[u]]+=sz[u]*f;
}
--cnt[c[u]];
} void dfs2(int u,int fa){
++cnt[c[u]];
if(cnt[c[u]]==){
sum-=color[c[u]];
++num;
}
ans[u]+=sum+num*t;
for(int i=head[u];i;i=edge[i].nex){
int v=edge[i].v;
if(vis[v]||v==fa) continue;
dfs2(v,u);
}
if(cnt[c[u]]==){
sum+=color[c[u]];
--num;
}
--cnt[c[u]];
} void clear(int u,int fa){
cnt[c[u]]=color[c[u]]=;
for(int i=head[u];i;i=edge[i].nex){
int v=edge[i].v;
if(vis[v]||v==fa) continue;
clear(v,u);
}
} void solve(int u){
dfs1(u,);
ans[u]+=sum;
for(int i=head[u];i;i=edge[i].nex){
int v=edge[i].v;
if(vis[v]) continue;
++cnt[c[u]],sum-=sz[v],color[c[u]]-=sz[v];
change(v,u,-);--cnt[c[u]];
t=sz[u]-sz[v];
dfs2(v,u);
++cnt[c[u]],sum+=sz[v],color[c[u]]+=sz[v];
change(v,u,);--cnt[c[u]];
}
sum=,num=;
clear(u,);
} void fenzhi(int u){
vis[u]=;
solve(u);
for(int i=head[u];i;i=edge[i].nex){
int v=edge[i].v;
if(vis[v]) continue;
Min=inf,root=,size=sz[v];
getroot(v,);
fenzhi(root);
}
} int main(){
n=read();
for(int i=;i<=n;++i)
c[i]=read();
for(int i=;i<n;++i){
int u=read(),v=read();
adde(u,v);
adde(v,u);
}
Min=inf,root=,size=n;
getroot(,);
fenzhi(root);
for(int i=;i<=n;++i)
printf("%lld\n",ans[i]);
return ;
}

  

luoguP2664树上游戏(点分治)的更多相关文章

  1. 【点分治】luoguP2664 树上游戏

    应该是一道中等难度的点分?麻烦在一些细节. 题目描述 lrb有一棵树,树的每个节点有个颜色.给一个长度为n的颜色序列,定义s(i,j) 为i 到j 的颜色数量.以及 现在他想让你求出所有的sum[i] ...

  2. 洛谷P2664 树上游戏(点分治)

    题意 题目链接 Sol 神仙题..Orz yyb 考虑点分治,那么每次我们只需要统计以当前点为\(LCA\)的点对之间的贡献以及\(LCA\)到所有点的贡献. 一个很神仙的思路是,对于任意两个点对的路 ...

  3. 洛谷P2664 树上游戏——点分治

    原题链接 被点分治虐的心态爆炸了 题解 发现直接统计路径上的颜色数量很难,考虑转化一下统计方式.对于某一种颜色\(c\),它对一个点的贡献为从这个点出发且包含这种颜色的路径条数. 于是我们先点分一下, ...

  4. luoguP2664 树上游戏

    https://www.luogu.org/problemnew/show/P2664 考虑对于每种颜色包含的点和这些点的子节点建出虚树,发现只要将一个联通块中的东西 Dp + 差分一下就行了 当然要 ...

  5. 【洛谷P2664】 树上游戏 点分治

    code: #include <bits/stdc++.h> #define N 200009 #define ll long long #define setIO(s) freopen( ...

  6. 【Luogu2664】树上游戏(点分治)

    [Luogu2664]树上游戏(点分治) 题面 洛谷 题解 很好的一道点分治题. 首先直接点分治,考虑过每个分治重心的链的贡献. 我们从分治重心开始找每种颜色,强制令一种颜色只在其到分治重心的链上第一 ...

  7. 洛谷 P2664 树上游戏 解题报告

    P2664 树上游戏 题目描述 \(\text{lrb}\)有一棵树,树的每个节点有个颜色.给一个长度为\(n\)的颜色序列,定义\(s(i,j)\) 为 \(i\) 到 \(j\) 的颜色数量.以及 ...

  8. P2664 树上游戏

    P2664 树上游戏 https://www.luogu.org/problemnew/show/P2664 分析: 点分治. 首先关于答案的统计转化成计算每个颜色的贡献. 1.计算从根出发的路径的答 ...

  9. Luogu P2664 树上游戏 dfs+树上统计

    题目: P2664 树上游戏 分析: 本来是练习点分治的时候看到了这道题.无意中发现题解中有一种方法可以O(N)解决这道题,就去膜拜了一下. 这个方法是,假如对于某一种颜色,将所有这种颜色的点全部删去 ...

随机推荐

  1. Noip 模拟题 T2 数字对

    2.数字对 [题目描述] 小H是个善于思考的学生,现在她又在思考一个有关序列的问题. 她的面前浮现出一个长度为n的序列{ai},她想找出一段区间[L, R](1 <= L <= R < ...

  2. bzoj2733永无乡

    永无乡 HYSBZ - 2733 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接, ...

  3. Django常见命令

    在Django的使用过程中需要使用命令让Django进行一些操作,例如创建Django项目,启动Django程序,创建新的APP,数据库迁移等. 1. 创建Django项目 新建一个文件夹来存放项目文 ...

  4. AtCoder AGC001E BBQ Hard (DP、组合计数)

    题目链接: https://atcoder.jp/contests/agc001/tasks/agc001_e 题解: 求\(\sum^n_{i=1}\sum^n_{j=i+1} {A_i+A_j+B ...

  5. vscode 遇到 TabError: inconsistent use of tabs and spaces in indentation

    Python开发,全靠缩进来控制Scope.缩进搞错了,代码也就有问题了.所以写着代码的时候,总是会遇到一个非常常见的问题.TabError: inconsistent use of tabs and ...

  6. redis基础操作概念等笔记

    Redis常用配置 daemonize ->是否是后台进程 port ->对外端口 logfile ->Redis 系统日志 dir ->Redis 工作目录 Redis的链接 ...

  7. 提交本地文件至gitlab已有的项目中(更新gitlab)

    gitlab代码更新 gitlab官网 1.安装git git官网 官网下载安装,安装过程一直next即可(路径自己选) 2.clone至本机 格式:git clone url(可转到指定目录克隆) ...

  8. 常用css模板

    段落超出显示省略号(可单行多行) .p-content{ overflow:hidden; text-overflow:ellipsis; display:-webkit-box; -webkit-b ...

  9. LeetCode 215. 数组中的第K个最大元素(Kth Largest Element in an Array)

    题目描述 在未排序的数组中找到第 k 个最大的元素.请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素. 示例 1: 输入: [3,2,1,5,6,4] 和 k = 2 ...

  10. pycharm中模块不能导入的问题

    在pycharm中发现模块老是导入不成功 只能以这样的映射的方式 现在才知道: 模块的标志符可以由字母.数字.下划线组成,但是, 不能以数字开头,如果在给python文件起名时,以数字开头是无法在py ...