题目链接

Problem Description
There is a tree with n nodes, each of which has a type of color represented by an integer, where the color of node i is ci.

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.

 
Input
The input contains multiple test cases.

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.

 
Output
For each test case, output "Case #x: y" in one line (without quotes), where x indicates the case number starting from 1 and y denotes the answer of corresponding case.
 
Sample Input
3
1 2 1
1 2
2 3
6
1 2 1 3 2 1
1 2
1 3
2 4
2 5
3 6
 
 
Sample Output
Case #1: 6
Case #2: 29
 
题意:有一棵n个节点的树,编号从 1 到 n ,每个节点涂有一种颜色ci (1<=ci<=n),对于树上的每条路径有个值,表示这条路径上的颜色种类数,现在对树上所有的路径值求和。
 
思路:可以转换一下题目描述,即求包含颜色ci的路径数,然后对所有颜色求和即可。这样求依然不好计算,可以反向考虑, 包含颜色ci的路径数=n*(n-1)/2 - 不包含颜色ci的路径数,求不包含颜色ci的路径数 即为颜色ci将整棵树分成一个个的联通块,对每个联通块,如果包含节点数为x,即为C(x,2),对所有联通块求和;
 
代码如下:
#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)的更多相关文章

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

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

  2. 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 ...

  3. hdu 6035:Colorful Tree (2017 多校第一场 1003) 【树形dp】

    题目链接 单独考虑每一种颜色,答案就是对于每种颜色至少经过一次这种的路径条数之和.反过来思考只需要求有多少条路径没有经过这种颜色即可. 具体实现过程比较复杂,很神奇的一个树形dp,下面给出一个含较详细 ...

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

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

  5. 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 ...

  6. HDU-6035 Colorful Tree(树形DP) 2017多校第一场

    题意:给出一棵树,树上的每个节点都有一个颜色,定义一种值为两点之间路径中不同颜色的个数,然后一棵树有n*(n-1)/2条 路径,求所有的路径的值加起来是多少. 思路:比赛的时候感觉是树形DP,但是脑袋 ...

  7. HDU 6035 Colorful Tree(补集思想+树形DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6035 [题目大意] 给出一颗树,一条路径的价值为其上点权的种类数,求路径总价值 [题解] 单独考虑 ...

  8. HDU 6035 Colorful Tree (树形DP)

    [题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6035 [题目大意] 给出一颗树,一条路径的价值为其上点权的种类数,求路径总价值 [题解] 我们计算 ...

  9. HDU 6035 Colorful Tree(dfs)

    题意:一棵有n个点的树,树上每个点都有颜色c[i],定义每条路径的值为这条路径上经过的不同颜色数量和.求所有路径的值的和. 可以把问题转化为对每种颜色有多少条不同的路径至少经过这种颜色的点,然后加和. ...

  10. hdu 6035 Colorful Tree(虚树)

    考虑到树上操作:首先题目要我们求每条路径上出现不同颜色的数量,并把所有加起来得到答案:我们知道俩俩点之间会形成一条路径,所以我们可以知道每个样例的总的路径的数目为:n*(n-1)/2: 这样单单的求, ...

随机推荐

  1. vue2.0 配置 选项 属性 方法 事件 ——速查

    全局配置 silent  设置日志与警告  optionMergeStrategies   合并策略  devtools  配置是否允许vue-devtools  errorHandler    错误 ...

  2. 不再迷惑,无值和NULL值的转换

    在关系型数据库的世界中,无值和NULL值的区别是什么?一直被这个问题困扰着,甚至在写TSQL脚本时,心有戚戚焉,害怕因为自己的一知半解,挖了坑,贻害后来人,于是,本着上下求索,不达通幽不罢休的决心(开 ...

  3. python 标准库 -- signal

    signal 的核心是 : 设置信号处理函数. 预定义信号 signal.SIG_DFL signal.SIGBUS signal.SIGFPE signal.SIGIO signal.SIGPOLL ...

  4. 开发抓包工具 Mac charles 3.11.5 破解版 安装包

    摘要 在发开过程中,追踪请求和监控请求与返回数据是我们经常会需要的一个需求,在Mac端,Charles是一款非常易用的抓包工具. 一.简介 Charles是Mac端的一款截取与分析网络请求的工具,在网 ...

  5. v9 调用模型中新增的字段

    在模型中新增字段的时候,可以选择“是否为主表”. 若选是,则前台调用可直接通过字段名调用. 若选否,在前台调用是应在{pc:content}中添加 moreinfo="1",表示允 ...

  6. 几种MQ消息队列对比与消息队列之间的通信问题

    消息队列 开发语言 协议支持 设计模式 持久化支持 事务支持 负载均衡支持 功能特点 缺点 RabbitMQ Erlang AMQP,XMPP,SMTP,STOMP 代理(Broker)模式(消息在发 ...

  7. 【LeetCode】60. Permutation Sequence

    题目: The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of t ...

  8. XMLHttpRequest函数封装

    XMLHttpRequest函数封装: function ajax(Url,sccuessFn,failureFn) { //1.创建XMLHttpRequest对象 var xhr = null; ...

  9. Latex加速: TexStudio的安装和使用

    TexStudio可以加速Latex的编辑和写作,这里简单介绍一下TexStudio的安装,配置和使用.但是有一个重要的前提,TexStudio会使Latex源代码和pdf文件并排显示,比较占用桌面的 ...

  10. 自己编写的 C++ 超轻量级日志类

    [自己编写的 C++ 超轻量级日志类(兼容vc++6.0.vs2010.vs2015)] 先来看效果: [测试文件:test.cpp] /* 作者:闫文山 时间:2017/07/02 介绍: 本日志类 ...