地址: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. SHGetSpecialFolderPath用法

    The SHGetSpecialFolderPath function retrieves the path of a special folder that is identified by its ...

  2. iOS开发之--去除按钮的点击效果

    Button.adjustsImageWhenHighlighted = NO; 去除按钮的点击效果,用这句代码就可以了!

  3. 部署全局ajax处理

    $.ajaxSetup({ beforeSend:function(){ $('.loading').show(); }, complete:function(){ $('.loading').fad ...

  4. phpcms v9表单实现问答咨询功能

    本文转自别人 phpcms v9的留言板插件可以安装留言板,做问答咨询,那样的话有很多东西需要修改,也有人发现phpcms v9有个表单向导功能,只能留言,不能回复,今天仿站网:新源网络工作室告诉大家 ...

  5. PHP函数addslashes和mysql_real_escape_string的区别

    转自:http://www.jb51.net/article/49205.htm   这篇文章主要介绍了PHP函数addslashes和mysql_real_escape_string的区别,以及一个 ...

  6. 随笔 javascript-抽象工厂模式

    随笔   javascript-抽象工厂模式 抽象工厂模式笔记   1.抽象工厂模式创建多个抽象类,创建出的结果是一个类簇(这里是抽象类的集合)   2.抽象工厂中传入的父类是否是抽象工厂方法创建的抽 ...

  7. Spark源码分析 -- TaskScheduler

    Spark在设计上将DAGScheduler和TaskScheduler完全解耦合, 所以在资源管理和task调度上可以有更多的方案 现在支持, LocalSheduler, ClusterSched ...

  8. 多线程入门-第五章-线程的调度与控制之yield

    yield与sleep类似,只是不能指定暂停多长时间,并且只能让同优先级的线程有执行的机会,让位时间不固定. /* yield使用 */ public class ThreadTest04 { pub ...

  9. Apache 配置ArcGIS server/portal 反向代理

    背景 处于安全,负载均衡,访问加速等原因会对服务器启用反向代理.arcgis for server的默认的访问地址为http://server:6080/arcgis.这个时候端口和实例名都暴露了.可 ...

  10. 原!linux脚本 expect命令 完成 输入密码交互 进行scp远程文件拷贝

    1.安装expect yum install  expect expect相关知识--- https://blog.csdn.net/lufeisan/article/details/53488395 ...