日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【HDU - 5977】Garden of Eden(树分治)

發布時間:2023/12/10 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【HDU - 5977】Garden of Eden(树分治) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

When God made the first man, he put him on a beautiful garden, the Garden of Eden. Here Adam lived with all animals. God gave Adam eternal life. But Adam was lonely in the garden, so God made Eve. When Adam was asleep one night, God took a rib from him and made Eve beside him. God said to them, “here in the Garden, you can do everything, but you cannot eat apples from the tree of knowledge.”?
One day, Satan came to the garden. He changed into a snake and went to live in the tree of knowledge. When Eve came near the tree someday, the snake called her. He gave her an apple and persuaded her to eat it. Eve took a bite, and then she took the apple to Adam. And Adam ate it, too. Finally, they were driven out by God and began a hard journey of life.?
The above is the story we are familiar with. But we imagine that Satan love knowledge more than doing bad things. In Garden of Eden, the tree of knowledge has n apples, and there are k varieties of apples on the tree. Satan wants to eat all kinds of apple to gets all kinds of knowledge.So he chooses a starting point in the tree,and starts walking along the edges of tree,and finally stops at a point in the tree(starting point and end point may be same).The same point can only be passed once.He wants to know how many different kinds of schemes he can choose to eat all kinds of apple. Two schemes are different when their starting points are different or ending points are different.?

Input

There are several cases.Process till end of input.?
For each case, the first line contains two integers n and k, denoting the number of apples on the tree and number of kinds of apple on the tree respectively.?
The second line contains n integers meaning the type of the i-th apple. Types are represented by integers between 1 and k .?
Each of the following n-1 lines contains two integers u and v,meaning there is one edge between u and v.1≤n≤50000, 1≤k≤10?

Output

For each case output your answer on a single line.

Sample Input

3 2 1 2 2 1 2 1 3

Sample Output

6

題目大意:

給定N個節點的樹,每個節點有顏色,共k種顏色,求樹上所有滿足從i到j所經過的點包含了所有顏色的點對,(i,j)和(j,i)視為不同且i可以等于j。(N<=5e4,k<=10)

解題報告:

k=10,考慮狀態壓縮,最多可以(1<<(10)-1)種狀態,利用樹分治保存每個點到節點的路徑狀態。比求路徑之和等于k的點對要簡單,因為不需要容斥,可以用藍書上第一種方法去求(按照子樹順序分別處理),但是細節還是要注意,比如當前的根節點別被統計多次了,且別忘了從根節點當其中一個端點,開始往下一條鏈的那種情況,其實這種情況是不需要特判的,只需要在做當前根節點上的所有工作之前,讓cnt[(1<<col[cur])]++即可。

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<stack> #include<map> #include<set> #include<string> #include<cmath> #include<cstring> #define FF first #define SS second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 1e5 + 5; int col[MAX],head[MAX],tot,size[MAX],n,k; struct Node {int to,ne; } e[MAX<<1]; bool vis[MAX]; int up,cnt[MAX],tmp[MAX]; void add(int u,int v) {e[++tot].to = v;e[tot].ne=head[u];head[u]=tot; } ll ans; int rt,all,son[MAX]; //getRoot調用之前必須先rt=0; void getRoot(int cur,int fa) {size[cur]=1;son[cur]=0;for(int i = head[cur]; ~i; i = e[i].ne) {int v = e[i].to;if(v == fa || vis[v]) continue;getRoot(v,cur); size[cur] += size[v];son[cur] = max(son[cur],size[v]);}son[cur] = max(son[cur],all - size[cur]);//注意這里的all指的就是當前子樹的節點個數 if(son[rt] == 0 || son[rt] > son[cur]) rt = cur;//值小,則更新根節點 } void gx(int cur,int fa) {size[cur]=1;for(int i = head[cur]; ~i; i = e[i].ne) {int v = e[i].to;if(v == fa || vis[v]) continue;gx(v,cur); size[cur] += size[v];} } void get(int cur,int fa,int sta) {tmp[sta]++; // if(sta == (up-1)) ans++;for(int bit = 0; bit<up; bit++) {if((bit|sta) == (up-1)) ans += cnt[bit];} for(int i = head[cur]; ~i; i = e[i].ne) {int v = e[i].to;if(v == fa || vis[v]) continue;get(v,cur,sta|(1<<col[v]));} } void cal(int cur) {for(int i = 0; i<up; i++) cnt[i] = 0;cnt[(1<<col[cur])]++; // get(cur,0,1<<col[cur]);//注意這里不能等效處理,因為cur的子節點需要特殊處理,因為我們需要一個子樹一個子樹的處理。 for(int i = head[cur]; ~i; i = e[i].ne) {int v = e[i].to; if(vis[v]) continue;for(int sta = 0; sta<up; sta++) tmp[sta]=0; // tmp[(1<<col[cur])]++;//不能在這里加!不然就重復了! get(v,cur,(1<<col[cur]) | (1<<col[v]));for(int sta = 0; sta<up; sta++) cnt[sta] += tmp[sta];} } void dfs(int cur) {rt=0; getRoot(cur,0); cur=rt; vis[cur]=1; gx(cur,0);cal(cur);for(int i = head[cur]; ~i; i = e[i].ne) {int v = e[i].to;if(vis[v]) continue;all = size[v]; dfs(v); } } int main() {while(~scanf("%d%d",&n,&k)) {tot=0; up = 1<<k; ans = 0;for(int i = 1; i<=n; i++) head[i] = -1,vis[i]=size[i]=0;for(int i = 1; i<=n; i++) scanf("%d",col+i),col[i]--; for(int u,v,i = 1; i<=n-1; i++) scanf("%d%d",&u,&v),add(u,v),add(v,u);if(k == 1) {printf("%lld\n",1LL*n*(n-1)+n);continue;}all=n; dfs(1);printf("%lld\n",ans*2); } return 0 ; }

?

總結

以上是生活随笔為你收集整理的【HDU - 5977】Garden of Eden(树分治)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。