zoukankan      html  css  js  c++  java
  • HDU 5977 Garden of Eden(点分治求点对路径颜色数为K)

    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。
    题解
    点分治,把模板的dfsdis改成状态。
    代码
      1 #include<stdio.h>
      2 #include<string.h>
      3 #include<vector>
      4 #include<algorithm>
      5 using namespace std;
      6 
      7 #define LL long long
      8 const int maxn=5e4+5;
      9 
     10 vector<int>G[maxn];
     11 int n,k,sumk,a[maxn],p[15];
     12 int mx[maxn],size[maxn],vis[maxn],dis[maxn],MIN,num[1050],cnt,root;
     13 LL ans;
     14 void init()
     15 {
     16     sumk=(1<<k)-1;
     17     ans=0;
     18     for(int i=1;i<=n;i++)
     19     {
     20         G[i].clear();
     21         vis[i]=0;
     22     }
     23 }
     24 void dfssize(int u,int fa)
     25 {
     26     size[u]=1;
     27     mx[u]=0;
     28     for(int i=0;i<G[u].size();i++)
     29     {
     30         int v=G[u][i];
     31         if(!vis[v]&&v!=fa)
     32         {
     33             dfssize(v,u);
     34             size[u]+=size[v];
     35             mx[u]=max(mx[u],size[v]);
     36         }
     37     }
     38 }
     39 void dfsroot(int r,int u,int fa)
     40 {
     41     if(size[r]-size[u]>mx[u])mx[u]=size[r]-size[u];
     42     if(mx[u]<MIN)MIN=mx[u],root=u;
     43     for(int i=0;i<G[u].size();i++)
     44     {
     45         int v=G[u][i];
     46         if(!vis[v]&&v!=fa)dfsroot(r,v,u);
     47     }
     48 }
     49 void dfsdis(int u,int fa,int sta)
     50 {
     51     dis[cnt++]=sta;
     52     num[sta]++;
     53     for(int i=0;i<G[u].size();i++)
     54     {
     55         int v=G[u][i];
     56         if(!vis[v]&&v!=fa)
     57             dfsdis(v,u,sta|a[v]);
     58     }
     59 }
     60 LL cal(int r,int sta)
     61 {
     62     cnt=0;
     63     memset(num,0,sizeof num);
     64     dfsdis(r,r,sta);
     65     for(int i=0;i<k;i++)
     66         for(int j=sumk;j>=0;j--)
     67             if(!(p[i]&j))
     68                 num[j]+=num[j|p[i]];
     69     LL ret=0;
     70     for(int i=0;i<cnt;i++)ret+=num[sumk^dis[i]];
     71     return ret;
     72 }
     73 void dfs(int u)
     74 {
     75     MIN=n;
     76     dfssize(u,u);
     77     dfsroot(u,u,u);
     78     int Grivate=root;
     79     ans+=cal(Grivate,a[Grivate]);
     80     vis[root]=1;
     81     for(int i=0;i<G[Grivate].size();i++)
     82     {
     83         int v=G[Grivate][i];
     84         if(!vis[v])
     85         {
     86             ans-=cal(v,a[v]|a[Grivate]);
     87             dfs(v);
     88         }
     89     }
     90 }
     91 int main()
     92 {
     93     p[0]=1;
     94     for(int i=1;i<=11;i++)p[i]=p[i-1]*2;
     95     while(scanf("%d%d",&n,&k)!=EOF)
     96     {
     97         init();
     98         for(int i=1;i<=n;i++)
     99         {
    100             scanf("%d",&a[i]);
    101             a[i]=p[a[i]-1];
    102         }
    103         int u,v;
    104         for(int i=1;i<n;i++)
    105         {
    106             scanf("%d%d",&u,&v);
    107             G[u].push_back(v);
    108             G[v].push_back(u);
    109         }
    110         dfs(1);
    111         printf("%lld
    ",ans);
    112     }
    113     return 0;
    114 }
  • 相关阅读:
    SQL中distinct的用法
    python requests 高级用法 -- 包括SSL 证书错误的解决方案
    odoo js
    线程池的理解及使用
    单点登录原理及简单实现
    如何重写hashCode()和equals()方法
    多线程中的Lock小结
    Sql语句的优化摘要
    Spring事务管理
    并发编程之原子性、可见性、有序性的简单理解
  • 原文地址:https://www.cnblogs.com/taozi1115402474/p/9939633.html
Copyright © 2011-2022 走看看