【HDUOJ 6035】 Colorful Tree 【DFS】
来源:互联网 发布:cf冰龙刷枪软件2016 编辑:程序博客网 时间:2024/06/03 20:17
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 2259 Accepted Submission(s): 972
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
2017 Multi-University Training Contest - Team 1
题意: 给出一棵有n个节点的树,每个节点有一种颜色,任意两个节点之间的距离为两点之间的最短路径上经过点的颜色种类数(包括端点),且所有点两两之间的距离和。
分析: 我们可以反向考虑,先算出所有路径都经过n种颜色的距离和: n * n * (n-1)/2,那么我们需要减去的便是对于每一种颜色,不经过该颜色的路径数即可。
#include<bits/stdc++.h>using namespace std;typedef long long LL ; const int MAXN = 200000+10;const int MAXM = 1e6;const int mod = 1e9+7;const int inf = 0x3f3f3f3f;struct Edge{ int from,to,nexts;}edge[MAXN<<2];int head[MAXN],top;void init(){ memset(head,-1,sizeof(head)); top=0;}void addedge(int a,int b){ Edge e={a,b,head[a]}; edge[top]=e;head[a]=top++;}LL n;LL ans;LL num[MAXN]; LL sum[MAXN]; LL color[MAXN];void dfs(int now,int fa){ num[now]=1; LL add=0; for(int i=head[now];i!=-1;i=edge[i].nexts){ Edge e=edge[i]; if(e.to==fa) continue; LL pre=sum[color[now]]; dfs(e.to,now); num[now]+=num[e.to]; LL tmp=num[e.to]-(sum[color[now]]-pre); ans-=tmp*(tmp-1)/2; add+=tmp; } sum[color[now]]+=add+1;}int main(){ int ncase=1; while(~scanf("%lld",&n)){ init(); memset(num,0,sizeof(num)); memset(sum,0,sizeof(sum)); printf("Case #%d: ",ncase++); for(int i=1;i<=n;i++) scanf("%d",&color[i]); int m=n-1; while(m--){ int a,b,c; scanf("%d%d",&a,&b); addedge(a,b); addedge(b,a); } ans=n*(n-1)*n/2; dfs(1,-1); for(int i=1;i<=n;i++){ LL temp=n-sum[i]; ans-=temp*(temp-1)/2; } printf("%lld\n",ans); } return 0;}
- 【HDUOJ 6035】 Colorful Tree 【DFS】
- Hdu-6035 Colorful Tree(dfs)
- hdu 6035 Colorful Tree(dfs)
- hdu 6035 Colorful Tree(dfs)
- HDU 6035 Colorful Tree dfs
- HDU-6035 Colorful Tree(dfs序造树)
- HDU 6035-Colorful Tree(dfs+树)
- HUD 6035 Colorful Tree dfs序||树形dp
- HDU 6035 Colorful Tree (树形DP,dfs)
- 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
- wordpress设置“固定链接”后,页面404错误的解决方法
- 选择器优先级
- 有时间要学习的框架spring boot
- 对象=null new一个对象,单列和对象实例化
- 各种运算符
- 【HDUOJ 6035】 Colorful Tree 【DFS】
- 储罐自动计量系统
- thinkphp5多级控制器及其使用
- 理解HTML语义化
- 线段树~~~~ hdu1166
- UVA 10518 How Many Calls? (递推+ 矩阵快速幂)
- javascript运行过程中的“预编译阶段”和“执行阶段”
- arcgis api javascript 之GraphicsLayer图层的infoWindow消息窗实现
- Android proc目录