ACM-Divide Tree】的更多相关文章

Divide Tree(0785) 问题描述 As we all know that we can consider a tree as a graph. Now give you a tree with nodes having its weight. We define the weight of a tree is the sum of the weight of all nodes on it. You know we can divide the tree into two subtr…
动态树问题是指的一类问题,而不是具体指的某一种数据结构.它主要维护一个包含若干有根树的森林,实现对森林的修改和查询等. 实现动态树的数据结构据说主要有4种,Link-Cut Tree是其中的一种.Link-Cut Tree可以看作是所求森林的一个映射,二者的映射关系将在后面讲述. 先说Link-Cut Tree支持的基本操作包含: 1.(access)Link-Cut Tree的核心操作,后面的操作都需要access这个操作的支持. 2.(make)在森林中建立一棵树. 3.(findroot)…
题目描述:Divide Tree   As we all know that we can consider a tree as a graph. Now give you a tree with nodes having its weight. We define the weight of a tree is the sum of the weight of all nodes on it. You know we can divide the tree into two subtrees…
病毒侵袭持续中 HDOJ-3065 第一个需要注意的是树节点的个数也就是tree的第一维需要的空间是多少:模板串的个数*最长模板串的长度 一开始我的答案总时WA,原因是我的方法一开始不是这样做的,我是在查找文本串的时候,结束的时候再来统计每个模板串出现的次数,但是这样似乎不行 这道题还有一个坑就是输入是多组数据... //AC自动机,复杂度为O(|t|+m),t表示文本串的长度,m表示模板串的个数 #include<iostream> #include<cstring> #incl…
病毒侵袭 HDOJ-2896 主要使用AC自动机解决,其次在query函数中改变一下,用来记录每个模板串出现的次数,还有insert函数中记录模板串的编号 需要注意最好使用结构体,而且不能一次性使用memset否则会超时 上次没有AC出现了output limit exceed问题,后来发现是我的num数组开的太小了,只开了505,实际上需要N的空间. 还有一个问题就是我以前提交的时候很容易就MLE,内存超限,直到我把AC自动机封存到一个结构体中,而且并不是一开始就使用memsettrie树上的…
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6961    Accepted Submission(s): 1619 [题目链接]http://acm.hdu.edu.cn/showproblem.php?pid=1325 这题如果用并查集做的话,最后判断的时候还是要看一个结点的入度是否为大于1(纯并查集的…
前言 第一天的算法都还没有缓过来,直接就进入了第二天的算法学习.前一天一直在整理Binary Search的笔记,也没有提前预习一下,好在Binary Tree算是自己最熟的地方了吧(LeetCode上面Binary Tree的题刷了4遍,目前95%以上能够Bug Free)所以还能跟得上,今天听了一下,觉得学习到最多的,就是把Traverse和Divide Conquer分开来讨论,觉得开启了一片新的天地!今天写这个博客我就尽量把两种方式都写一写吧. Outline: 二叉树的遍历 前序遍历t…
参考[LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal 可以对binary tree进行遍历. 此处说明Divide and Conquer 的做法,其实跟recursive的做法很像,但是将结果存进array并且输出,最后conquer (这一步worst T:O(n)) 起来,所以时间复杂度可以从遍历O(n) -> O(n^2). 实际上代码是一样, 就是把[root.val] 放在先, 中, 后就是pr…
Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and doe…
上图论课的时候无意之间看到了这个,然后花了几天的时间学习了下,接下来做一个总结. 一般斯坦纳树问题是指(来自百度百科): 斯坦纳树问题是组合优化问题,与最小生成树相似,是最短网络的一种.最小生成树是在给定的点集和边中寻求最短网络使所有点连通.而最小斯坦纳树允许在给定点外增加额外的点,使生成的最短网络开销最小. 然后听说已经被证明为是NP问题了,在ACM竞赛中我们不研究这个,我们研究更简单一些的问题. 对于图G(V,E),其中V表示图中点集,E表示图中边集.设A是V的某个子集,求至少包含A中所有点…
Problem Description You are given a tree with N nodes which are numbered by integers 1..N. Each node is associated with an integer as the weight. Your task is to deal with M operations of 4 types: 1.Delete an edge (x, y) from the tree, and then add a…
#include<iostream> #include<cstdio> #include<algorithm> #define Max 1005 using namespace std; struct line{ double x, y1, y2; int flag; }x_line[Max]; struct node{ int l, r, flag; double x, f; }tree[Max]; double point[Max]; int n, m, xm; i…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4751 题目大意:判断一堆人能否分成两组,组内人都互相认识. 解题思路:如果两个人不是相互认识,该两人之间连边.最终构成一张图,二分匹配. #include<cstdio> #include<cstring> #include<iostream> using namespace std; #define maxn 105 #define maxm 20010 int n,e;…
原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round7-I.html 题目传送门 -  https://www.nowcoder.com/acm/contest/145/I 题意 给定一棵有 $n$ 个节点的树,问有多少个点集的直径恰好等于 $D$ . 一个点集的直径定义为该点集中距离最远的两个点的距离. 两个点的距离定义为他们在树上的最短路径经过的边数. $n\leq 10^5$ 题解 我的做法有点难写,官方…
一. 1. Lowest Common Ancestor class Solution { public: TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *A, TreeNode *B) { if (root == NULL || root == A || root == B) { return root; } TreeNode* left = lowestCommonAncestor(root->left, A, B); Tre…
Problem Description   Zero and One are good friends who always have fun with each other. This time, they decide to do something on a tree which is a kind of graph that there is only one path from node to node. First, Zero will give One an tree and ev…
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5534 题目大意是给了n个结点,让后让构成一个树,假设每个节点的度为r1, r2, ...rn,求f(x1)+f(x2)+...+f(xn)的最大值. 首先由于是树,所以有n-1条边,然后每条边连接两个节点,所以总的度数应该为2(n-1). 此外每个结点至少应该有一个度. 所以r1+r2+...rn = 2n-2.ri >= 1; 首先想到让ri >= 1这个条件消失: 令xi = ri,则x1+x…
Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them: For a tree T, let F(T,i) be the distance between vertice 1 and vertice i.(The length of…
之前两次遇到过函数的思想的题,所以这次很敏感就看出来了.可以参考之前的题: https://www.cnblogs.com/hua-dong/p/9291507.html Christmas is coming! Eddy has received a Christmas tree as gift. Not surprisingly, the tree consists of N vertices and N-1 edges and magically remains connected. Cu…
Problem Description After inventing Turing Tree, 3xian always felt boring when solving problems about intervals, because Turing Tree could easily have the solution. As well, wily 3xian made lots of new problems about intervals. So, today, this sick t…
      Description There is an apple tree outside of kaka's house. Every autumn, a lot of apples will grow in the tree. Kaka likes apple very much, so he has been carefully nurturing the big apple tree. The tree has N forks which are connected by bran…
Revenge of Segment Tree Problem Description In computer science, a segment tree is a tree data structure for storing intervals, or segments. It allows querying which of the stored segments contain a given point. It is, in principle, a static structur…
Consider a un-rooted tree T which is not the biological significance of tree or plant, but a tree as an undirected graph in graph theory with n nodes, labelled from 1 to n. If you cannot understand the concept of a tree here, please omit this problem…
题目描述 In computer science, a binary tree is a tree data structure in which each node has at most two children. Consider an infinite full binary tree (each node has two children except the leaf nodes) defined as follows. For a node labelled v its left…
#include <cstdio> #include <cstdlib> #include <climits> #include <cstring> #include <algorithm> using namespace std; int map[1015][1015]; void update(int x,int y, int n) { for(int i=x;i<=1005;i+=(i&(-i))) { for(int j=y…
#include<iostream> #include<cstring> #include<cstdio> using namespace std; int n, m; int num[100005]; int front(int x) { return x&(-x); } int update(int x,int k) { while(x<=n) { num[x]+=k; x+=front(x); } return 1; } int sum(int x)…
#include<iostream> #include<cstdio> using namespace std; struct node { int l, r, m; int max; }num[800005]; int val[200005]; int n, m; int init(int l, int r, int k) { num[k].l = l; num[k].r = r; if(l==r) { num[k].m = l; num[k].max=val[l]; retur…
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; struct node{ int l, r, s; }num[800005]; int n, m, key; void build(int l,int r,int k) { num[k].l = l; num[k].r = r; num[k].s = 0; if(l == r) { num[k].s = 1; ret…
SDUST的训练赛 当时死磕这个水题3个小时,也无心去搞其他的 按照题意,转换成无向图,预处理去掉单向的边,然后判断剩下的图能否构成两个无向完全图(ps一个完全图也行或是一个完全图+一个孤点) 代码是赛后看的网上大神,所以转载过来了,dfs染色的时候很巧妙,巧妙的用到了就两个无向完全图 #include <cstdio> #include <cstring> #include <cmath> #include <vector> #include <qu…
原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round3-G.html 题目传送门 - 2018牛客多校赛第三场 G 题意 给定一个 $n$ 个节点的树,有 $k$ 种颜色. 现在让你给每一个节点都染上一种颜色,总共有 $k^n$ 种方法. 现在问,在所有染色方案中,使得相同颜色点对之间的最短距离为 $D$ 的有多少种方案. 答案对于 $10^9+7$ 取模. $n,k,d\leq 5000$ 题解 首先我们来算…