hdu-5977 Garden of Eden(树分治)

题目链接:

Garden of Eden

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 210    Accepted Submission(s): 75


Problem Description
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
 
题意:
 
给出一棵树,给出每个节点的颜色,问有多少对节点满足这个路径上的所有点的并为K种颜色;
 
思路:
 
一开始想用树形dp搞,然而MLE了,所以就是树分治了,模板往上一套就好了;注意每次寻找到了root就保存下来;
 
AC代码:
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#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]&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;
}

  

posted @   LittlePointer  阅读(1367)  评论(0编辑  收藏  举报
编辑推荐:
· 用 C# 插值字符串处理器写一个 sscanf
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
阅读排行:
· 趁着过年的时候手搓了一个低代码框架
· 本地部署DeepSeek后,没有好看的交互界面怎么行!
· 为什么说在企业级应用开发中,后端往往是效率杀手?
· 用 C# 插值字符串处理器写一个 sscanf
· 乌龟冬眠箱湿度监控系统和AI辅助建议功能的实现
点击右上角即可分享
微信分享提示