HDU 6035 Colorful Tree(樹形DP)
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 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
Source
http://blog.csdn.net/Bahuia/article/details/76141574
這篇講的比較好。
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define LL long long
const 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);
}
}
}
相關文章
- HDU6035-Colorful Tree-虛樹思想
- [HDU6793] Tokitsukaze and Colorful Tree
- 熟練剖分(tree) 樹形DP
- 樹形DP
- 樹形DP!
- CF 1029E Tree with Small Distances 樹形DP or 貪心
- 樹上染色(樹形dp)
- [SDCPC2023] Colorful Segments 線段樹轉移DP
- [筆記]樹形dp筆記
- CF1039D You Are Given a Tree (樹形 dp + 貪心 + 根號分治)
- 樹形DP二三知識
- 樹上的等差數列 [樹形dp]
- [樹形dp][HAOI2015]樹上染色
- HDU 3333 Turing Tree(線段樹+離線操作)
- hdu 2111 Saving HDU (DP)
- LayUI—tree樹形結構的使用UI
- hdu 6415 - DP
- UVA 1220 Party at Hali-Bula (樹形DP)
- cf633F. The Chocolate Spree(樹形dp)
- BZOJ 4726 [POI2017]Sabota?:樹形dp
- CCF之網路延時(樹形dp)
- HDU1024(dp)
- HDU6415(DP)
- 【BZOJ3743】[Coci2015]Kamp 樹形DP
- 【動態規劃】樹形DP完全詳解!動態規劃
- SDOI2018 榮譽稱號(樹形dp)
- ZROJ#398. 【18提高7】隨機遊走(期望dp 樹形dp)隨機
- HDU 6415 (計數dp)
- [ABC337G] Tree Inversion(換根 dp + 值域線段樹)
- E73 樹形DP P3177 [HAOI2015] 樹上染色
- Luogu P3177 樹上染色 [ 藍 ] [ 樹形 dp ] [ 貢獻思維 ]
- HDU 6415 Rikka with Nash Equilibrium (DP)UI
- bzoj1060: [ZJOI2007]時態同步(樹形Dp)
- HDU 1074 Doing Homework(狀壓DP)
- HDU6415:Rikka with Nash Equilibrium(dp)UI
- Antd:Tree樹形控制元件資料解析(JSON轉換)控制元件JSON
- Vue+element中Tree樹形控制元件資料失效~Vue控制元件
- hdu 6446 Tree and Permutation(dfs+思維)