地址: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

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
 
Source
 

思路:很巧妙的树形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的更多相关文章

  1. 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) ...

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

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

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

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

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

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

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

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

随机推荐

  1. oracle和SQLserver数据库中select into 的区别

    在Oracle中是这样的 在SQLserver中是这样的

  2. ip地址查询系统和CMD查询的结果不一样

    由于cmd输入 ipconfig查看的IP是局域网内网IP,而用ip地址查看器查看是公网上网的ip地址.所以不一样. 查询内网ip: windows系统: 开始--运行--cmd,命令行输入: ipc ...

  3. 让所有IE支持HTML5的解决方案

    自从HTML5能为我们的新网页带来更高效洁净的代码而得到更多的关注,然而唯一能让IE识别那些新元素(如<article>)的途径是使用HTML5 shiv,感谢remy sharp为我们提 ...

  4. Oracle sqlldr命令

    今天别人的入库代码,看的真有点晕,最后看完才知道是用了sqlldr命令.哎...还是学艺不精啊,今后还是要多努力. 总结哈sqlldr命令:虽然大多是网上来的,自己要有体会嘛 !开源就是好啊. sql ...

  5. Java四种会话跟踪技术

    1.cookie:2.session:3.隐藏表单域:4.URL重写

  6. c++ const(不断跟新)

    1.把一个 const 对象的地址赋给一个普通的.非 const 对象的指针也会导致编译时的错误: const double pi = 3.14; double *ptr = π // error: ...

  7. poj_3415 后缀数组+单调栈

    题目大意 定义字符串T的子串T(i, k)=TiTi+1...Ti+k-1, 1≤i≤i+k-1≤|T|. 给定两个字符串A和B,定义集合S为S = {(i, j, k) | k≥K, A(i, k) ...

  8. TFS 创建团队成员及管理

    用TFS服务器管理代码 1:创建TFS服务器 2:创建TFS项目 3:创建TFS团队成员 4:上传代码,共享链接,对代码进行管理 这里主要介绍第三步:创建TFS团队成员 第一种:通过连接进入TFS可视 ...

  9. However, a general purpose protocol or its implementation sometimes does not scale very well.

    https://netty.io/wiki/user-guide-for-4.x.html The Problem Nowadays we use general purpose applicatio ...

  10. Enables DNS lookups on client IP addresses

    w虚拟域名访问,路由可以到达,但无输出. http://httpd.apache.org/docs/2.2/mod/core.html#hostnamelookups