HDU 6035---Colorful Tree(树形DP)
Posted 茶飘香~
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 6035---Colorful Tree(树形DP)相关的知识,希望对你有一定的参考价值。
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.
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.
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
题意:有一棵n个节点的树,编号从 1 到 n ,每个节点涂有一种颜色ci (1<=ci<=n),对于树上的每条路径有个值,表示这条路径上的颜色种类数,现在对树上所有的路径值求和。
思路:可以转换一下题目描述,即求包含颜色ci的路径数,然后对所有颜色求和即可。这样求依然不好计算,可以反向考虑, 包含颜色ci的路径数=n*(n-1)/2 - 不包含颜色ci的路径数,求不包含颜色ci的路径数 即为颜色ci将整棵树分成一个个的联通块,对每个联通块,如果包含节点数为x,即为C(x,2),对所有联通块求和;
代码如下:
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring> #include <vector> using namespace std; typedef long long LL; const int N=200005; int c[N],sum[N],sz[N]; vector<int> t[N]; LL ans; int dfs(int u,int pa) { sz[u]=1; int cn=t[u].size(); for(int i=0;i<cn;i++) { int v=t[u][i]; if(v==pa) continue; int r=sum[c[u]]; sz[u]+=dfs(v,u); int o=sum[c[u]]-r; ans+=(LL)(sz[v]-o)*(LL)(sz[v]-o-1)/2; sum[c[u]]+=sz[v]-o; } sum[c[u]]++; return sz[u]; } int main() { ///cout << "Hello world!" << endl; int n,Case=1; while(scanf("%d",&n)!=EOF) { memset(sum,0,sizeof(sum)); for(int i=1;i<=n;i++){ scanf("%d",&c[i]); t[i].clear(); } for(int i=1;i<n;i++) { int u,v; scanf("%d%d",&u,&v); t[u].push_back(v); t[v].push_back(u); } ans=0; dfs(1,-1); for(int i=1;i<=n;i++) { ans+=(LL)(n-sum[i])*(LL)(n-sum[i]-1)/2; } ans=(LL)n*(LL)(n-1)*(LL)n/2-ans; printf("Case #%d: %lld\n",Case++,ans); } return 0; }
以上是关于HDU 6035---Colorful Tree(树形DP)的主要内容,如果未能解决你的问题,请参考以下文章