HDU 6035 Colorful Tree(树形DP)
来源:互联网 发布:java redis list 存取 编辑:程序博客网 时间:2024/05/19 18:41
Colorful Tree
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1903 Accepted Submission(s): 804
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
Source
2017 Multi-University Training Contest - Team 1
http://blog.csdn.net/Bahuia/article/details/76141574
这篇讲的比较好。
#include <iostream>#include <stdio.h>#include <string.h>using namespace std;#define LL long longconst LL N = 2e5+10;struct node{ LL v,nxt;}len[N<<1];LL sum[N],size[N];LL head[N],vis[N],col[N];LL cnt;LL lenn;void add(LL u,LL v){ lenn++; len[lenn].v=v; len[lenn].nxt=head[u]; head[u]=lenn;}LL ans;LL dfs(LL u,LL p){ LL allson=0,pre; size[u]=1; for(LL i=head[u];i!=-1;i=len[i].nxt) { LL v=len[i].v; if(v==p) continue; pre=sum[col[u]]; size[u]+=dfs(v,u); LL add=sum[col[u]]-pre; LL temp=(size[v]-add)*(size[v]-add-1LL)/2LL; ans+=temp; allson+=size[v]-add; } sum[col[u]]+=allson+1; return size[u];}int main(){ LL n; LL k=0; while(~scanf("%lld",&n)) { cnt=0; lenn=0; ans=0; memset(head,-1,sizeof head); memset(vis,0,sizeof vis); memset(len,0,sizeof len); memset(sum,0,sizeof sum); for(LL i=1;i<=n;i++) { scanf("%lld",&col[i]); if(!vis[col[i]]) { cnt++; vis[col[i]]=1; } } for(LL i=1;i<=n-1;i++) { LL u,v; scanf("%lld %lld",&u,&v); add(u,v); add(v,u); } printf("Case #%lld: ",++k); if(cnt==1) //只有一种情况 { printf("%lld\n",(LL)n*(n-1LL)/2LL); } else { dfs(1,-1); for(LL i=1;i<=n;i++) { if(!vis[i]) continue; ans+=(LL)(n-sum[i])*(n-sum[i]-1LL)/2LL; } printf("%lld\n",(LL)cnt*n*(n-1LL)/2LL-ans); } }}
阅读全文
0 0
- HDU 6035 Colorful Tree [树形dp]
- hdu 6035 Colorful Tree(树形DP)
- HDU 6035 Colorful Tree (树形dp)
- HDU 6035 Colorful Tree(树形DP)
- HDU 6035 Colorful Tree(树形dp)
- HDU 6035 Colorful Tree(树形DP)
- 【HDU 6035 Colorful Tree】+ 树形 dp + 思维
- Colorful Tree hdu 6035 (虚树,树形dp)
- HDU 6035 Colorful Tree (树形DP,dfs)
- HDU6035 Colorful Tree(树形dp)
- HDU-6035 Colorful Tree(树型dp)
- hdu 6035 Colorful Tree 树状dp
- HUD 6035 Colorful Tree dfs序||树形dp
- 【HDU6035】Colorful Tree(dfs,树形dp)
- hdu 6035 Colorful Tree
- HDU 6035 colorful tree
- [HDU]-6035 Colorful Tree
- hdu 6035 Colorful Tree
- 学前段半个月的一些心得(二)
- 由bootstrap-fileinput.js图片预览高度BUG谈谈如何提高驾驭开源插件的能力
- PO/POJO/BO/DTO/VO的区别
- Fibonacci Again!
- Java基础
- HDU 6035 Colorful Tree(树形DP)
- bash: 一键安装Pycharm
- 栈
- 二叉树的遍历
- 批量修改文件名
- Netty
- C++17中那些值得关注的特性(上)
- 2:面向对象
- git中使用fork