HDU 5977 Garden of Eden (树分治+状态压缩)

题意:给一棵节点数为n,节点种类为k的无根树,问其中有多少种不同的简单路径,可以满足路径上经过所有k种类型的点?

析:对于路径,就是两类,第一种情况,就是跨过根结点,第二种是不跨过根结点,分别讨论就好,由于结点比较大,所以采用分治来进行处理,优先选取重点作为划分的依据。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define FOR(x,n)  for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 50000 + 10;
const LL mod = 1e9 + 7;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
    return r > 0 && r <= n && c > 0 && c <= m;
}
int all;

struct Edge{
  int to, next;
};

Edge edge[maxn<<1];
int head[maxn], cnt;
int val[maxn];

void addEdge(int u, int v){
  edge[cnt].to = v;
  edge[cnt].next = head[u];
  head[u] = cnt++;
}

int root, num, f[maxn];
LL dp[1<<10];
int sum[maxn];
bool vis[maxn];
LL ans;

void dfs_for_root(int u, int fa){
  sum[u] = 1;  f[u] = 0;
  for(int i = head[u]; ~i; i = edge[i].next){
    int v = edge[i].to;
    if(v == fa || vis[v])  continue;
    dfs_for_root(v, u);
    sum[u] += sum[v];
    f[u] = max(f[u], sum[v]);
  }
  f[u] = max(f[u], num - sum[u]);
  if(f[root] > f[u])  root = u;
}

void dfs_for_color(int u, int fa, int s){
  for(int i = head[u]; ~i; i = edge[i].next){
    int v = edge[i].to;
    if(v == fa || vis[v])  continue;
    ++dp[s|1<<val[v]];
    dfs_for_color(v, u, s|1<<val[v]);
  }
}

LL solve(int u, int s){
  ms(dp, 0);
  ++dp[s];
  dfs_for_color(u, -1, s);
  LL ans = 0;
  for(int i = 0; i <= all; ++i){
    if(!dp[i])  continue;
    int tmp = 0;
    tmp += dp[all];
    for(int j = i; j; j = (j-1)&i)
      tmp += dp[all^j];
    ans += (LL)tmp * dp[i];
  }
  return ans;
}

void dfs_for_ans(int u){
  ans += solve(u, 1<<val[u]);
  vis[u] = true;
  for(int i = head[u]; ~i; i = edge[i].next){
    int v = edge[i].to;
    if(vis[v])  continue;
    ans -= solve(v, 1<<val[u]|1<<val[v]);
    root = 0;
    f[0] = num = sum[v];
    dfs_for_root(v, u);
    dfs_for_ans(root);
  }
}

int main(){
  while(scanf("%d %d", &n, &m) == 2){
    for(int i = 1; i <= n; ++i){
      scanf("%d", val+i);
      --val[i];
    }
    all = (1<<m) - 1;
    ms(head, -1);  cnt = 0;
    for(int i = 1; i < n; ++i){
      int u, v;
      scanf("%d %d", &u, &v);
      addEdge(u, v);
      addEdge(v, u);
    }
    ms(vis, 0);
    root = 0;  ans = 0LL;
    f[0] = num = n;
    dfs_for_root(1, -1);
    dfs_for_ans(root);
    printf("%I64d
", ans);
  }
  return 0;
}

  

原文地址:https://www.cnblogs.com/dwtfukgv/p/7420139.html