HDU 6035---Colorful Tree(树形DP)
The path between each two different nodes is unique, of which we define the value as the number of different colors appearing in it.
Calculate the sum of values of all paths on the tree that has n(n−1)2 paths in total.
For each test case, the first line contains one positive integers n, indicating the number of node. (2≤n≤200000)
Next line contains n integers where the i-th integer represents ci, the color of node i. (1≤ci≤n)
Each of the next n−1 lines contains two positive integers x,y (1≤x,y≤n,x≠y), meaning an edge between node x and node y.
It is guaranteed that these edges form a tree.
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
typedef long long LL;
const int N=;
int c[N],sum[N],sz[N];
vector<int> t[N];
LL ans; int dfs(int u,int pa)
{
sz[u]=;
int cn=t[u].size();
for(int i=;i<cn;i++)
{
int v=t[u][i];
if(v==pa) continue;
int r=sum[c[u]];
sz[u]+=dfs(v,u);
int o=sum[c[u]]-r;
ans+=(LL)(sz[v]-o)*(LL)(sz[v]-o-)/;
sum[c[u]]+=sz[v]-o;
}
sum[c[u]]++;
return sz[u];
} int main()
{
///cout << "Hello world!" << endl;
int n,Case=1;
while(scanf("%d",&n)!=EOF)
{
memset(sum,,sizeof(sum));
for(int i=;i<=n;i++){
scanf("%d",&c[i]);
t[i].clear();
}
for(int i=;i<n;i++)
{
int u,v; scanf("%d%d",&u,&v);
t[u].push_back(v);
t[v].push_back(u);
}
ans=;
dfs(,-);
for(int i=;i<=n;i++)
{
ans+=(LL)(n-sum[i])*(LL)(n-sum[i]-)/;
}
ans=(LL)n*(LL)(n-)*(LL)n/-ans;
printf("Case #%d: %lld\n",Case++,ans);
}
return ;
}
HDU 6035---Colorful Tree(树形DP)的更多相关文章
- HDU 6035 - Colorful Tree | 2017 Multi-University Training Contest 1
/* HDU 6035 - Colorful Tree [ DFS,分块 ] 题意: n个节点的树,每个节点有一种颜色(1~n),一条路径的权值是这条路上不同的颜色的数量,问所有路径(n*(n-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 ...
- hdu 6035:Colorful Tree (2017 多校第一场 1003) 【树形dp】
题目链接 单独考虑每一种颜色,答案就是对于每种颜色至少经过一次这种的路径条数之和.反过来思考只需要求有多少条路径没有经过这种颜色即可. 具体实现过程比较复杂,很神奇的一个树形dp,下面给出一个含较详细 ...
- hdu6035 Colorful Tree 树形dp 给定一棵树,每个节点有一个颜色值。定义每条路径的值为经过的节点的不同颜色数。求所有路径的值和。
/** 题目:hdu6035 Colorful Tree 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6035 题意:给定一棵树,每个节点有一个颜色值.定 ...
- 2017ACM暑期多校联合训练 - Team 1 1003 HDU 6035 Colorful Tree (dfs)
题目链接 Problem Description There is a tree with n nodes, each of which has a type of color represented ...
- HDU-6035 Colorful Tree(树形DP) 2017多校第一场
题意:给出一棵树,树上的每个节点都有一个颜色,定义一种值为两点之间路径中不同颜色的个数,然后一棵树有n*(n-1)/2条 路径,求所有的路径的值加起来是多少. 思路:比赛的时候感觉是树形DP,但是脑袋 ...
- HDU 6035 Colorful Tree(补集思想+树形DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6035 [题目大意] 给出一颗树,一条路径的价值为其上点权的种类数,求路径总价值 [题解] 单独考虑 ...
- HDU 6035 Colorful Tree (树形DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6035 [题目大意] 给出一颗树,一条路径的价值为其上点权的种类数,求路径总价值 [题解] 我们计算 ...
- HDU 6035 Colorful Tree(dfs)
题意:一棵有n个点的树,树上每个点都有颜色c[i],定义每条路径的值为这条路径上经过的不同颜色数量和.求所有路径的值的和. 可以把问题转化为对每种颜色有多少条不同的路径至少经过这种颜色的点,然后加和. ...
- hdu 6035 Colorful Tree(虚树)
考虑到树上操作:首先题目要我们求每条路径上出现不同颜色的数量,并把所有加起来得到答案:我们知道俩俩点之间会形成一条路径,所以我们可以知道每个样例的总的路径的数目为:n*(n-1)/2: 这样单单的求, ...
随机推荐
- asp.net core 教程(六)-中间件
Asp.Net Core-中间件 在这一章,我们将了解如何设置中间件.中间件技术在 ASP.NET Core中控制我们的应用程序如何响应 HTTP 请求.它还可以控制应用程序的异常错误,这是一个在如何 ...
- 微信小程序代开发
微信申请第三方之后可以获取授权方的很多权限,主要的是生码和待开发,生码的第三方授权之前已经写了一篇文章,最近做了小程序待开发,总结一下写下来供大家参考 注意事项:如果在调试过程中返回了错误码请到小程序 ...
- 双核 CPU
双核 CPU 时间限制: 1 Sec 内存限制: 128 MB 题目描述 由于越来越多的计算机配置了双核 CPU,TinySoft 公司的首席技术官员,SetagLilb,决定升级他们的产品-SWO ...
- Akka(10): 分布式运算:集群-Cluster
Akka-Cluster可以在一部物理机或一组网络连接的服务器上搭建部署.用Akka开发同一版本的分布式程序可以在任何硬件环境中运行,这样我们就可以确定以Akka分布式程序作为标准的编程方式了. 在上 ...
- IE11中navigator.userAgent的变化
在原来判断浏览器是否是ie时,我们可以根据navigator.userAgent中时候有MSIE,但是IE11进行变革,userAgent中不在包含MSIE字段, 在实际项目中,入到类似的在控制台报错 ...
- Linux 下开启ssh服务
在虚拟机里安装完后,想往虚拟机里拷贝软件 当使用Xmanager ftp连接时候 出现 连接不上的问题,其解决方案如下: 1.修改sshd_config文件,命令为:vim /etc/ssh/sshd ...
- 解决yii框架,gii脚手架不能使用。
应用场景 把代码转移到线上服务器时,GII.BUG工具不正常使用,但在本地服务器是正常的. 分析原因 Yii框架在使用GII 和BUG 时,会针对访问IP地址拦截,没有在配置中设置的IP地址是会默认被 ...
- 【Android Developers Training】 27. 序言:和其它应用交互
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- pouchdb-find( pouchdb查询扩展插件 ,便于查询)
pouchdb-find pouchdb-find 环境搭建 下载lib bower install pouchdb-find 引入js <script src="pouchdb.js ...
- 打包可执行的jar
#配置项目路径 *除程序文件以外,其他相关素材也可以打包进jar,但在内部访问时需以包名作为跟路径,如hello/xxx/yyy.zzz mkdir hello vi hello/HelloWorld ...