HDU 3094 A tree game 树的删边游戏

叶子节点的SG值为0 非叶子节点的SG值为为它的所有子节点的SG值加1 后的异或和

#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
vector <int> G[100010];
int sg[100010];
int dfs(int x, int f)
{
	if(sg[x] != -1)
		return sg[x];
	if(!G[x].size())
		return 0;
	int ans = 0;
	for(int i = 0; i < G[x].size(); i++)
	{
		int v = G[x][i];
		if(v == f)
			continue;
	ans ^= dfs(v, x)+1;
	}
	return ans;
}
int main()
{
	int T;
	scanf("%d", &T);
	while(T--)
	{
		memset(sg, -1, sizeof(sg));
		int n;
		scanf("%d", &n);
		for(int i = 1; i <= n; i++)
			G[i].clear();
		for(int i = 1; i < n; i++)
		{
			int u, v;
			scanf("%d %d", &u, &v);
			G[u].push_back(v);
			G[v].push_back(u);
		}
		int ans = dfs(1, -1);
		if(ans)
			puts("Alice");
		else
			puts("Bob");
	}
	return 0;
}
时间: 2024-09-15 12:00:24

HDU 3094 A tree game 树的删边游戏的相关文章

HDU 3094 A tree game 树删边游戏

叶节点SG值至0 非叶节点SG值至于它的所有子节点SG值添加1 XOR和后 #include <cstdio> #include <cstring> #include <vector> using namespace std; vector <int> G[100010]; int sg[100010]; int dfs(int x, int f) { if(sg[x] != -1) return sg[x]; if(!G[x].size()) return

hdu 1754 splay tree伸展树 初战(单点更新,区间属性查询)

题意:与区间查询点更新,点有20W个,询问区间的最大值.曾经用线段树,1000+ms,今天的伸展树,890没ms,差不多. 第一次学习伸展树,一共花了2个单位时间,感觉伸展树真很有用,也很好玩.现在只学了一点点.切个点更新试试. 大致思路:用编号(数组)作为树的键值建树,每插一个数,沿路节点更新最大值(每个结点有一个附加信息标记以之为子树的树所有点的最大值).所以,查询时[i,j],只要把i-1伸展到树根,把j+1伸展到I-1下面,那么j+1的左子树就是要的区间了!查该子树根值信息即可(特判端点

hdu 3094 A tree game

http://acm.hdu.edu.cn/showproblem.php?pid=3094 树上删边游戏 一条链的情况:SG分别是0,1,2,……,相当于Nim取石子游戏 那么把边看作石子,树可看做若干堆石子 所以叶节点的SG=0,其余节点的SG等于子节点SG+1的异或和 #include<cstdio> #include<cstring> using namespace std; #define N 100001 int tot,front[N],to[N<<1],

hdu 3333 Turing Tree(线段树)

题目链接:hdu 3333 Turing Tree 题目大意:给定一个长度为N的序列,有M次查询,每次查询l,r之间元素的总和,相同元素只算一次. 解题思路:涨姿势了,线段树的一种题型,离线操作,将查询按照右区间排序,每次考虑一个询问,将mv ~ r的点全部标记为存在,并且对于每个位置i,如果A[i]在前面已经出现过了,那么将前面的那个位置减掉A[i],当前位置添加A[i],这样做维护了每个数尽量做,那么碰到查询,用sum[r] - sum[l-1]即可. #include <cstdio>

HDU 3333 Turing Tree(树状数组离线处理)

HDU 3333 Turing Tree 题目链接 题意:给定一个数组,每次询问一个区间,求出这个区间不同数字的和 思路:树状数组离线处理,把询问按右端点判序,然后用一个map记录下每个数字最右出现的位置,因为一个数字在最右边出现,左边那些数字等于没用了,利用树状数组进行单点修改区间查询即可 代码: #include <cstdio> #include <cstring> #include <algorithm> #include <map> using n

HDU 3333 Turing Tree (主席树)

题意:给定上一个序列,然后有一些询问,求区间 l - r 中有多少个不同的数的和. 析:和求区间不同数的方法是一样,只要用主席树维护就好. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iost

HDU 3333 Turing Tree(树状数组 || 线段树)

题意:给定一个区间,q个查询,对于每次查询回答这个区间内所有不重复的数的和. 思路:可以考虑使用树状数组来做. 先读入所有查询,离线来做,将所有查询按右端点升序排序. 那么我们从给定区间的第一个元素开始遍历这个区间,在此过程中更新每一个元素上一次出现的位置,每次将现在位置加上a[i]并将lastpos位置减去a[i], 也就是说,我们每一步都是保留与当前位置距离最近的重复元素值,其余置零,那么这样肯定能保证答案正确, 如果遍历到的这个位置恰好是一个询问的右端点,那么我们输出修改后的区间值之和.

codeforces gym #101161F-Dictionary Game(字典树+树上删边游戏)

题目链接: http://codeforces.com/gym/101161/attachments 题意: 给一个可以变化的字典树 在字典树上删边 如果某条边和根节点不连通那么这条边也删除 谁没得删就输了 数据范围: $1\leq n \leq 100000$ $1\leq q \leq 100000$ $1\leq |s| \leq 40$ 分析: ac代码: #include<bits/stdc++.h> using namespace std; #define ll long long

HDU 4718 The LCIS on the Tree(树链剖分)

Problem Description For a sequence S1, S2, ... , SN, and a pair of integers (i, j), if 1 <= i <= j <= N and Si < Si+1 < Si+2 < ... < Sj-1 < Sj , then the sequence Si, Si+1, ... , Sj is a CIS(Continuous Increasing Subsequence). The