HDU5977 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
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 5e4 + 5;
int n, cnt, head[maxn], k, vis[maxn], root, maxx, d[maxn];
int num, allnode, son[maxn], maxson[maxn];
ll dp[1030], ans; int col[maxn];
struct fucl {
	int v, w, ne;
}ed[maxn << 1];
void init() {
	memset(vis, 0, sizeof vis);
	memset(head, -1, sizeof head);
	root = 0; ans = 0; cnt = 0;
}
void add(int u, int v, int w) {
	ed[cnt].v = v; ed[cnt].w = w;
	ed[cnt].ne = head[u]; head[u] = cnt++;
}
void getdis(int u, int fa, int dep)
{
	dp[dep]++;
	d[++num] = dep;
	for (int i = head[u]; ~i; i = ed[i].ne) {
		int to = ed[i].v;
		if (to == fa || vis[to]) continue;
		getdis(to, u, dep | (1 << col[to]));
	}
}
void getroot(int u, int fa) {
	son[u] = 1; maxson[u] = 0;
	for (int i = head[u]; ~i; i = ed[i].ne) {
		int v = ed[i].v;
		if (vis[v] || v == fa)continue;
		getroot(v, u);
		son[u] += son[v];
		maxson[u] = max(maxson[u], son[v]);
	}
	maxson[u] = max(maxson[u], allnode - son[u]);
	if (maxson[u] < maxson[root])root = u;
}
ll cal(int u, int sta) {
	num = 0;
	memset(dp, 0, sizeof dp);
	getdis(u, -1, sta | (1 << col[u]));
	for (int i = 0; i < k; i++) {
		for (int j = 0; j < (1 << k); j++) {
			if (!((1 << i)&j))dp[j] += dp[j | (1 << i)];
		}
	}
	ll res = 0;
	for (int i = 1; i <= num; i++) {
		int t = d[i];
		t ^= (1 << k) - 1;
		res += dp[t];
	}
	return res;
}
void getdfs(int u) {
	vis[root] = 1;
	ans += cal(u, 1 << col[u]);
	for (int i = head[u]; ~i; i = ed[i].ne) {
		int v = ed[i].v;
		if (vis[v])continue;
		ll t = cal(v, 1 << col[u]);
		ans -= t;
		maxson[root = 0] = allnode = son[v];
		getroot(v, -1);
		getdfs(root);
	}
}
int main() {
	ios::sync_with_stdio(0);
	while (cin >> n >> k) {
		init();
		for (int s = 1; s <= n; s++) {
			cin >> col[s];
			col[s]--;
		}
		for (int s = 1; s < n; s++) {
			int a, b; cin >> a >> b;
			add(a, b, 0); add(b, a, 0);
		}
		ans = 0;
		maxson[root = 0] = allnode = n;
		getroot(1, -1);
		getdfs(root);
		cout << ans << endl;
	}
}

 

 

 

 

全部评论

相关推荐

不愿透露姓名的神秘牛友
11-29 12:19
点赞 评论 收藏
分享
牛客162194370号:
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务