随笔分类 - 树
摘要:https://www.acwing.com/problem/content/1520/ 思路: 先找到在一个团伙中的人(可以用树的dfs来做,也可以用并查集来做),然后遍历每一个合法的集合,找到里面的头目。 并查集的做法 #include <iostream> #include <unordere
阅读全文
摘要:https://www.acwing.com/problem/content/description/1638/ 思路: LCA(爬山法)+重建一个树(知道其两个排序),由于时间n可能比较大,而且这题时间限制比较紧张,所以最后离散化,尽量查询不用哈希,因为哈希的常数太大了。 #include <io
阅读全文
摘要:https://www.acwing.com/problem/content/1625/ 思路: 注意这题dfs的写法,值得学习。 #include <iostream> using namespace std; const int N = 25; int n; int l[N], r[N]; st
阅读全文
摘要:https://www.acwing.com/problem/content/1630/ 思路: 思路不难,按照题目意思判断即可,但是这个dfs有些难写,值得学习,特记录此题。 #include <iostream> #include <algorithm> #include <unordered_
阅读全文
摘要:https://www.acwing.com/problem/content/1554/ 思路: 感觉这个左旋,右旋有些抽象,不好理解记忆,硬记也不好,当整个代码不算难写,因为很多部分都是堆成的。 #include <iostream> #include <algorithm> using name
阅读全文
摘要:https://www.acwing.com/problem/content/1611/ 思路: 由于n只有30,直接暴力枚举,虽说理论上限很高可能会超时,但是其实dfs过程中剪枝了许多。 关键在如何dfs,由于知道前序和后序,无法确定每次左右子树的长度,所以每次枚举左子树的长度,又因为总长一定,现
阅读全文

浙公网安备 33010602011771号