题意:有一颗数,每个点有一个颜色,定义两点之间的距离为两点路径之间不同颜色的数目,问所有路径的距离和是多少?

思路:每个颜色的贡献为路径中有这个颜色的路径数。先假设所有路径都会经过一种颜色,再减去不会经过这个颜色的路径数就是这个颜色的贡献。

看一下这个博客会好理解一些吧:https://blog.csdn.net/f_zyj/article/details/76168180

代码:

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int maxn = 200010;
vector<int> G[maxn];
int c[maxn];
LL ans[maxn * 2], res;
int s[maxn];
int n;
void add(int x, int y) {
G[x].push_back(y);
G[y].push_back(x);
}
LL dfs(int x, int fa) {
LL sz = 1, tmp = s[c[x]];
s[c[x]] = x;
for (int i = 0; i < G[x].size(); i++) {
int y = G[x][i];
if(y == fa) continue;
ans[x] = 0;
LL sz1 = dfs(y, x);
res -= (LL)(sz1 - ans[x]) * (sz1 - ans[x] - 1) / 2;
sz += sz1;
}
if(tmp <= n) ans[tmp] += sz;
else ans[tmp] -= sz;
s[c[x]] = tmp;
return sz;
}
set<int> st;
set<int>::iterator it;
int main() {
int x, y, kase = 0;
while(~scanf("%d", &n)) {
st.clear();
res = 0;
for (int i = 1; i <= n; i++) {
scanf("%d", &c[i]);
G[i].clear();
s[i] = 0;
st.insert(c[i]);
ans[i] = 0;
}
for (it = st.begin(); it != st.end(); it++) {
ans[(*it) + n] = n;
s[(*it)] = (n + (*it));
}
for (int i = 1; i < n; i++) {
scanf("%d%d", &x, &y);
add(x, y);
}
res = (LL) st.size() * n * (n - 1) / 2;
dfs(1, -1);
for (it = st.begin(); it != st.end(); it++) {
res -= ans[(*it) + n] * (ans[(*it) + n] - 1) / 2;
}
printf("Case #%d: %lld\n", ++kase, res);
}
}

  

gym/102253C Colorful Tree 树上计数的更多相关文章

  1. 2017 Multi-University Training Contest - Team 1 1003&&HDU 6035 Colorful Tree【树形dp】

    Colorful Tree Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)T ...

  2. hdu6035 Colorful Tree 树形dp 给定一棵树,每个节点有一个颜色值。定义每条路径的值为经过的节点的不同颜色数。求所有路径的值和。

    /** 题目:hdu6035 Colorful Tree 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6035 题意:给定一棵树,每个节点有一个颜色值.定 ...

  3. [HDU6793] Tokitsukaze and Colorful Tree

    题目 又是一个条历新年,窗前的灼之花又盛开了. 时隔多年,现在只有这一棵树上盛开着残存的 \(n\) 朵灼之花了. 尽管如此,这些灼之 花仍散发出不同色彩的微弱的光芒. 灼之花的生命极为短暂,但它的花 ...

  4. BZOJ 3221: [Codechef FEB13] Obserbing the tree树上询问( 可持久化线段树 + 树链剖分 )

    树链剖分+可持久化线段树....这个一眼可以看出来, 因为可持久化所以写了标记永久化(否则就是区间修改的线段树的持久化..不会), 结果就写挂了, T得飞起...和管理员拿数据调后才发现= = 做法: ...

  5. [BZOJ 3221][Codechef FEB13] Obserbing the tree树上询问

    [BZOJ 3221]Obserbing the tree树上询问 题目 小N最近在做关于树的题.今天她想了这样一道题,给定一棵N个节点的树,节点按1~N编号,一开始每个节点上的权值都是0,接下来有M ...

  6. Count on a tree 树上主席树

    Count on a tree 树上主席树 给\(n\)个树,每个点有点权,每次询问\(u,v\)路径上第\(k\)小点权,强制在线 求解区间静态第\(k\)小即用主席树. 树上主席树类似于区间上主席 ...

  7. AtCoder Beginner Contest 133 F Colorful Tree

    Colorful Tree 思路: 如果强制在线的化可以用树链剖分. 但这道题不强制在线,那么就可以将询问进行差分,最后dfs时再计算每个答案的修改值, 只要维护两个数组就可以了,分别表示根节点到当前 ...

  8. HDU 6035 - Colorful Tree | 2017 Multi-University Training Contest 1

    /* HDU 6035 - Colorful Tree [ DFS,分块 ] 题意: n个节点的树,每个节点有一种颜色(1~n),一条路径的权值是这条路上不同的颜色的数量,问所有路径(n*(n-1)/ ...

  9. CF629E Famil Door and Roads【树上计数+分类讨论】

    Online Judge:Codeforces629E,Luogu-CF629E Label:树上计数,分类讨论,换根 题目描述 给出一棵n个节点的树.有m个询问,每一个询问包含两个数a.b,我们可以 ...

随机推荐

  1. java static在成员方法中

    package java08; public class Myclass { int num;//成员变量 static int numStatic;//静态变量 //成员方法 public void ...

  2. spring boot 提纲

    http://tengj.top/categories/Spring-Boot%E5%B9%B2%E8%B4%A7%E7%B3%BB%E5%88%97/ http://blog.csdn.net/ca ...

  3. poj 3258:River Hopscotch(二分)

    题目链接 L为N+2块石子中最右边石子位置,0最左,M为可移除块数,求移除后相邻石子可达到的最大距离. #include<iostream> #include<cstdio> ...

  4. css linear-gradient;心跳animation

    css线性背景 background:linear-gradient(20deg,#ccffff,#ffcccc); transform transform:scale(1.5); transform ...

  5. 纯JSP简单登录实例

    记一下,免得以后忘记了,又要去查. 文件共有四个web.xml.login.jsp.logout.jsp.welcome.jsp四个文件 测试环境:Tomcat 6.0.x 假设项目名称是LoginS ...

  6. 4412 GPIO读 和 ioremap控制GPIO寄存器

    一.配置GPIO读 在视频14的基础上做 1.利用拨码开关来实现GPIO输入 所以AP_SLEEP对应GPC0_3,然后在drivers/gpio/gpio-exynos4.c中对应EXYNOS4_G ...

  7. 【BZOJ2946&SPOJ1812】公共串(后缀自动机)

    题意:给出几个由小写字母构成的单词,求它们最长的公共子串的长度. 单词的数量<=5,单词的长度至少为1,最大为2000. 思路: #include<bits/stdc++.h> us ...

  8. 安卓Activity布局简述

    Activity布局简述 基于xml的布局 Main.xml(调用工程res/layout/main.xml定义的界面元素完成布局显示) <?xml version="1.0" ...

  9. windows防火墙批量开放端口

    windows自带的防火墙没有增加端口范围的功能,这让一些常见的配置变得非常麻烦(比如FTP被动模式下,就需要在防火墙上开启端口范围),经过查询资料,使用以下cmd批处理内容,即可增加端口范围: ec ...

  10. winserver安装redis

    原文: https://www.cnblogs.com/xuzhiwei/p/4569315.html 1.Redis本身不支持windows,但是有另外的团队在维护着一个windows下的版本   ...