地址: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. Java 执行linux scp 远程获取文件和上传

      需要的jar包:ganymed-ssh2-build210.jar import java.io.ByteArrayOutputStream;import java.io.File;import ...

  2. C++关键字之friend

    原则上, 类的私有(private)和受保护(protected)成员不能从声明它们的同一类外部访问.但是, 此规则不适用于友元 "friends". 以friend关键字修饰的函 ...

  3. 理解javascript函数调用和“this”

    http://blog.csdn.net/littlechang/article/details/8180550

  4. 剑指 offer set 19 翻转单词顺序 && 字符串左旋

    题目 1. 翻转单词 student. a am I 转换成 I am a student. 2. 字符串左旋 abba 1 转成 bbaa 总结 1. 先对每个单词旋转, 再整齐旋转 2. 先翻转一 ...

  5. mac 10.9 dock在多屏幕间移动

    想要在哪个屏幕使用dock,就在这个屏幕把鼠标移动到最底部即可.神奇吧?太意外了...居然被我发现了...

  6. Maven编译失败,提示No compiler is provided in this enviroment. Perhaps you are running on a JRE rathen a JDK ?

    用maven对项目进行构建时,提示No compiler is provided in this enviroment. Perhaps you are running on a JRE rathen ...

  7. 常用CGI环境变量 转自https://blog.csdn.net/acmdream/article/details/58070066

    CGI环境变量名称 说明 REQUEST_METHOD 请求类型,如“GET”或“POST” CONTENT_TYPE 被发送数据的类型 CONTENT_LENGTH 客户端向标准输入设备发送的数据长 ...

  8. 【BZOJ4155】[Ipsc2015]Humble Captains 最小割+DP

    [BZOJ4155][Ipsc2015]Humble Captains Description 每天下午放学时都有n个zky冲出教室去搞基.搞基的zky们分成两队,编号为1的zky是1号队的首领,编号 ...

  9. Hibernate更新数据报错:a different object with the same identifier value was already associated with the session: [com.elec.domain.ElecCommonMsg#297e35035c28c368015c28c3e6780001]

    使用hibernate更新数据时,报错 Struts has detected an unhandled exception: Messages: a different object with th ...

  10. SecureCRT图形界面

    一般的咱们用这个工具连接服务器啥的都是命令行模式的,其实他也可以连接图形界面 一.Xmanager SecureCRT连接图形界面的话必须要有 Xmanager 工具的配合才行,SecureCRT显示 ...