1531 树上的博弈

51nod  1531 树上的博弈


树型dp

1. 建树,复杂度O(n)

2. 自下向上dp, 复杂度O(n)


记录状态量:   当前该棵子树 分配m个数,答案是第k小数  。       进行状态转移

由于有先后手,并且有 分配希望 最大与最小 两种前提。因此2*2=4种状态,不过最大最小是对称关系,因此可以只计算其中2种,另2种根据对称求解。


转移方程草稿:


#include<iostream>

using namespace std;

#define maxlen 200001
#define result_len 4
int *c[maxlen];//[5]
int x[maxlen], y[maxlen];
int myarray[maxlen];
bool is_visited[maxlen];
int result[maxlen][result_len];
int tree_size[maxlen];
int friend_num[maxlen];
int n, root;

void getInput()
{
	scanf("%d", &n);

	for (int i = 1; i < n; ++i)
	{
		scanf("%d%d", &x[i], &y[i]);
		++friend_num[x[i]];
		++friend_num[y[i]];
	}

	for (int i = 1; i <= n; ++i)
	{
		c[i] = new int[friend_num[i] + 1];
		c[i][0] = 0;
		result[i][0] =  result[i][3] = n;
		result[i][1] = result[i][2] = 0;
	}

	for (int i = 1; i < n; ++i)
	{
		c[x[i]][++c[x[i]][0]] = y[i];
		c[y[i]][++c[y[i]][0]] = x[i];
	}

}

int max(int x, int y)
{
	if (x < y)
	{
		return y;
	}
	return x;
}

void dp(int x)
{
	//	int num = c[x][0] - 1, left_child, right_child;
	int father_index = c[x][c[x][0]];

	result[x][0] = tree_size[x] - result[x][0];
	result[x][2] -= (c[x][0] - 2);
//	cout << c[x][0] << '\t' << result[x][2] << endl;
//	cout << x << ':' <<tree_size[x]<<'\t'<< result[x][0] << '\t' << result[x][1] << '\t' << result[x][2] << '\t' << result[x][3] <<'\t'<< father_index<<endl;

	tree_size[father_index] += tree_size[x];

	if (tree_size[x] - result[x][2] < result[father_index][0])
	{
		result[father_index][0] = tree_size[x] - result[x][2];
//		cout << result[father_index][0]<<endl;
	}
	result[father_index][1] += result[x][3];
	result[father_index][2] += result[x][0];

	if (result[x][1] < result[father_index][3])
	{
		result[father_index][3] = result[x][1];
	}

;

}


//建立树,时间复杂度O(n),n为树节点个数。
//myarray数组存储的是拓扑排序过程的节点序列,可以后续利用
//若是根节点可能为0或负数,要用另一种写法

void buildTree()
{
	const int father_num = 1, root_father = 0; //root_father也可以设为n+1
	//找根root的过程,可以自己设定
	root = 1;
	int num = 0;

	for (int i = 2; i <= n; ++i)
	{
		//		cout << c[i][0] <<'\t';
		if (1 == c[i][0])
		{
			myarray[num++] = i;
			tree_size[i] = 1;
			result[i][1] = result[i][3] = 1;
			result[i][0] = result[i][2] = 0;
			is_visited[i] = true;
			//cout << "vistit" << i << endl;
		}
/*		else if (c[i][0] > c[root][0])		//把孩子最多的节点作为root
		{
			root = i;
		}*/
	}
	/*	if (0 == root)
	{
	root = myarray[--num];
	is_visited[root] = false;
	}*/
	++friend_num[root];
	c[root][++c[root][0]] = root_father;			//为了计算方便,将0作为根节点的父节点

	//	c[root][0] = c[root][0] * 2 + 1;


	//	cout << "根"<<root <<'\t'<<num<< endl;

	//利用叶子节点度为1的思想,进行拓扑排序,建立树,最终c[i][0]存储 度、边数,  1-c[i][0]为所有相邻点, 最后一个c[i][c[i][0]]为父节点
	//根节点的父节点为0,也可以自己设定。

	int index, tmp_father, index_begin = 0;
	while (index_begin < num)
	{
		index = myarray[index_begin++];
		//cout << num << '\t' << index << '\t' << c[index][0] <<'\t'<<c[index][1]<<'\t'<<c[index][tmp]<< endl;
		for (int i = 1; i <= c[index][0]; ++i)
		{
			if (!is_visited[tmp_father = c[index][i]])
			{
				if ((--friend_num[tmp_father]) == father_num)		//删减index,若之后父节点入度为0,则入队列
				{
					myarray[num++] = tmp_father;
					is_visited[tmp_father] = true;
					//	cout <<index<< "vistit" << tmp_father << endl;
				}
				c[index][i] = c[index][c[index][0]];			//father移至最后
				c[index][c[index][0]] = tmp_father;
				break;
			}
		}
		dp(index);
	}

	if (n == 1)
	{
		tree_size[root] = 1;
		result[root][1] = result[root][3] = 1;
		result[root][0] = result[root][2] = 0;
		dp(root);
	}

	//从根节点开始递推
/*	for (int i = num - 2; i >= 0; --i)
	{
		index = myarray[i];
	//	result[index] = result[c[index][c[index][0]]] + n - (tree_size[index] << 1);
	}*/

}

void test()
{
/*	for (int i = 1; i <= n; ++i)
	{
		printf("%lld\n", result[i]);
		}*/
//	cout << x << ':' << tree_size[x] << '\t' << result[x][0] << '\t' << result[root][1] << '\t' << result[x][2] << '\t' << result[x][3] << '\t'  << endl;
//	cout << root << endl;
	printf("%d %d %d %d %d\n", tree_size[root], result[root][0], result[root][1], result[root][2], result[root][3]);
}

int main()
{
	getInput();
	buildTree();
	test();
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值