HDU - 5156 Harry and Christmas tree】的更多相关文章

题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=5156 题意 : 给一颗编号为1-n的以1为根的树, 已知有m个颜色的礼物分布在某些节点上(同一节点可以有多个), 问 : 对于编号从1-n的节点, 每一个节点对应子树上有多少颜色不同的礼物. 思路 : 一开始的想法是DFS记录节点序列, 再开vector记录每个节点上挂的礼物(同一节点上对颜色去重), 用树状数组统计一个区间内不同颜色的种类 但是由于记录位置的数组同样要开到二维, 超过了限制,…
Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 23387   Accepted: 5063 Description Christmas is coming to KCM city. Suby the loyal civilian in KCM city is preparing a big neat Christmas tree. The simple structure of t…
http://poj.org/problem?id=3013 Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 19009   Accepted: 4048 Description Christmas is coming to KCM city. Suby the loyal civilian in KCM city is preparing a big neat Christmas…
Big Christmas Tree 题意:图中每个节点和边都有权值,图中找出一颗树,树根为1使得 Σ(树中的节点到树根的距离)*(以该节点为子树的所有节点的权值之和) 结果最小: 分析:直接求出每个节点到树根的最短距离距离,之后乘上自身节点的权值求和即可: ps:注意特判v = 0:这时cnt = 1 > v; Djistra + priority_queue //Accepted 2804K 141MS #include <cstdio> #include <cstring&g…
题目链接:hdu 4912 Paths on the tree 题目大意:给定一棵树,和若干个通道.要求尽量选出多的通道,而且两两通道不想交. 解题思路:用树链剖分求LCA,然后依据通道两端节点的LCA深度排序,从深度最大优先选.推断两个节点均没被标 记即为可选通道. 每次选完通道.将该通道LCA下面点所有标记. #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #includ…
POJ 3013 Big Christmas Tree(最短路Dijkstra+优先队列优化,SPFA) ACM 题目地址:POJ 3013 题意:  圣诞树是由n个节点和e个边构成的,点编号1-n.树根为编号1,选择一些边.使得全部节点构成一棵树.选择边的代价是(子孙的点的重量)×(这条边的价值). 求代价最小多少. 分析:  单看每一个点被计算过的代价,非常明显就是从根到节点的边的价值.所以这是个简单的单源最短路问题. 只是坑点还是非常多的. 点的数量高达5w个,用矩阵存不行.仅仅能用边存.…
Big Christmas Tree 题目分析: 叫你构造一颗圣诞树,使得 (sum of weights of all descendant nodes) × (unit price of the edge)尽量的小.转换后就是求根节点到每一个节点的距离最短,也就是最短路. 生成树可能会超时.我没试过.然后,求解最短路要用优化的解法不然会超时.最后的答案就是:sum = w[1] * dist[1] + w[2] * dist[2] + ..... w[n] * dist[n].能够自己推推例…
Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 20974   Accepted: 4535 Description Christmas is coming to KCM city. Suby the loyal civilian in KCM city is preparing a big neat Christmas tree. The simple structure of t…
Big Christmas Tree Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 25823   Accepted: 5577 Description Christmas is coming to KCM city. Suby the loyal civilian in KCM city is preparing a big neat Christmas tree. The simple structure of t…
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5423 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 t…
多校9 1001 hdu 6161 Big binary tree 题意 有一个完全二叉树.编号i的点值是i,操作1是修改一个点的值为x,操作2是查询经过点u的所有路径的路径和最大值.10^5个点,10^8次操作. 题解 用map储存修改过的点的值val,和dp[i],表示i子树的最大路径和. 查询就是考虑两种情况,经过u点的两个孩子和经过它的一个孩子再经过它父亲,需要边走到根节点边更新答案. 代码 #include <cstdio> #include <algorithm> #i…
Query on A Tree Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total Submission(s): 712    Accepted Submission(s): 266 Problem Description Monkey A lives on a tree, he always plays on this tree. One day, monke…
题目:这里 题意: 感觉并不能表达清楚题意,所以 Problem Description In mathematics, and more specifically in graph theory, a tree is an undirected graph in which any two nodes are connected by exactly one path. In other words, any connected graph without simple cycles is a…
Problem Description XXX is very interested in algorithm. After learning the Prim algorithm and Kruskal algorithm of minimum spanning tree, XXX finds that there might be multiple solutions. Given an undirected weighted graph with n (1<=n<=100) vertex…
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16702    Accepted Submission(s): 3761 Problem Description A tree is a well-known data structure that is either empty (null, void, n…
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 20416    Accepted Submission(s): 4628 Problem Description A tree is a well-known data structure that is either empty (null, void, n…
推断是否为树 森林不是树 空树也是树 成环不是树 数据: 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 1 0 0 1 2 2 3 4 5 0 0 2 5 0 0 ans: no no yes #include <stdio.h> #include <string.h> #include <stdlib.h> #include <limits.h> #include <malloc.h> #include <ctype…
http://acm.hdu.edu.cn/showproblem.php?pid=3848 题意: 求一棵树上两个叶子结点之间的最短距离. 思路: 两个叶子节点之间一定会经过非叶子节点,除非只有两个节点. 所以我们只需要维护离每个非叶子节点最远的叶子节点距离和次远距离,两者相加即是两个叶子节点之间的距离. #include<iostream> #include<algorithm> #include<cstring> #include<cstdio> #i…
Query on A Tree Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total Submission(s): 733    Accepted Submission(s): 275 Problem Description Monkey A lives on a tree, he always plays on this tree. One day, monkey…
Revenge of Segment Tree                                                          Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)                                                                                    To…
Is It A Tree? Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 20222    Accepted Submission(s): 456 Problem Description A tree is a well-known data structure that is either empty (null, void, not…
Bob intends to color the nodes of a tree with a pen. The tree consists of NN nodes. These nodes are numbered 1,2,...,N1,2,...,N. The root of the tree is node 11. The initial color of each node is white. Bob can use one unit energy to color one node i…
题目大意: 给你两个节点,前者指向后者(可以认为前者是后者的父节点),然后让你判断是否是一棵树. 解题思路: 先说说这道题和小希的迷宫(HDU1272)那道题的区别,前者给出的两个点是有方向的,而后者是没有的,这就是唯一的区别.再者这道题其实就是让你判断所有的点最后所形成的图是否是一棵树.做这道题时错误了很多遍,细想之,主要是开始思路不好,很容易乱,理清思路让代码变得清晰起来就好了.判断一个图是否为一棵树.有三个重要条件(比小希的迷宫多了一个条件): 1:已经在一个集合里面的两个数不能再同时出现…
题目链接 Paths on the tree 来源  2014 多校联合训练第5场 Problem B 题意就是给出m条树上的路径,让你求出可以同时选择的互不相交的路径最大数目. 我们先求出每一条路径(u, v)中u和v的LCA:w,按照路径的w的深度大小deep[w]对所有的路径排序. deep[w]越大,排在越前面. 然后从第一条路径开始一次处理,看c[u]和c[v]是否都没被标记过,如果都没被标记过则我们把这条路径选上,把答案加1. 同时标记以w为根的子树的节点为1,方便后续对c数组的查询…
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…
Is It A Tree?Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 29483    Accepted Submission(s): 6775 Problem DescriptionA tree is a well-known data structure that is either empty (null, void, noth…
Query on A Tree Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total Submission(s): 1238    Accepted Submission(s): 444 Problem Description Monkey A lives on a tree, he always plays on this tree.One day, monkey…
传送门 AND Minimum Spanning Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 378    Accepted Submission(s): 190 Problem Description You are given a complete graph with N vertices, numbered fr…
题目大概说给一张点和边都有权的图,现在要求其一棵以1结点为根的生成树使树的边权和最小,树边权 = 对应的图边权 * 树边末端点为根的子树所有结点对于图顶点的点权和. 要求∑(边权*子树点权和),等价于求∑(点权*点到根路径上的边权和). 而如果在图中各个点到根都存在最短路,那么最短路的边一定能构造出一棵生成树.一个构造方法就是在进行最短路的松弛操作时记录各点是从哪点转移的,而各点的这个前驱是唯一的这就对应着各点的父结点.这就是最短路径树. 那么这一题直接求各个点到1点的最短路,最后乘一下加一下就…
题意:每次找出第i大的数的位置p输出,然后将i~p之间的数反转. 题解:每次把要的区间转成一棵子树,然后更新.因为每次将第i小的数转到了了i,所以k次操作后,可知前k个数一定是最小的那k个数,所以以后的操作一定不会和前k个数有关,所以每次操作后可以把操作完的数删掉.所以每次把p转到根,然后翻转左子树,删除根就可以了.(也就是p不需要翻转 这个splay处理比较特殊,每个结点的序号就是一开始的位置.splaytree中第i个结点的序号就是第i个数一开始的位置,然后对于每个数排序,记录一开始的位置,…