hdu 6035 Colorful Tree(树形DP)
来源:互联网 发布:算法的定义和特征 编辑:程序博客网 时间:2024/06/05 02:26
Colorful Tree
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 1194 Accepted Submission(s): 477
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
解题报告
#include<iostream>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<vector>#include<cmath>//#include <bits/stdc++.h>using namespace std;const int N = 200000+7;typedef long long LL;int vis[N], f[N], c[N];LL size1[N], col[N];vector<int>p[N];LL cnt;void dfs(int u,int f){ size1[u]=1; LL tmp=0; if(col[c[u]]!=0) { tmp=col[c[u]]; col[c[u]]=0; } for(int i=0;i<p[u].size();i++) { int v=p[u][i]; if(v==f) continue; dfs(v,u); size1[u]+=size1[v]; cnt+=(LL)(size1[v]-col[c[u]])*(size1[v]-col[c[u]]-1)/2; col[c[u]]=0; } col[c[u]]=tmp+size1[u]; return ;}int main(){ LL n; int ncase=1; while(scanf("%lld", &n)!=EOF) { LL color=0; memset(vis,0,sizeof(vis)); memset(col,0,sizeof(col)); memset(c,0,sizeof(c)); for(int i=1;i<=n;i++) { scanf("%d", &c[i]); if(vis[c[i]]==0) color++; vis[c[i]]=1; p[i].clear(); } for(int i=1;i<n;i++) { int x, y; scanf("%d %d", &x, &y); p[x].push_back(y),p[y].push_back(x); } cnt=0; dfs(1,-1); for(int i=1;i<=n;i++) { if(!vis[i]) continue; cnt+=(LL)(n-col[i])*(n-col[i]-1)/2; } LL x=(LL)(n*(n-1))/2*color-cnt; printf("Case #%d: %lld\n",ncase++,x); } return 0;}
阅读全文
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)
- HDU-6035 Colorful Tree(树型dp)
- HDU6035 Colorful Tree(树形dp)
- hdu 6035 Colorful Tree 树状dp
- Colorful Tree(HDU 6035)
- HUD 6035 Colorful Tree dfs序||树形dp
- hdu6035-树形dp-2017多校(2)&难-Colorful Tree
- 【HDU6035】Colorful Tree(dfs,树形dp)
- hdu 6035 Colorful Tree
- HDU 6035 colorful tree
- 喷水装置一
- GTK编译后运行出现: GLib-GIO-CRITICAL **: g_dbus_connection_register_object: assertion 'G_IS_DBUS_CONNECTION
- PHP向数组中添加元素
- OSGi框架与类加载器
- excel 自动group脚本
- hdu 6035 Colorful Tree(树形DP)
- postgresql的json操作
- mybatis+oracle+foreach list 批量插入数据
- Java 设计模式_适配器模式
- 微信开发 现金红包、裂变红包、企业付款
- 2017 Multi-University Training Contest
- 用C语言程序判断一个字符是否是字母或数字
- Javascript闭包
- 在datatable中显示百分比进度条