C. Ilya And The Tree 题意 给一棵树求每个点到根的路上允许修改一个为0,gcd的最大值. 题解 g是从根到当前点允许修改的最大gcd,gs为不修改的最大gcd.枚举当前点的因子,更新路径上每个因子出现次数,回溯时减去.并用这个因子更新答案.另外当前点修改为0时,还要用父节点的gs更新答案.复杂度\(O(n\sqrt n)\) 代码 const int N=201000; int n; int a[N]; int u,v; VI e[N]; int g[N],gs[N]; in…
地址:http://codeforces.com/contest/842/problem/C 题目: C. Ilya And The Tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Ilya is very fond of graphs, especially trees. During his last trip…
C. Ilya And The Tree 写法还是比较容易想到,但是这么暴力的写法不是那么的敢写. 就直接枚举了每一个点上面的点的所有的情况,对于这个点不放进去特判一下,然后排序去重提高效率. 注意dp[v]一开始存的是从根节点到这个节点都选的情况,这样才好往后转移. #include <iostream> #include <cstdio> #include <algorithm> #include <vector> #include <cstrin…
Ilya is very fond of graphs, especially trees. During his last trip to the forest Ilya found a very interesting tree rooted at vertex 1. There is an integer number written on each vertex of the tree; the number written on vertex i is equal to ai. Ily…
Ilya is very fond of graphs, especially trees. During his last trip to the forest Ilya found a very interesting tree rooted at vertex 1. There is an integer number written on each vertex of the tree; the number written on vertex i is equal to ai. Ily…
A. Kirill And The Game time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Kirill plays a new computer game. He came to the potion store where he can buy any potion. Each potion is characteriz…
Codeforces 686 D. Kay and Snowflake 要求$O(n)$求出以每个节点为根的重心. 考虑对于一个根节点$u$,其重心一定在[各个子树的重心到$u$]这条链上.这样就能够$O(n)$推出来了.证明起来难证易忘.不如记住树的重心的几条奇妙性质: 1. 以重心为根,各子树大小都不超过树的一半. 2. 已知一子树$v$的重心为$x$,则$v$父节点$u$的重心一定在$(x,u)$这条链上. ... 下次用到了再说 Codeforces 842 C. Ilya And Th…
在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,000 Things You Should Know About WPF .他以类似微博式的150字简短语言来每天更新一条WPF和C#重要又容易被遗忘的知识.Follow他的博客也有一段日子了,很希望能够分享给大家. 本系列我不仅会翻译他的每一个tip,也会加入自己开发之中的看法和见解.本系列我希望自…
题目链接:Balanced Binary Tree | LeetCode OJ 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 tha…
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value. Tags: Depth-first Search 分析 很基本的一道深度优…
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straight forward. Could you devise a constan…
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right…
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \ 3 4 4 3 But the following is not: 1 / \ 2 2 \ \ 3 3 Note: B…