hdu-5977 Garden of Eden (tree Division)

Source: Internet
Author: User

Topic Links:

Garden of Eden

Time limit:10000/5000 MS (java/others) Memory limit:131072/131072 K (java/others)
Total submission (s): Accepted submission (s): 75


Problem Descriptionwhen 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 is lonely in the garden and so God made Eve. When Adam is asleep one night, God took a rib from him and made Eve beside him. God said to them, "Here's 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 to the tree someday, the snake called her. He gave her Apple and persuaded she 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 stories we are familiar with. But we imagine this Satan love knowledge more than doing bad things. In Garden of Eden, the tree of knowledge has n apples, and there is k varieties of apples on the tree. Satan wants to eat all kinds of apples 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, same). The same point can is only passed once. He wants to know what many different kinds of schemes he can choose to eat all kinds of apple. Schemes is different when their starting points is different or ending points is different.

Inputthere is several cases. Process till end of input.
For each case, the first line contains-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 is represented by integers between 1 and K.
Each of the following n-1 lines contains a integers u and v,meaning there is one edge between U and v.1≤n≤50000, 1≤k≤10

Outputfor each case output your answer to a single line.

Sample INPUT3 21 2 21 21 3

Sample OUTPUT6 Test Instructions: Give a tree, give the color of each node, ask how many pairs of nodes meet all the points on this path and the k color; Idea: first want to use the tree-shaped DP, but the MLE, so is the tree divided the treatment, the template to a set on the good, attention every time to find the root to save; AC Code:
#include <bits/stdc++.h>using namespace Std;typedef long long ll;const int maxn=5e4+5;int n,k,fk,a[maxn],p[13]; int SIZ[MAXN],SON[MAXN],VIS[MAXN],ROOT,MAX,CNT,NUM[1030],D[MAXN],CN=0,HEAD[MAXN]; LL ans=0;struct edge{int to,next;}    edge[2*maxn];inline void Add_edge (int from,int to) {edge[cn].to=to;    Edge[cn].next=head[from]; head[from]=cn++;}    inline void init () {cn=0;    fk= (1<<k)-1;    ans=0;        for (int i=0;i<=n;i++) vis[i]=0; memset (head,-1,sizeof (Head));}    void get_siz (int cur,int fa) {siz[cur]=1;    son[cur]=0;        for (int i=head[cur];i!=-1;i=edge[i].next) {int x=edge[i].to; if (x==fa| |        VIS[X]) continue;        Get_siz (x,cur);        SIZ[CUR]+=SIZ[X];    if (Siz[x]>son[cur]) son[cur]=siz[x];    }}void find_root (int cur,int fa,int RT) {if (Siz[rt]-siz[cur]>son[cur]) son[cur]=siz[rt]-siz[cur];    if (Son[cur]<max) max=son[cur],root=cur;        for (int i=head[cur];i!=-1;i=edge[i].next) {int x=edge[i].to; if (x==fa| | Vis[x])        Continue    Find_root (X,CUR,RT);    }}void get_state (int cur,int fa,int STA) {D[cnt++]=sta;    num[sta]++;        for (int i=head[cur];i!=-1;i=edge[i].next) {int x=edge[i].to; if (vis[x]| |        X==FA) continue;    Get_state (X,cur,a[x]|sta);    }}ll cal (int cur,int STA) {cnt=0;    memset (num,0,sizeof (num));    Get_state (Cur,0,sta); for (int i=0;i<k;i++) {for (int j=fk;j>=0;j--) {if (!) (        P[I]&AMP;J)) Num[j]+=num[j|p[i]];    }} LL ret=0;    for (int i=0;i<cnt;i++) ret=ret+num[fk^d[i]]; return ret;}    void dfs (int cur) {max=n;    Get_siz (cur,0);    Find_root (cur,0,cur);    int root=root;    Ans=ans+cal (Root,a[root]);    Vis[root]=1;        for (int j=head[root];j!=-1;j=edge[j].next) {int x=edge[j].to;        if (vis[x]) continue;        Ans=ans-cal (x, (A[x]|a[root]));    DFS (x);    }}int Main () {p[0]=1;for (int i=1;i<=11;i++) p[i]=p[i-1]*2;      while (scanf ("%d%d", &n,&k)!=eof) {init ();  for (int i=1;i<=n;i++) {scanf ("%d", &a[i]);        A[I]=P[A[I]-1];        } int u,v;            for (int i=1;i<n;i++) {scanf ("%d%d", &u,&v);            Add_edge (U,V);        Add_edge (V,u);        } dfs (1);    printf ("%lld\n", ans); } return 0;}

  

hdu-5977 Garden of Eden (tree Division)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.