1. 程式人生 > >HDU-6035:Colorful Tree(虛樹+DP)

HDU-6035:Colorful Tree(虛樹+DP)

node different ase 得到 第一題 false all 直接 files

這裏有三道長得像的題:

一: HDU6036:

There is a tree with nn nodes, each of which has a type of color represented by an integer, where the color of node ii is cici.

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.

InputThe input contains multiple test cases.

For each test case, the first line contains one positive integers nn, indicating the number of node. (2n200000)(2≤n≤200000)

Next line contains nn integers where the

ii-th integer represents cici, the color of node ii. (1cin)(1≤ci≤n)

Each of the next n1n−1 lines contains two positive integers x,yx,y (1x,yn,xy)(1≤x,y≤n,x≠y), meaning an edge between node xx and node yy.

It is guaranteed that these edges form a tree.
OutputFor each test case, output " Case #
xx: yy
" in one line (without quotes), where xxindicates the case number starting from 11 and yy 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

題意:求出所有路徑的顏色種類之和。

二:洛谷P2664

題目描述

lrb有一棵樹,樹的每個節點有個顏色。給一個長度為n的顏色序列,定義s(i,j) 為i 到j 的顏色數量。以及

技術分享圖片

現在他想讓你求出所有的sum[i]

輸入輸出格式

輸入格式:

第一行為一個整數n,表示樹節點的數量

第二行為n個整數,分別表示n個節點的顏色c[1],c[2]……c[n]

接下來n-1行,每行為兩個整數x,y,表示x和y之間有一條邊

輸出格式:

輸出n行,第i行為sum[i]

輸入樣例#1:

5
1 2 3 2 3
1 2
2 3
2 4
1 5

輸出樣例#1:

10
9
11
9
12

說明

sum[1]=s(1,1)+s(1,2)+s(1,3)+s(1,4)+s(1,5)=1+2+3+2+2=10
sum[2]=s(2,1)+s(2,2)+s(2,3)+s(2,4)+s(2,5)=2+1+2+1+3=9
sum[3]=s(3,1)+s(3,2)+s(3,3)+s(3,4)+s(3,5)=3+2+1+2+3=11
sum[4]=s(4,1)+s(4,2)+s(4,3)+s(4,4)+s(4,5)=2+1+2+1+3=9
sum[5]=s(5,1)+s(5,2)+s(5,3)+s(5,4)+s(5,5)=2+3+3+3+1=12

對於40%的數據,n<=2000
對於100%的數據,1<=n,c[i]<=10^5

題意:求出每個點到其他點路徑的顏色種類和。

三:Wannafly挑戰賽19C:多彩的樹

有一棵樹包含 N 個節點,節點編號從 1 到 N。節點總共有 K 種顏色,顏色編號從 1 到 K。第 i 個節點的顏色為 Ai
Fi 表示恰好包含 i 種顏色的路徑數量。請計算:
技術分享圖片

輸入描述:

第一行輸入兩個正整數 N 和 K,N 表示節點個數,K 表示顏色種類數量。
第二行輸入 N 個正整數, 表示節點的顏色。
接下來 N - 1 行,第 i 行輸入兩個正整數 Ui 和 Vi,表示節點 Ui 和節點 Vi 之間存在一條無向邊,數據保證這 N-1 條邊連通了 N 個節點。 1 ≤ N ≤ 50000.
1 ≤ K ≤ 10.
1 ≤ Ai ≤ K.

輸出描述:

輸出一個整數表示答案。

示例1

輸入

5 3
1 2 1 2 3
4 2
1 3
2 1
2 5

輸出

4600065

題意:對於L=[1,K],統計有多少路徑的顏色種類=L;

------------------------------------------分界線-------------------------------------------------

對於第三題,可以容斥搞定。 前面兩題可以借助虛樹來做。

第三題,容斥,因為K<=10,只有2^K種顏色組合,按照每種顏色組合,用並查集分塊。求出塊的數量...反正一系列常規操作,最後容斥減去,這裏和虛樹無關,就不講了。

第二題,因為要針對每一個點來求,所以考慮虛樹加差分。

第一題,因為只計算最後的總結果,所以可以直接一次性操作完。

具體的,對於第二題:對於每種顏色,我們用是這種顏色的點來建立虛樹,假設現在按照顏色C得到了一個虛樹:虛樹的邊代表了一個不含顏色C的連通塊,還有一些不含顏色C的連通塊在虛樹的葉子節點下邊的連通塊虛樹的根的上面的連通塊。 對於C顏色對其他點的貢獻:顏色是C的點,顯然它的結果是N,(即它到每個點的路徑都會包含這種顏色);否則,它的結果是N-所在連通塊的大小。 因為一個連通塊的結果是相同的,所以我們用差分來統計:即在這個連通塊的最高點+ans,所有最低點的兒子-ans,那麽求得的前綴和就是結果。 每種顏色都這麽幹,最後累加一次前綴和,得到每個點的結果。復雜度就是O(N*17)。17是求LCA的復雜度。

對於第三題:可以像第二題那麽幹,那麽ans=(Σ sum[i] )/2。也可以用更高效的統計方法。 同樣需要用虛樹的思想去想,但是累計的時候一起累計。那麽一次DFS就可以完事了。

【第三題可以參考】:https://blog.csdn.net/Bahuia/article/details/76141574

HDU-6035:Colorful Tree(虛樹+DP)