2017 Multi-University Training Contest - Team 1 03Colorful Tree
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6035
题面:
Colorful Tree
Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1651    Accepted Submission(s): 675
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.
1 2 1
1 2
2 3
6
1 2 1 3 2 1
1 2
1 3
2 4
2 5
3 6
Case #2: 29
思路:很巧妙的树形dp!
具体看这个博客把,我也是看了这个才懂的http://blog.csdn.net/Bahuia/article/details/76141574
#include <bits/stdc++.h> using namespace std; #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=1e6+;
const int mod=1e9+; vector<int>mp[K];
LL n,cnt,ans,sz[K],col[K],vis[K],sum[K]; void dfs(int x,int f)
{
LL tmp,add,all=;
sz[x]=;
for(auto v:mp[x])
if(v!=f)
{
tmp=sum[col[x]];
dfs(v,x);
sz[x]+=sz[v];
add=sum[col[x]]-tmp;
ans+=(sz[v]-add)*(sz[v]-add-)/;
all+=sz[v]-add;
}
sum[col[x]]+=all+;
} int main(void)
{
int cs=;
while(~scanf("%lld",&n))
{
cnt=,ans=;
memset(mp,,sizeof mp);
memset(sum,,sizeof sum);
memset(sz,,sizeof sz);
for(int i=;i<=n;i++) scanf("%d",col+i),vis[col[i]]=;
for(int i=,x,y;i<n;i++) scanf("%d%d",&x,&y),mp[x].PB(y),mp[y].PB(x);
dfs(,);
for(int i=;i<=n;i++)
if(vis[i])
ans+=(n-sum[i]-)*(n-sum[i])/,vis[i]=,cnt++;
printf("Case #%d: %lld\n",cs++,cnt*n*(n-1LL)/2LL-ans);
}
return ;
}
2017 Multi-University Training Contest - Team 1 03Colorful Tree的更多相关文章
- 2017 Multi-University Training Contest - Team 9 1005&&HDU 6165 FFF at Valentine【强联通缩点+拓扑排序】
		FFF at Valentine Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ... 
- 2017 Multi-University Training Contest - Team 9 1004&&HDU 6164 Dying Light【数学+模拟】
		Dying Light Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Tot ... 
- 2017 Multi-University Training Contest - Team 9 1003&&HDU 6163 CSGO【计算几何】
		CSGO Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Subm ... 
- 2017 Multi-University Training Contest - Team 9 1002&&HDU 6162 Ch’s gift【树链部分+线段树】
		Ch’s gift Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ... 
- 2017 Multi-University Training Contest - Team 9 1001&&HDU 6161 Big binary tree【树形dp+hash】
		Big binary tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ... 
- 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 ... 
- 2017 Multi-University Training Contest - Team 1 1006&&HDU 6038 Function【DFS+数论】
		Function Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ... 
- 2017 Multi-University Training Contest - Team 1 1002&&HDU 6034 Balala Power!【字符串,贪心+排序】
		Balala Power! Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ... 
- 2017 Multi-University Training Contest - Team 1 1011&&HDU 6043 KazaQ's Socks【规律题,数学,水】
		KazaQ's Socks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ... 
随机推荐
- STL容器:deque双端队列学习
			所谓deque,是"double-ended queue"的缩写; 它是一种动态数组形式,可以向两端发展,在尾部和头部插入元素非常迅速; 在中间插入元素比较费时,因为需要移动其它元 ... 
- Spring_day01--Spring的bean管理(xml方式)_属性注入介绍
			Spring的bean管理(xml方式) Bean实例化的方式 1 在spring里面通过配置文件 创建对象 2 bean实例化(创建对象)三种方式实现 第一种 使用类的无参数构造创建(重点) Use ... 
- uilabel 和uitextview 自适应大小
			本文转载至 http://blog.csdn.net/liulichao20/article/details/8957752 分类: ios2013-05-21 22:06 321人阅读 评论(0) ... 
- 盘点:七大.NET著名开源项目
			尽管过了相当长的时间,花费了不少资源,微软和.NET社区还是在最近几年加入到了开源运动的阵营中来了,这令人相当惊讶,因为两大阵营一直都是经常对立的.然而,事实是依靠开源,微软社区中的开源开发工具日益发 ... 
- spring基础---->spring自定义标签(一)
			Spring具有一个基于架构的扩展机制,可以使用xml文件定义和配置bean.本博客将介绍如何编写自定义XML bean的解析器,并用实例来加以说明.其实我一直相信 等你出现的时候我就知道是你. Sp ... 
- JZOJ.5326【NOIP2017模拟8.21】LCA 的统计
			Description 
- 比较好用的Opera 翻译工具  ddict
			http://ddict.me 
- [Domino]执行命令load design的时候出现Warning: Cannot locate design template
			发现问题 在做Pseudo Test的时候被QA测出了一个问题.在Domino concole打命令“load design”,被Workbench翻译过后的数据库提示了一个警告:Warning: C ... 
- 用wamp实现前端和php的交互效果
			我们今天来用php来做一下前台与后台的交互效果,首先我们先打开这个软件. 看一下电脑右下角的小图标 当变成之后鼠标左键 打开这个之后点击第二个之后会打开一个网站 点击右面页面的数据库打开新建数据库,填 ... 
- jQuery UI dialog 参数说明
			前段时间碰到个问题 jquery UI dialog弹出层 弹出多个层是 比如弹出两个层A和B B层如果显示的数据表格太大,伸到了A层的外面,那伸到A层之外的部分就看不到了,因为B层是在A层上弹出的 ... 
