首页 > 其他分享 >cf1709E. XOR Tree(启发式合并入门)

cf1709E. XOR Tree(启发式合并入门)

时间:2023-11-05 21:14:13浏览次数:34  
标签:head XOR int Tree dfs cf1709E tot include define

cf1709E. XOR Tree
贪心是显然的,关键是如何合并两棵子树的信息,可以采用启发式合并。

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<map>
#include<vector>
#include<set>
#include<ctime>
#include<unordered_map>
#define A puts("YES")
#define B puts("NO")
#define fo(i,a,b) for (ll (i)=(a);(i)<=(b);(i)++)
#define fd(i,b,a) for (int (i)=(b);(i)>=(a);(i)--)
#define mk(x,y) make_pair((x),(y))
#define lc (o<<1)
#define rc (o<<1|1)
using namespace std;
//typedef __int128 i128;
typedef double db;
typedef long long ll;
const ll inf=1ll<<60;
const int N=2e5+5;
int n,a[N],x,y,b[N];
int head[N],to[N*2],nex[N*2],tot,ans;
set<int> c[N];
void add(int x,int y){
	to[++tot]=y; nex[tot]=head[x]; head[x]=tot;
}
void dfs(int x,int y){
	b[x]^=a[x];
	for (int i=head[x];i;i=nex[i]){
		int v=to[i];
		if (v==y) continue;
		b[v]^=b[x];
		dfs(v,x);
	}
}
void calc(int x,int y){
	c[x].insert(b[x]);
	bool flag=0;
	for (int i=head[x];i;i=nex[i]){
		int v=to[i];
		if (v==y) continue;
		calc(v,x);
		
		if (c[x].size() < c[v].size()) c[x].swap(c[v]);
		for (auto j:c[v]) {
			if (c[x].find(j^a[x])!=c[x].end()) {  
				flag=1;
			}
		}
		
		for (auto j:c[v]) {
			c[x].insert(j);
		}
	}
	
	if (flag) {
		ans++;
		c[x].clear();
	}
}
int main()
{
//	freopen("data.in","r",stdin);
//	freopen("data.out","w",stdout);

	scanf("%d",&n);
	fo(i,1,n) scanf("%d",&a[i]);
	fo(i,1,n-1) {
		scanf("%d %d",&x,&y);
		add(x,y); add(y,x);
	}

	dfs(1,0);
	
	calc(1,0);
	
	printf("%d",ans);
	
	return 0;
}

 
 

标签:head,XOR,int,Tree,dfs,cf1709E,tot,include,define
From: https://www.cnblogs.com/ganking/p/17811183.html

相关文章

  • Binary Tree Level Order Traversal
    SourceGivenabinarytree,returnthelevelordertraversalofitsnodes'values.(ie,fromlefttoright,levelbylevel).ExampleGivenbinarytree{3,9,20,#,#,15,7},3/\920/\157returnitslevelordertraversala......
  • 【刷题笔记】100. Same Tree
    题目Giventwobinarytrees,writeafunctiontocheckiftheyarethesameornot.Twobinarytreesareconsideredthesameiftheyarestructurallyidenticalandthenodeshavethesamevalue.Example1:Input:11/\/\......
  • Educational Codeforces Round 157 (Rated for Div. 2) D. XOR Construction
    题目链接题意给你\(n-1\)个整数\(a_1,a_2,\dots,a_{n-1}\)。你的任务是构造一个数组\(b_1,b_2,\dots,b_n\),使得:从\(0\)到\(n-1\)的每个整数都在\(b\)中出现一次;对于从\(1\)到\(n-1\)的每个\(i\),\(b_i\oplusb_{i+1}=a_i\)(其中\(\oplus\)表示......
  • 【刷题笔记】98. Validate Binary Search Tree
    题目Givenabinarytree,determineifitisavalidbinarysearchtree(BST).AssumeaBSTisdefinedasfollows:Theleftsubtreeofanodecontainsonlynodeswithkeys lessthan thenode'skey.Therightsubtreeofanodecontainsonlynodeswith......
  • [ARC122D] XOR Game
    ProblemStatementThereare$2N$integerswrittenonablackboard.The$i$-thintegeris$A_i$.AliceandBobwillplayagameconsistingof$N$rounds.Ineachround,theydothefollowing:First,Alicechoosesanintegerontheblackboardanderasesit.......
  • Oracle中B-tree索引的访问方法(一)-- 索引逻辑结构
    B-tree索引的逻辑结构1.1B-tree索引依据不同的维度,我们可以对索引进行相应的分类。比如,根据索引键值是否允许有重复值,可以分为唯一索引和非唯一索引;根据索引是由单个列,还是由多个列构成,又可以分为单列索引和组合索引(也称之为联合索引);而从索引的数据组织结构上来分类,则最常见的是B-......
  • A. Copil Copac Draws Trees
    A.CopilCopacDrawsTrees题目大意:给出一个树边序列,要求你从1号节点建树,对于每条边只有两个端点中有一个绘制了才可以绘制此边思路:这题思路不难,但以前写图太少,遍历被卡,给每个边按序列编号,dfs如果该边的编号大于上条边\(ans++\)code:intn;vector<pii>a[N];intans[N]=......
  • vim 的nerdtree插件中如何显示当前打开的文件路径?
    树形目录nerdtree插件中如何显示当前打开的文件路径?类似这样:只需在.vimrc文件中加入下面3行就可以了"设置NerdTreemap<F3>:NERDTreeMirror<CR>map<F3>:NERDTreeToggle<CR>map<leader>r:NERDTreeFind<cr>......
  • TreeMap
    TreeMap是Map家族中的一员,也是用来存放key-value键值对的。平时在工作中使用的可能并不多,它最大的特点是遍历时是有顺序的,根据key的排序规则来TreeMap是一个双列集合,是Map的子类。底层由红黑树结构构成。TreeMap是一个基于key有序的keyvalue散列表。map根据其键的自然顺序排......
  • CF1039D You Are Given a Tree
    CF1039DYouAreGivenaTree更好的阅读体验一种神奇套路:对答案根分,根分的依据是链的长度和答案大致是一个成反比的关系。考虑确定了\(k\)怎么做。因为一个点只能在一条链里,所以dfs的时候如果能拼成一条链就一定会拼成一条链,不然就把贡献传给父亲继续尝试。对于\(k\)比......