HDU 6035 Colorful Tree
来源:互联网 发布:学编程从什么开始学 编辑:程序博客网 时间:2024/06/15 21:50
Colorful Tree
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2067 Accepted Submission(s): 881
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条路都出现 那么答案就是n*(n-1)/2*num
现在去重就是去掉每种颜色没经过多少路径
用一个dfs序搞一搞
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;typedef long long ll;struct node{ int to,nex;}edge[400005];int head[200005],cnt,num[200005],col[200005],cs,sum[200005],siz[200005],vis[200005];void add(int u,int v){ edge[cnt].to=v; edge[cnt].nex=head[u]; head[u]=cnt++;}ll ans,de;void dfs(int u,int pre){ siz[u]=1; int step=0; int s1=sum[num[u]]; for(int i=head[u];i!=-1;i=edge[i].nex){ int v=edge[i].to; if(v!=pre){ dfs(v,u); siz[u]+=siz[v]; int qr=sum[num[u]]-s1; int szv=siz[v]-qr; s1=sum[num[u]]; de=(de+1LL*szv*(szv-1)/2); step+=qr; } } sum[num[u]]+=siz[u]-step; } int main(){ int n,i,j,cas=1,u,v; while(scanf("%d",&n)!=EOF){ cs=0; de=0; cnt=0; memset(vis,0,sizeof(vis)); for(i=1;i<=n;i++){ head[i]=-1; scanf("%d",&num[i]); col[++cs]=num[i]; sum[i]=0; siz[i]=0; vis[num[i]]=1; } for(i=1;i<n;i++){ scanf("%d%d",&u,&v); add(u,v); add(v,u); } sort(col+1,col+1+cs); cs=unique(col+1,col+1+cs)-col-1; ans=(ll)n*(n-1)*cs/2; dfs(1,1); for(int i=1;i<=n;i++){ if(vis[i]&&i!=num[1]){ int qr=sum[i]; int szv=n-qr; de=(de+1LL*szv*(szv-1)/2); } } ans-=de; printf("Case #%d: %lld\n",cas++,ans); } return 0;}
阅读全文
0 0
- 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
- HDU 6035 Colorful Tree
- HDU 6035 Colorful Tree
- hdu 6035(Colorful Tree)
- Hdu-6035 Colorful Tree(dfs)
- hdu 6035 Colorful Tree(dfs)
- hdu 6035 Colorful Tree(dfs)
- Colorful Tree(HDU 6035)
- HDU 6035 Colorful Tree dfs
- HDU 6035 Colorful Tree [树形dp]
- HDU 6035 Colorful Tree(虚树)
- Java 内部类原理解析
- HDU 6040 Hints of sd0061 stl
- Java-synchronize和lock详解(转)
- oop
- POJ3041:Asteroids(二分图匹配)
- HDU 6035 Colorful Tree
- JDK源码分析之主要阻塞队列实现类LinkedBlockingQueue
- 杂记-路由器上网设置:输入路由器设置网址却登陆至猫
- 最长公共子序列
- MyISAM存储引擎
- 不完整类型incomplete type
- Feign学习笔记1-实战
- Spring MVC 学习总结(一)——MVC概要与环境配置
- HDU 6044 Limited Permutation 想法