【多校训练】hdu 6035 Colorful Tree 树状数组
来源:互联网 发布:java素数的判断 编辑:程序博客网 时间:2024/05/16 05:05
Colorful Tree
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2100 Accepted Submission(s): 892
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 hasn(n−1)2 paths in total.
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
Input
The input contains multiple test cases.
For each test case, the first line contains one positive integersn , indicating the number of node. (2≤n≤200000)
Next line containsn integers where the i -th integer represents ci , the color of node i . (1≤ci≤n)
Each of the nextn−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.
For each test case, the first line contains one positive integers
Next line contains
Each of the next
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
31 2 11 22 361 2 1 3 2 11 21 32 42 53 6
Sample Output
Case #1: 6Case #2: 29
题意:
给出一棵树,每个点一个颜色,定义路径长度为路径上的不同颜色数,求树上所有路径长度和。
思路:
最直观的想法是求每条路径长度相加,但这样需要求出每条路径的长度,复杂度太高
-> 转换一下思路,可以求每种颜色对总长度的贡献,但是实现起来也比较困难
-> 正着解比较难时,我们可以倒过来想一下,假设每种颜色对总长度的贡献都是全体路径(n*(n-1)/2),再减去实际上多统计的就为答案
-> 怎么求解多统计的贡献呢?如果对于一种颜色,其相邻的点中都没有这个颜色,那么这些相邻点构成的连通块中的路径都为多统计的贡献
->需要用树状数组来计算多余的贡献,代码中还用到了很多技巧,具体实现见代码。
//// main.cpp// 1003//// Created by zc on 2017/8/2.// Copyright © 2017年 zc. All rights reserved.//#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>#define ll long longusing namespace std;const int N=550000;vector<int>r[N];int n,sz[N],v[N];ll c[N],ans,sum[N];ll dfs(int t,int fa){ sz[t]=1; for(int i=0;i<r[t].size();i++) { int u=r[t][i]; if(u==fa) continue; ll last=sum[c[t]];//保存没计算没计算子树之前,以c[t]为根的子树的总大小 sz[t]+=dfs(u,t); ll add=sum[c[t]]-last;//add=计算子树后的sum-没计算前的sum=该子树拥有的以c[t]颜色为根的子树的总大小啊 ans+=(sz[u]-add)*(sz[u]-add-1ll)/2ll;//sz[u]-add=该子树中没有c[t]颜色的连通块大小 sum[c[t]]+=sz[u]-add;//sum加上连通块 } sum[c[t]]++;//加上自己 return sz[t];}int main(int argc, const char * argv[]) { int kase=0; while(~scanf("%d",&n)) { int m=0; memset(v,0,sizeof(v)); for(int i=1;i<=n;i++) { scanf("%lld",&c[i]); r[i].clear(); if(v[c[i]]==0) v[c[i]]=1,m++; } for(int i=0;i<n-1;i++) { int u1,u2; scanf("%d%d",&u1,&u2); r[u1].push_back(u2); r[u2].push_back(u1); } ans=0; memset(sum,0,sizeof(sum)); dfs(1,-1); for(int i=1;i<=n;i++)//需要加上整棵树作为子树统计的各种颜色的连通块 if(v[i]) ans+=(n-sum[i])*(n-sum[i]-1ll)/2ll; printf("Case #%d: %lld\n",++kase,(ll)m*n*(n-1ll)/2ll-ans); }}
阅读全文
0 0
- 【多校训练】hdu 6035 Colorful Tree 树状数组
- 2017 多校训练第一场 HDU 6035 Colorful Tree
- HDU-2017 多校训练赛1-1003-Colorful Tree
- 2017多校训练赛第一场 HDU 6035 Colorful Tree (dfs+正序统计)
- hdu 6035 Colorful Tree 树状dp
- 2016多校训练#5 1012 HDU 5792 树状数组 代码详解
- (HDU 5775)Bubble Sort <树状数组> 多校训练4
- (HDU 5792)World is Exploding <树状数组+去重> 多校训练5
- (树状数组)多校训练7 Segment Game
- 多校训练-Bubble Sort(树状数组+离散化)
- hdu 6035 Colorful Tree
- HDU 6035 colorful tree
- [HDU]-6035 Colorful Tree
- hdu 6035 Colorful Tree
- hdu--6035--Colorful Tree
- hdu 6035 Colorful Tree
- HDU 6035 Colorful Tree
- hdu 6035 Colorful Tree
- iTween基础之Look(使对象面朝指定位置)
- Perlin noise(一)
- HBase 1.x Coprocessor使用指南
- <C#>_应用程序与MySQL Server数据库的链接
- JS代码在页面的什么位置插入合适(提高加载速率)
- 【多校训练】hdu 6035 Colorful Tree 树状数组
- OpenCV学习之形态学操作
- ssh整合的配置
- iTween基础之Scale(缩放大小)
- Spark学习之键值对(pair RDD)操作
- Java环境变量配置&解决版本不一致问题
- HTML5_京东首页样式
- mysql
- 删除kfaka topic