cf1042F. Leaf Sets(贪心)】的更多相关文章

题意 题目链接 给出一棵树,删除一些边,使得任意联通块内的任意点距离不超过$k$ sol 考场上想的贪心是对的:考虑一棵子树,如果该子树内最深的两个节点的距离相加$>k$就删掉最深的那个点,向上update的时候只返回最深的点的深度 然而却苦于写不出代码... /* */ #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<map&g…
题目大意:给你一棵树,让你对叶节点分组,保证每组中,任意两个叶节点之间的距离不大于K,求最小的组数 手动yy的贪心竟然对的 对于每个节点,维护一个$ma[i]$,表示在$i$节点的子树内 未被分组的叶节点到$i$节点的最长距离 那么,对于每个节点,把它的子节点按照$ma[i]$排序,那么如果这个点的子树不需要额外的分组,就要保证最大的$ma[v1]$和次大的$ma[v2]$之间的距离小于等于K 如果不满足,说明需要对子树内的节点进行额外分组 根据贪心的思想,选择ma最大的子节点$v1$,那么就从…
Leaf Sets 题意:给你一棵树,树上有n个点,只有一条边的点叫做叶子,现在要求把所有的叶子分组,每个组内的所有叶子的距离都不能大于k. 题解: 我们可以随意找一个不是叶子的节点当做这颗树的根节点,这样这棵树中叶子就不会出现在上方了,现在我们先把所有的叶子都单独当做一个集合来. 假设现在我们在处理以u为根的这个子树信息, 我们可以得到u子树内的叶子都到u的这个地方的信息,对这些信息来说,我们把距离都sort一遍,然后看一下是不是能合并,能合并就把信息合并一下,然后在把u的信息记为 min (…
题意:给定一棵$n$个点的树,将叶子节点分为数个集合使集合里点对最长距离不超过$k$,求最少集合数.($n\le1000000$) 首先我们可以想到,这道题并不是让你构造最优方案,因为只要把所有叶子节点的集合任意合并至无法操作,就一定是最优答案了 这个感性理解一下就是那么回事,我一开始做的时候就想到的 然后其实我们把叶子结点向上合并即可,只要子树内两个集合的距离不超过$k$,就把他们合起来,记成到当前点距离较大的那个集合 这样对于子树两两合并,我们可以用启发式合并的堆来实现,复杂度是$nlog^…
[CF1042F]Leaf Sets 题面 洛谷 题解 对于一个根节点\(x\),考虑其子树内的所有\(lca\)为它的叶子节点到它的距离\(d_1<d2<...<d_m\). 那么对于中间最小的\(d_i+d_{i+1}>K\),我们可以将\(i\)之前的所有叶子节点合并成一个大点,并以深度\(d_i\)向上合并,再将\(d_{i+1}...d_m\)向上合并即可. 这样子用数据结构维护复杂度是\(O(n\log n)\)的. 然而我们发现只有\(d_i\)继续向上合并才有用,那…
F. Leaf Sets http://codeforces.com/contest/1042/problem/F 题意: 将所有的叶子节点分配到尽量少的集合,一个可行的集合中两两叶子节点的距离<=k. 分析: 可以证明,对于一个子树内的两个叶子节点,把它们分到同一个集合中一定比分到两个集合中好. 所以一个子树内的叶子集合,能合并,尽量合并. 从叶子节点dfs,往上推,当到达一个点时,求出这棵子树内所有的叶子集合到这个节点的距离.如果两个距离小于等于k,那么显然是可以合并的.合并后,只返回这棵子…
传送门 Luogu 解题思路 比较显然的一种做法: 我们把一个点的子树高度抠出来并排序记为 \(L_i\),找到最大的 \(i\) 使得 \(L_{i-1}+L_i\le K\). 于是我们把前 \(i\) 个对应的子树中的叶子合并为一个集合,之后的单独为一个集合,从非叶子跑一遍 \(\text{DFS}\) 就可以了,最后输出答案即可. 细节注意事项 咕咕咕 参考代码 #include <algorithm> #include <iostream> #include <cs…
D. Generating Sets time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a set Y of n distinct positive integers y1, y2, ..., yn. Set X of n distinct positive integers x1, x2, ...,…
D. Generating Sets 题目连接: http://codeforces.com/contest/722/problem/D Description You are given a set Y of n distinct positive integers y1, y2, ..., yn. Set X of n distinct positive integers x1, x2, ..., xn is said to generate set Y if one can transfo…
H - Generating Sets Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description You are given a set Y of ndistinct positive integers y1, y2, ..., yn. Set X of ndistinct positive integers x1, x2, ..., xn is…
D. Generating Sets time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a set Y of n distinct positive integers y1, y2, ..., yn. Set X of n distinct positive integers x1, x2, ...,…
大意: 给定树, 求叶子的最小划分, 使得每个划分内任意两个叶子距离不超过k. 任选一个非叶结点, 贪心合并. #include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include <math.h> #include <set> #include <map> #include <queue> #include…
由于我的codeforces的帐号登不上,所以我错过了这场比赛,只好赛后再抄题解自己做. A Benches 最大的情况就是所有人都挤在那个人最多的长椅上,最小的情况是所有人尽量平均的坐. #include <cstdio> #include <cstring> #include <algorithm> const int N = 110; int a[N], n, m, mx; int main() { scanf("%d%d", &n,…
题意: 给你一张有向图,问你最少加多少条边这张图强连通 思路: 缩点之后,如果不为1个点,答案为出度为0与入度为0的点的数量的最大值 代码: #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<string> //#include<stack> #include<queue…
D. Generating Sets time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given a set Y of n distinct positive integers y1, y2, ..., yn. Set X of n distinct positive integers x1, x2, ...,…
F - Supermarket Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1456 Appoint description:  System Crawler  (2015-11-30) Description A supermarket has a set Prod of products on sale. It earns a p…
A. Memory and Crow time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output There are n integers b1, b2, ..., bn written in a row. For all i from 1 to n, values ai are defined by the crows performi…
E. Ants in Leaves 题目连接: http://www.codeforces.com/contest/622/problem/E Description Tree is a connected graph without cycles. A leaf of a tree is any vertex connected with exactly one other vertex. You are given a tree with n vertices and a root in t…
  Product of digits  For a given non-negative integer number N , find the minimal natural Q such that the product of all digits of Q is equal N . Input The first line of input contains one positive integer number, which is the number of data sets. Ea…
I can do it! Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 1010    Accepted Submission(s): 471 Problem Description Given n elements, which have two properties, say Property A and Property B.…
Y2K Accounting Bug Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8678   Accepted: 4288 Description Accounting for Computer Machinists (ACM) has sufferred from the Y2K bug and lost some vital data for preparing annual report for MS Inc.…
[抄题]: Given a binary tree where every node has a unique value, and a target key k, find the value of the nearest leaf node to target k in the tree. Here, nearest to a leaf means the least number of edges travelled on the binary tree to reach any leaf…
[抄题]: Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number. An example is the root-to-leaf path 1->2->3 which represents the number 123. Find the total sum of all root-to-leaf numbers. Note: A leaf is…
题目链接:http://poj.org/problem?id=1456 Time Limit: 2000MS Memory Limit: 65536K Description A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold by a deadline dx that is measured as an integral number of tim…
题意:给定两个数字n,m,让你把数字 n 拆成一个长度为 m 的序列a1,a2,a3...am,并且∑2^ai = n,如果有多组,要求序列中最大的数最小,然后再相同就要求除了最大数字典序最大. 析:直接想可能不好想,可以考虑,如果把数字 n 拆成 2 的多次幂,可以用贪心来解决,然后如果长度已经超过了 m ,那么就是无解,否则就是有解,再考虑把这个序列变成 m 长度,因为要让最大的最小,所以,可以把最大的拆成两个,然后再看里面最大的是几个,如果序列当前长度加上序列中最大的数的数目,仍然不超过m…
Supermarket Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10725 Accepted: 4688 Description A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold by a deadline dx that is measured as an integr…
Description As we all know caterpillars love to eat leaves. Usually, a caterpillar sits on leaf, eats as much of it as it can (or wants), then stretches out to its full length to reach a new leaf with its front end, and finally "hops" to it by c…
Supermarket Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 17634   Accepted: 7920 题目链接:http://poj.org/problem?id=1456 Description: A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold by a…
A. Memory and Crow time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output There are n integers b1, b2, ..., bn written in a row. For all i from 1 to n, values ai are defined by the crows performi…
A. Initial Bet time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output There are five people playing a game called "Generosity". Each person gives some non-zero number of coins b as an initial…