【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 ...
随机推荐
- 编写可维护的JavaScript 收纳架
如果你看过Nicolas C.Zakas写过的任何作品,你必须承认他是个不折不扣的天才.也只有天才级的才能写出<JavaScript高级程序设计>让所有的前端攻城师人手一本.Nicolas ...
- java利用递归实现汉诺塔算法
package 汉诺塔; //引入Scanner包,用于用户输入 import java.util.Scanner; public class 汉诺塔算法 { public static void m ...
- 2018.09.14 codechef Milestone(随机化算法)
传送门 由于存在不超过7条直线可以覆盖超过所有的点. 所以如果我们随机选点的话(每次随机两个) 那么得到的解恰好为最优解的概率是149" role="presentation&qu ...
- schwarz( 施瓦兹)不等式证明
证明 如果: 函数 y=ax^2+2bx+c 对任意x >=0 时 y>=0; 函数图象在全部x轴上方,故二次方程判别式 b^2-4ac<=0;(即方程无实数解) 即(2b)^2&l ...
- 【Unity】2.1 初识Unity编辑器
分类:Unity.C#.VS2015 创建日期:2016-03-26 一.简介 本节要点:了解Unity编辑器的菜单和视图界面,以及最基本的操作,这是入门的最基础部分,必须掌握. 二.启动界面 双击桌 ...
- IntelliJ IDEA 2017版 spring-boot修改端口号配置把端口号改为8081
1.修改端口号主要是通过配置文件修改.如图: 完整版配置 ######################################################## ###server 配置信息 ...
- IntelliJ IDEA 2017版 spring-boot使用Spring Data JPA使用Repository<T, T>编程
1.环境搭建pom.xml搭建 <?xml version="1.0" encoding="UTF-8"?> <project xmlns=& ...
- Vivado 常见报错
1.[Synth 8-2543] port connections cannot be mixed ordered and named 说明例化时最后一个信号添加了一个逗号. 2. 原因:报告说明有一 ...
- cxf maven依赖
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-front ...
- angular2+ 初理解
一.Angular Module 1.angular 模块是一个类,它需要NgModule这个装饰器函数接受一个原数据对象作为参数来描述这个模块类属性. 其中最重要的属性有: ...