HDU 5977 树分治+状态压缩

Garden of Eden

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


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
题意:求树上所有满足从i到j所经过的点包含了1到k的所有权值的点对,(1,j)和(j,i)视为不同且i可以等于j。
k=10,考虑状态压缩,最多可以后(1<<(10)-1)种状态,利用树分治保存每个点到节点的路径状态。比较类似求路径之和等于k的点对,只不过这里需要或运算。
#include
#include
#include
#include
using namespace std;
#define ll long long
const int maxm = 100005;
const int INF = 1023;
vectorv[maxm];
int val[maxm], vis[maxm], flag[maxm], cnt[maxm], son[maxm], s[maxm];
int n, m, k, mx, sn, root;
long long ans;
void getroot(int k, int pre)
{
	son[k] = 1, s[k] = 0;
	for (int i = 0;i < v[k].size();i++)
	{
		int V = v[k][i];
		if (V == pre || vis[V]) continue;
		getroot(V, k);
		son[k] += son[V], s[k] = max(s[k], son[V]);
	}
	s[k] = max(s[k], sn - s[k]);
	if (s[root] > s[k]) root = k;
}
void getdeep(int k, int pre, int num)
{
	num |= (1 << (val[k] - 1));
	int x = num;
	for (int i = 1;i <= mx;i++)
	{
		if (flag[i])
			if ((num | i) == mx) ans += flag[i] - cnt[i];
	}
	flag[num]++, cnt[num]++;
	for (int i = 0;i < v[k].size();i++)
	{
		int V = v[k][i];
		if (V == pre || vis[V]) continue;
		getdeep(V, k, num);
	}
}
void query(int k)
{
	for (int i = 0;i <= mx;i++) flag[i] = 0;
	flag[1 << (val[k]-1)]++;
	for (int i = 0;i < v[k].size();i++)
	{
		int V = v[k][i];
		if (vis[V]) continue;
		for (int j = 1;j <= mx;j++) cnt[j] = 0;
		getdeep(V, 0, 1 << (val[k] - 1));
	}
}
void dfs(int k)
{
	vis[k] = 1, query(k);
	for (int i = 0;i < v[k].size();i++)
	{
		int V = v[k][i];
		if (vis[V]) continue;
		root = 0, sn = son[V];
		getroot(V, 0), dfs(root);
	}
}
int main()
{
	int n, i, j, sum, x, y;
	while (scanf("%d%d", &n, &k) != EOF)
	{
		mx = 0;ans = 0;
		for (i = 0;i <= INF;i++) cnt[i] = 0;
		for (i = 1;i <= n;i++) v[i].clear(), vis[i] = 0;
		for (i = 0;i < k;i++) mx += (1 << i);
		for (i = 1;i <= n;i++) scanf("%d", &val[i]);
		for (i = 1;i < n;i++)
		{
			scanf("%d%d", &x, &y);
			v[x].push_back(y), v[y].push_back(x);
		}
		if (k == 1)
		{
			ll ans = (n*(n - 1)) + n;
			printf("%lld\n", ans);
			continue;
		}
		root = 0, sn = n, s[0] = 20000000;
		getroot(1, 0), dfs(root);
		printf("%lld\n", ans * 2);
	}
	return 0;
}


你可能感兴趣的:(HDU 5977 树分治+状态压缩)