题解:CF593D Happy Tree Party】的更多相关文章

题解:CF593D Happy Tree Party Description Bogdan has a birthday today and mom gave him a tree consisting of \(n\) vertecies. For every edge of the tree \(i\) , some number \(x_i\) was written on it. In case you forget, a tree is a connected non-directed…
[题解]Digit Tree CodeForces - 716E 呵呵以为是数据结构题然后是淀粉质还行... 题目就是给你一颗有边权的树,问你有多少路径,把路径上的数字顺次写出来,是\(m\)的倍数. 很明显可以点分治嘛,我们可以按照图上的样子,把一条路径本来是\(12345678\)的路径,变成\(1234|5678\),我们记录图中左边的那种路径为\(f\)(往根),右边的那种路径为\(g\)(从根),记右边的那种到分治中心的深度为\(d\),那么这条路径就可以被表示成\(f\times 1…
[题解]P4178 Tree 一道点分治模板好题 不知道是不是我见到的题目太少了,为什么这种题目都是暴力开值域的桶QAQ?? 问点对,考虑点分治吧.直接用值域树状数组开下来,统计的时候直接往树状数组里面查询.记得每一层先把这一层的答案统计一下,统计的方法就是刚刚讲的在桶里查. 问题是回溯,值域不大,所以常数还可以,但是我们最好还是开个\(temp\)把我们做修改的地方记录一下,在\(calc\)返回的时候直接回溯. 时间复杂度\(nlog^2n\) 有一些细节需要注意.比如要把\(d[]\)的先…
题目: Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 说明:1)下面有两种实现:递归(Recursive )与非递归(迭代iterati…
Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / \ 2 5 / \ \ 3 4 6 The flattened tree should look like: 1 \ 2 \ 3 \ 4 \ 5 \ 6 Hints: If you notice carefully in the flattened tree, each node's right child points to the…
题目: Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 说明: 1)二叉树可空 2)思路:a.根据前序遍历的特点, 知前序序列(PreSequence)的首个元素(PreSequence[0])为二叉树的根(root),  然后在中序序列(InSequence)中查找此根(…
题面 题解 我们发现,对于除法有效的xi最小为2,yi最多除log次就会变成0,所以我们可以每次找路径上下一个>=2的xi,暴力除,当发现y=0时就停止 于是我们维护每个点向上走一直走到根最近的一条数字大于1的边,存下该边的下端点,每当有一条边数字大于1,就要更新它的下端点子树中每一个的最近边,这个可以把点按照dfs序排序后用线段树做区间修改,单点查询 但是题目中的修改操作很特殊,每次会将一条边上的数字变小,后面一旦变为1了就不好维护,所以我决定倒着来(感性理解,把进度条从后往前拖),先把操作输…
1.题目描述 2.问题分析 使用层序遍历 3.代码 vector<int> v; vector<int> rightSideView(TreeNode* root) { if (root == NULL) return v; queue<TreeNode*> q; q.push(root); while (!q.empty()) { int size = q.size(); ; i < size; i++) { TreeNode *node = q.front()…
1.题目描述 2.问题分析 使用递归 3.代码 TreeNode* pruneTree(TreeNode* root) { if (root == NULL) return NULL; prun(root); return root; } void prun(TreeNode *root) { if (root == NULL) return; if (!has_ones(root->left)) root->left = NULL; if (!has_ones(root->right)…
1.题目描述 2.分析 找出最大元素,然后分割数组调用. 3.代码 TreeNode* constructMaximumBinaryTree(vector<int>& nums) { int size = nums.size(); ) return NULL; TreeNode *dummy = ); maxcont(dummy->left, , size-, nums); return dummy->left; } void maxcont(TreeNode* &…
1.题目描述 2.分析 使用递归. 3.代码 vector<string> ans; vector<string> binaryTreePaths(TreeNode* root) { if (root == NULL) return ans; leafPath(root,""); return ans; } void leafPath(TreeNode *root, string s) { if (root == NULL) return ; if (root-…
1.题目描述 2.题目分析 先遍历,再反转. 3.代码 vector<vector<int>> levelOrderBottom(TreeNode* root) { vector<vector<int>> ans; if (root == NULL) return ans; queue<TreeNode*> q; q.push(root); vector<int> v; while (!q.empty()) { int size =…
1.题目描述 2.分析 利用递归实现. 3.代码 int findTilt(TreeNode* root) { if (root == NULL) ; ; nodesTilt(root,ans); return ans; } int nodesTilt(TreeNode *root, int & ans) { if (root == NULL) ; int tiltleft = nodesTilt(root->left, ans); int tiltright = nodesTilt(roo…
1.题目描述 2.问题分析 利用先进先出队列解决问题. 3.代码 vector<vector<int>> levelOrder(Node* root) { vector<vector<int>> v; if (root == NULL) return v; queue<Node*> q; q.push(root); vector<int> v1; while (!q.empty()) { int size = q.size(); ;…
1.题目描述 2.问题分析 递归 3.代码 vector<int> postorderTraversal(TreeNode* root) { vector<int> v; postBorder(root,v); return v; } void postBorder(TreeNode *root, vector<int> &v) { if (root == NULL) return ; postBorder(root->left, v); postBord…
1.题目描述 2.问题分析 递归. 3.代码 vector<int> postorder(Node* root) { vector<int> v; postNorder(root, v); return v; } void postNorder(Node *root, vector<int> &v) { if (root == NULL) return; for (auto it = root->children.begin(); it != root-&…
1.题目描述 2.问题分析 利用递归. 3.代码 vector<int> preorderTraversal(TreeNode* root) { vector<int> v; preBorder(root, v); return v; } void preBorder(TreeNode *root, vector<int> &v) { if (root == NULL) return ; v.push_back(root->val); preBorder(…
1.题目描述 2.问题分析 采用递归方法是标准解法. 3.代码 vector<int> preorder(Node* root) { vector<int> v; preNorder(root, v); return v; } void preNorder(Node *root , vector<int> &v) { if (root == NULL) return ; v.push_back(root->val); for (auto it = root…
首先,子树上的查询问题可以通过$DFS$序转为序列问题 再一看,没有修改,可以离线,这不就是莫队吗? 我们用$sum_i$表示出现次数$\geq i$的个数 用$val_i$表示第$i$种颜色的出现次数 那么每次修改时只要$O(1)$修改$sum$和$val$即可 详见代码 #include <bits/stdc++.h> ; struct node { int val, dfn, r, id; }; struct query { int l, r; int pos, id, k; }; st…
Link \(\text{Solution:}\) 讲实话这题有点烦,不知道为啥改了下\(\text{dfs}\)就过了--原版本\(dfs\)好像没啥错啊-- 其实对于子树问题,我们求出原来树的\(dfs\)序列,则可以将它转化为一个序列问题.注意题目中说的是有根树,以\(1\)为根. 那么,我们一遍\(dfs\)求出序列后,把它插到询问里面,即更新为原序列. 注意,我们对应\(dfs\)序并不是原来的点,所以还需要一个数组\(rk\)维护映射\(point\to dfn\). 那么,对于维护…
原题链接在这里:https://leetcode.com/problems/count-complete-tree-nodes/ Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia:In a complete binary tree every level, except possibly the last, is complete…
[CF600E]Lomsat gelral(dsu on tree) 题面 洛谷 CF题面自己去找找吧. 题解 \(dsu\ on\ tree\)板子题 其实就是做子树询问的一个较快的方法. 对于子树的询问,我们不难想到子树就是\(dfs\)序上的连续一段, 可以把树转化成序列再用莫队来解. 其实可以对树进行树链剖分,然后暴力+优化来解 具体的做法就是: 递归处理轻儿子,计算轻儿子答案, 然后消去轻儿子对于答案的影响. 然后递归处理重儿子,不消去影响,最后加入所有轻儿子贡献,计算答案. 复杂度?…
比赛时候还是太慢了……要是能做快点就能上分了 Monocarp has drawn a tree (an undirected connected acyclic graph) and then has given each vertex an index. All indices are distinct numbers from 11 to nn. For every edge ee of this tree, Monocarp has written two numbers: the ma…
二叉树基础 满足这样性质的树称为二叉树:空树或节点最多有两个子树,称为左子树.右子树, 左右子树节点同样最多有两个子树. 二叉树是递归定义的,因而常用递归/DFS的思想处理二叉树相关问题,例如LeetCode题目 104. Maximum Depth of Binary Tree: // 104. Maximum Depth of Binary Tree int maxDepth(TreeNode* root) { ; +max(maxDepth(root->left),maxDepth(roo…
1. 遗传编程简介 0x1:什么是遗传编程算法,和传统机器学习算法有什么区别 传统上,我们接触的机器学习算法,都是被设计为解决某一个某一类问题的确定性算法.对于这些机器学习算法来说,唯一的灵活性体现在参数搜索空间上,向算法输入样本,算法借助不同的优化手段,对参数进行调整,以此来得到一个对训练样本和测试样本的最佳适配参数组. 遗传编程算法完全走了另一外一条路,遗传编程算法的目标是编写一个程度,这个程序会尝试自动构造出解决某一问题的最佳程度.从本质上看,遗传编程算法构造的是一个能够构造算法的算法.…
[Submit][Status][Discuss] Description 请写一个程序,要求维护一个数列,支持以下 6 种操作: 请注意,格式栏 中的下划线‘ _ ’表示实际输入文件中的空格 Input 输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目.第2行包含N个数字,描述初始时的数列.以下M行,每行一条命令,格式参见问题描述中的表格.任何时刻数列中最多含有500 000个数,数列中任何一个数字均在[-1 000, 1 000]内…
day 1:  result:    sum_rank: 11   school_rank:1   水题在你高估的时候就已经不水了   sum:有个快速乘类似快速幂:       int ans=0;      while(y) { if(y&1)ans=(ans+x)%P: y>>=1; x=(x<<1)%P; } rest: sum cactusday 2:  result:    sum_rank: 18   school_rank:6 beetle:甲虫要离散,特殊…
原题链接在这里:https://leetcode.com/problems/minimum-absolute-difference-in-bst/#/description 题目: Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. Example: Input: 1 \ 3 / 2 Output: 1…
BFS基础 广度优先搜索(Breadth First Search)用于按离始节点距离.由近到远渐次访问图的节点,可视化BFS 通常使用队列(queue)结构模拟BFS过程,关于queue见:算法与数据结构基础 - 队列(Queue) 最直观的BFS应用是图和树的遍历,其中图常用邻接表或矩阵表示,例如 LeetCode题目 690. Employee Importance: // LeetCode 690. Employee Importance/* class Employee { publi…
DFS基础 深度优先搜索(Depth First Search)是一种搜索思路,相比广度优先搜索(BFS),DFS对每一个分枝路径深入到不能再深入为止,其应用于树/图的遍历.嵌套关系处理.回溯等,可以用递归.堆栈(stack)实现DFS过程. 关于广度优先搜索(BFS)详见:算法与数据结构基础 - 广度优先搜索(BFS) 关于递归(Recursion)详见:算法与数据结构基础 - 递归(Recursion) 树的遍历 DFS常用于二叉树的遍历,关于二叉树详见: 算法与数据结构基础 - 二叉查找树…