Problem F. Grab The Tree HDU - 6324(树形dp+博弈)

Little Q and Little T are playing a game on a tree. There are nn vertices on the tree, labeled by 1,2,…,n1,2,…,n, connected by n−1n−1 bidirectional edges. The ii-th vertex has the value of wiwi.
In this game, Little Q needs to grab some vertices on the tree. He can select any number of vertices to grab, but he is not allowed to grab both vertices that are adjacent on the tree. That is, if there is an edge between xx and yy, he can’t grab both xx and yy. After Q’s move, Little T will grab all of the rest vertices. So when the game finishes, every vertex will be occupied by either Q or T.
The final score of each player is the bitwise XOR sum of his choosen vertices’ value. The one who has the higher score will win the game. It is also possible for the game to end in a draw. Assume they all will play optimally, please write a program to predict the result.
Input
The first line of the input contains an integer T(1≤T≤20)T(1≤T≤20), denoting the number of test cases.
In each test case, there is one integer n(1≤n≤100000)n(1≤n≤100000) in the first line, denoting the number of vertices.
In the next line, there are nn integers w1,w2,…,wn(1≤wi≤109)w1,w2,…,wn(1≤wi≤109), denoting the value of each vertex.
For the next n−1n−1 lines, each line contains two integers uu and vv, denoting a bidirectional edge between vertex uu and vv.
Output
For each test case, print a single line containing a word, denoting the result. If Q wins, please print Q. If T wins, please print T. And if the game ends in a draw, please print D.
Sample Input
1
3
2 2 2
1 2
1 3
Sample Output
Q
在树上选取点求异或和,Q先选,剩下的点就是T选的了。问最后谁会赢。
树形dp,dp[i][0/1]代表着这个节点选还是不选。
状态转移方程:
dp[u][1]=max(dp[u][1],dp[u][1]^dp[to][0]);
dp[u][0]=max(dp[u][0],max(dp[u][0] ^ dp[to][1],dp[u][0]^dp[to][0]));
树形dp完了之后,选取dp[1][1]和dp[1][0]之间最大的那一个作为Q的得分,剩下的就是T的得分了,然后作比较就好了。
代码如下:

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;

const int maxx=1e5+100;
struct edge{
	int to,next;
}e[maxx<<1];
int head[maxx<<1];
int a[maxx];
int dp[maxx][2];
int n,tot;
/*-----------事前准备-----------*/
inline void init()
{
	tot=0;
	memset(head,-1,sizeof(head));
	memset(dp,0,sizeof(dp));
}
inline void add(int u,int v)
{
	e[tot].to=v,e[tot].next=head[u],head[u]=tot++;
}
/*---------------dfs--------------*/
inline void dfs(int u,int f)
{
	for(int i=head[u];i!=-1;i=e[i].next)
	{
		int to=e[i].to;
		if(to==f) continue;
		dfs(to,u);
		dp[u][1]=max(dp[u][1],dp[u][1]^dp[to][0]);
		dp[u][0]=max(dp[u][0],max(dp[u][0]^dp[to][1],dp[u][0]^dp[to][0]));
	}
	dp[u][1]^=a[u];
}
int main()
{
	int t,l,r;
	scanf("%d",&t);
	while(t--)
	{
		init();
		scanf("%d",&n);
		for(int i=1;i<=n;i++) scanf("%d",&a[i]);
		for(int i=1;i<n;i++)
		{
			scanf("%d%d",&l,&r);
			add(l,r);
			add(r,l);
		}
		memset(dp,inf,sizeof(dp));
		dfs(1,0);
		int ant=max(dp[1][0],dp[1][1]);
		int sum=0;
		for(int i=1;i<=n;i++) sum^=a[i];
		sum^=ant;//先把所有节点的值的异或和求出来,再异或上Q的,就是T的。因为^的逆运算就是^.
		if(sum==ant) puts("D");
		else if(sum>ant) puts("T");
		else puts("Q");
	}
	return 0;
}

努力加油a啊,(o)/~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
资源包主要包含以下内容: ASP项目源码:每个资源包中都包含完整的ASP项目源码,这些源码采用了经典的ASP技术开发,结构清晰、注释详细,帮助用户轻松理解整个项目的逻辑和实现方式。通过这些源码,用户可以学习到ASP的基本语法、服务器端脚本编写方法、数据库操作、用户权限管理等关键技术。 数据库设计文件:为了方便用户更好地理解系统的后台逻辑,每个项目中都附带了完整的数据库设计文件。这些文件通常包括数据库结构图、数据表设计文档,以及示例数据SQL脚本。用户可以通过这些文件快速搭建项目所需的数据库环境,并了解各个数据表之间的关系和作用。 详细的开发文档:每个资源包都附有详细的开发文档,文档内容包括项目背景介绍、功能模块说明、系统流程图、用户界面设计以及关键代码解析等。这些文档为用户提供了深入的学习材料,使得即便是从零开始的开发者也能逐步掌握项目开发的全过程。 项目演示与使用指南:为帮助用户更好地理解和使用这些ASP项目,每个资源包中都包含项目的演示文件和使用指南。演示文件通常以视频或图文形式展示项目的主要功能和操作流程,使用指南则详细说明了如何配置开发环境、部署项目以及常见问题的解决方法。 毕业设计参考:对于正在准备毕业设计的学生来说,这些资源包是绝佳的参考材料。每个项目不仅功能完善、结构清晰,还符合常见的毕业设计要求和标准。通过这些项目,学生可以学习到如何从零开始构建一个完整的Web系统,并积累丰富的项目经验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

starlet_kiss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值