Problem E. TeaTree Problem Description Recently, TeaTree acquire new knoledge gcd (Greatest Common Divisor), now she want to test you. As we know, TeaTree is a tree and her root is node 1, she have n nodes and n-1 edge, for each node i, it has it's v…
Problem E. Split The Tree Problem Description You are given a tree with n vertices, numbered from 1 to n. ith vertex has a value wi We define the weight of a tree as the number of different vertex value in the tree. If we delete one edge in the tree,…
Boring counting Problem Description In this problem we consider a rooted tree with N vertices. The vertices are numbered from 1 to N, and vertex 1 represents the root. There are integer weights on each vectice. Your task is to answer a list of querie…
Query on A Tree Problem Description Monkey A lives on a tree, he always plays on this tree. One day, monkey A learned about one of the bit-operations, xor. He was keen of this interesting operation and wanted to practise it at once. Monkey A gave a v…
题目: Invert Binary Tree Total Accepted: 20346 Total Submissions: 57084My Submissions Question Solution Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was inspired by this original tweet by Max Howell:…
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. 代码: /** * Definition for a bina…
题意比较难理解,就是给你n个点的树,然后给你m个修改操作,每一次修改包括一个点对(x, y),意味着将x到y所有的点权值加一,最后问你整个树上的点权最大是多少. 比较裸的树链剖分了,感谢Haild的讲解. 首先第一遍dfs预处理出size,son(重儿子). 第二遍dfs重编号. 然后线段树就可以了. 感觉就是把一棵树弄成一条一条的链,新奇的hash方法. #include <bits/stdc++.h> #define rep(i, a, b) for (int i = a; i <=…
[模板整合计划]图论-有向无环图 (DAG) 与树 一:[拓扑排序] 最大食物链计数 \(\text{[P4017]}\) #include<cstring> #include<cstdio> #include<queue> #define Re register int using namespace std; const int N=5003,M=5e5+3,inf=2e9,P=80112002; int n,m,x,y,o,ans,dp[N],ru[N],chu[…
[题意]给定n个点的树,每条边有一个小写字母a~v,求每棵子树内的最长回文路径,回文路径定义为路径上所有字母存在一种排列为回文串.n<=5*10^5. [算法]dsu on tree [题解]这题经典套路就是按照22个字母个数的奇偶性压位,然后两段路径异或起来是0或1<<j就是合法路径. dsu的时候每个点统计其子树内经过这个点的路径,注意包括从子树到该点终止的和该点自身也要算. 那么类似点分治的方式,算完重儿子后处理一下根,然后就一棵一棵轻儿子子树和之前的子树状态桶数组统计然后加入.…
[题目]E. Lomsat gelral [题意]给定n个点的树,1为根,每个点有一种颜色ci,一种颜色占领一棵子树当且仅当子树内没有颜色的出现次数超过它,求n个答案——每棵子树的占领颜色的编号和Σci(一棵子树可能有多种占领颜色).1<=n,ci<=10^5. [算法]dsu on tree [题解]入门题,讲一下dsu on tree算法. 一.dsu on tree的适用范围:①子树询问,②支持数组上的快速信息加,③不带修.(注意不需要支持信息删除,只需要能清空信息) 如果写暴力的时候,…
[题解]CF741D(DSU on TREE) 写一写这道题来学习学习模板 用二进制来转换一下条件,现在就是要求一下\(lowbit(x)=x\)的那些路径了. DSU on TREE 是这样一种算法: 像树剖一样分出轻重链,根据那套理论可知轻边\(O(\log n)\). 递归处理一个节点的所有轻儿子,并且回溯的时候将统计信息清空. 递归处理一个节点的那个重儿子,并且回溯的时候保留统计信息. 获得重儿子信息后,遍历一下所有轻儿子统计答案. 分析复杂度:对于每个点,可以被他父亲所有的轻边多遍历一…
dsu on tree的本质是树上的启发式合并,它利用启发式合并的思想,可以将O(N^2)的暴力优化成O(NlogN),用于不带修改的子树信息查询. 具体如何实现呢?对于一个节点,继承它重儿子的信息,轻儿子直接dfs统计,更新完本节点的答案后,再dfs一次清除轻儿子的信息,相当于一个启发式合并的过程,因为一次合并会使得被遍历的子树变大一倍,所以一棵子树最多遍历logn次,也就是一个点最多被遍历logn次,于是最劣复杂度为O(NlogN). dsu on tree的具体流程 dfs计算轻儿子的答案…