【hdu6035】 Colorful Tree dfs序
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6035
题目大意:给你一棵树,树上每个节点都有一个颜色。 现在定义两点间的距离为两点最短路径上颜色集合大小,求该树上所有点对的距离之和。其中树上的节点个数$≤2*10^5$
如果直接处理每一条路径上颜色集合大小,显然比较麻烦。我们不妨换一种思路。
我们用S_i表示经过颜色i的路径的数量,显然答案$=\sum S_i$。
考虑如何求S_i。我们先将所有颜色为i的节点全部找出来,按照dfs序排序。
显然,若树上所有的路径均经过该颜色的节点,则$S_i=\frac{n*(n-1)}{2}$。
对于该点集中的节点x的每一棵子树,不妨设当前子树的根节点为v,找出所有点集中满足$dfn[v]<dfn[u]≤low[v]$且不存在$y$,使得$dfn[v]<dfn[y]<dfn[u]≤low[v]$的所有的$u$,则显然有$\frac{(siz[v]-\sum siz[u]) \times (siz[v]-\sum siz[u]-1)}{2}$个点对不会对答案产生贡献。其中$siz[x]$表示以x为根的子树的节点个数。
该统计方法的时间复杂度为$O(n log n)$
#include<bits/stdc++.h>
#define L long long
#define M 200005
using namespace std;
struct edge{int u,next;}e[M*]={}; int head[M]={},use=;
void add(int x,int y){use++;e[use].u=y;e[use].next=head[x];head[x]=use;} int dfn[M]={},low[M]={},t=;
int siz[M]={},col[M]={}; void dfss(int x,int fa){
dfn[x]=++t; siz[x]=;
for(int i=head[x];i;i=e[i].next) if(e[i].u!=fa){
dfss(e[i].u,x);
siz[x]+=siz[e[i].u];
}
low[x]=t;
}
bool cmp(int x,int y){
if(col[x]==col[y]) return dfn[x]<dfn[y];
return col[x]<col[y];
}
int p[M]={};
L ans=,sum=,n; void dfs(int &x,int y){
int xx=p[x]; x++;
for(int i=head[xx];i;i=e[i].next)
if(dfn[xx]<dfn[e[i].u]){
int v=e[i].u;
L cnt=siz[v];
while(x<=y&&dfn[p[x]]<=low[v]){
cnt-=siz[p[x]];
dfs(x,y);
}
sum-=cnt*(cnt-);
}
}
int hh=;
int Main(){
hh++;
ans=; sum=; t=;
memset(head,,sizeof(head)); use=;
for(int i=;i<=n;i++) scanf("%d",col+i);
for(int i=;i<n;i++){
int x,y; scanf("%d%d",&x,&y);
add(x,y); add(y,x);
}
dfss(,); add(,);
for(int i=;i<=n;i++) p[i]=i;
sort(p+,p+n+,cmp);
for(int i=,j;i<=n;i=j+){
for(j=i;col[p[i]]==col[p[j]];j++); j--;
sum=n*(n-);
p[--i]=;
dfs(i,j);
ans+=sum;
}
printf("Case #%d: %lld\n",hh,ans/);
//cout<<ans/2<<endl;
} int main(){
freopen("in.txt","r",stdin);
while(cin>>n) Main();
}
【hdu6035】 Colorful Tree dfs序的更多相关文章
- hdu6035 Colorful Tree 树形dp 给定一棵树,每个节点有一个颜色值。定义每条路径的值为经过的节点的不同颜色数。求所有路径的值和。
/** 题目:hdu6035 Colorful Tree 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6035 题意:给定一棵树,每个节点有一个颜色值.定 ...
- POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和)
POJ.3321 Apple Tree ( DFS序 线段树 单点更新 区间求和) 题意分析 卡卡屋前有一株苹果树,每年秋天,树上长了许多苹果.卡卡很喜欢苹果.树上有N个节点,卡卡给他们编号1到N,根 ...
- poj 3321 Apple Tree dfs序+线段树
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Description There is an apple tree outsid ...
- [poj3321]Apple Tree(dfs序+树状数组)
Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 26762 Accepted: 7947 Descr ...
- Codeforces Round #225 (Div. 1) C. Propagating tree dfs序+树状数组
C. Propagating tree Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/383/p ...
- Codeforces Round #200 (Div. 1)D. Water Tree dfs序
D. Water Tree Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/343/problem/ ...
- POJ3321Apple Tree Dfs序 树状数组
出自——博客园-zhouzhendong ~去博客园看该题解~ 题目 POJ3321 Apple Tree 题意概括 有一颗01树,以结点1为树根,一开始所有的结点权值都是1,有两种操作: 1.改变其 ...
- [Split The Tree][dfs序+树状数组求区间数的种数]
Split The Tree 时间限制: 1 Sec 内存限制: 128 MB提交: 46 解决: 11[提交] [状态] [讨论版] [命题人:admin] 题目描述 You are given ...
- Codeforces Round #381 (Div. 2) D. Alyona and a tree dfs序+树状数组
D. Alyona and a tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
随机推荐
- jQuery DataTables插件分页允许输入页码跳转
背景说明 项目中使用jQuery DataTables插件来实现分页表格,但是默认的分页样式不能输入页码进行跳转,在页数非常多的时候使用很不方便,最主要的还是没有达到产品部门的设计要求,所以我需要寻找 ...
- Web Service测试工具小汇
1..NET WebService Studio 这款工具出自微软内部,最大的优点是可视化很好,不用去看那些XML文件,WebService的基础内容就有XML,但是测试中Case过多,每次测试结果都 ...
- Type mismatch in value from map: expected org.apache.hadoop.io.longWritable
hadoop 编译的程序的报错 执行了命令: hadoop fs -put HTTP_20130313143750.dat /date.dochadoop jar MyDataCount.jar co ...
- wadl 的自动生成(cxf版本3.1.1)
官方文档 http://cxf.apache.org/docs/jaxrs-services-description.html 举例: package cn.zno; import javax.ws. ...
- node API buffer
https://cnodejs.org/topic/5189ff4f63e9f8a54207f60c 1.拼接字符串时,String比buffer要快,buffer需要toString().当保存非u ...
- 手动设置3G的wifi迷你无线路由
1.插入中兴的3G无线网卡,终端上显示如下内容: ~ >: usb 1-1.3: new full speed USB device number 11 using s3c2410-ohci u ...
- window.open()用法说明
1.例子 : window.open("index.jsp","_self"); window.open()格式: window.open( [sURL] [, ...
- A Good Story for Great Friends
There once was a little girl who had a bad temper. Her mother gave her a bag of nails and told her t ...
- 团队项目第六周——Alpha阶段项目复审
排名 队名 项目名 优点 缺点 1 大猪蹄子队 四六级背单词游戏 功能开发出来了,界面简洁美观. 功能的确开发出来了,但是还未完成整个程序.不过考虑到开发时长问题,可以理解.页面还是比较简洁,但是测试 ...
- Ubuntu/Debian下通过Apt-get简单安装Oracle JDK
近几年本人对各种Arm小板,开发板不明原因中毒,基本以Linux系统为主,本篇文章以记录在32位Arm的Debian8上,通过Apt-get的简单命令安装Oracle JDK8并成功的记录. 1.首先 ...