CF-1099 D. Sum in the tree】的更多相关文章

D. Sum in the tree 题目链接:https://codeforces.com/contest/1099/problem/D 题意: 给出一棵树,以及每个点的si,这里的si代表从i号结点到根节点的权值和.但是有些si=-1,这就相当于丢失了当前结点的数据. 假设原本每个点的权值为ai,那么现在求sum{ai}的最小为多少,ai为非负数. 题解: 这题可以单独看每一条链上的s值,假设当前结点为u,儿子结点v,那么就有几种情况: 1.su==-1&&sv==-1,这种不用管,继…
1099 Build A Binary Search Tree(30 分) A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a nod…
D. Sum in the tree time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Mitya has a rooted tree with nn vertices indexed from 11 to nn, where the root has index 11. Each vertex vv initially had…
1099 Build A Binary Search Tree (30)(30 分) A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of…
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90701125 1099 Build A Binary Search Tree (30 分)   A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node cont…
1099. Build A Binary Search Tree (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only…
CF-1099 D. Sum in the tree 题意:结点序号为 1~n 的一个有根树,根序号为1,每个点有一个权值a[i], 然后定义一s[i]表示从根节点到 结点序号为i的结点的路途上所经过的结点的权值之和. 如图所示有: s[1] = 1 s[2] = 2 s[3] = 2 s[4] = 2 s[5] = 2 而现在的情况是:所有的a我们都不知道,只知道部分的s,然后需要我们求出对a求和的最小值. 考虑一般情况,每一个结点有很多子结点.为了描述清楚,对于某个结点i,a为 i 的权值a…
D. Sum in the tree 题意 给出一颗树,奇数层数的点有值,值代表从1到该点的简单路的权值的和,偶数层数的点权值被擦去了 问所有节点的和的最小可能是多少 思路 对于每一个-1(也就是值未知的点)其所填的值的最小值小于等于他的自己点中已知的点的权值 换个说法就是 一个-1点有多个子树 那么这个点的值最小也是这几个子树里面的最大的值,才能使得其合法 从根节点递归求值即可 其中注意 这里计算权值和的时候,因为已知每个点到根节点的路径的点的权值和所以计算一个节点以及其子树到根节点的权值和的…
A. Sum in the tree Mitya has a rooted tree with nn vertices indexed from 11 to nn, where the root has index 11. Each vertex vv initially had an integer number av≥0av≥0 written on it. For every vertex vv Mitya has computed svsv: the sum of all values…
Mitya has a rooted tree with nn vertices indexed from 11 to nn, where the root has index 11. Each vertex vv initially had an integer number av≥0av≥0 written on it. For every vertex vv Mitya has computed svsv: the sum of all values written on the vert…
题目链接;http://codeforces.com/contest/1099/problem/D 题目大意:给出一棵树,每个节点到根节点的路径上经过的所有点的权值之和,其深度为偶数的节点的信息全部擦除了,也就是用-1表示,让你求最终所有点权之和(要求最小) 具体思路:对于每一个节点,这个点到根节点的权值最小的时候是他的所有的根节点的最小值,然后一路更新上去就可以了,最后求值得时候,我们求父亲节点和子节点之间的差就可以了, 如果差值为负值就是非法情况. AC代码: #include<bits/s…
http://www.patest.cn/contests/pat-a-practise/1099 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subt…
Vasya and a Tree 题意: 给定一棵树,对树有3e5的操作,每次操作为,把树上某个节点的不超过d的子节点都加上值x; 思路: 多开一个vector记录每个点上的操作.dfs这颗树,同时以深度开一个树状数组,踩到u节点的时候,给数组add(deep, x); add(min(maxn,deep + op[u][i].fi + 1), - op[u][i].se); 搜索下去,反回前得到这个节点的答案,并消去这个点的影响. //#pragma GCC optimize(3) //#pr…
解题思路 \(dsu\) \(on\) \(tree\)的模板题.暴力而优雅的算法,轻儿子的信息暴力清空,重儿子的信息保留,时间复杂度\(O(nlogn)\) 代码 #include<iostream> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<set> using namespace std; const int…
New Year is coming in Tree World! In this world, as the name implies, there are n cities connected by n - 1 roads, and for any two distinct cities there always exists a path between them. The cities are numbered by integers from 1 to n, and the roads…
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greate…
https://pintia.cn/problem-sets/994805342720868352/problems/994805367987355648 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than th…
我是题面.原题地址 很简单的一道贪心题 首先,先想想怎么判断是否合法 题目中说,a是自然数,那么子节点的s明显是不能比父节点大的,如果比父节点大,不合法! 所有深度为偶数的点的s被删除了,也只有深度为偶数的点被删除了,所以如果深度为奇数的点被删除了,或者有深度为偶数的点没有被删除,不合法! 所有不合法的情况我们已经判断完了,下面考虑如何使权值和最小 对于奇数层的点我们肯定是无法影响了,只有通过控制偶数层的点的权值才能影响总的权值和,而且只能影响自己和自己子节点的权值 我们从简单的入手,对于偶数层…
D. Arpa’s letter-marked tree and Mehrdad’s Dokhtar-kosh paths http://codeforces.com/problemset/problem/741/D 题意: 一棵根为1 的树,每条边上有一个字符(a-v共22种). 求每个子树内的最长的路径,使得路径上的边按照一定顺序排列后是回文串. 分析: dsu on tree.询问子树信息. 首先将这22个字符,转化为二进制.a=1,b=10,c=100...如果一条路径可以是回文串,那么…
给定两正整数 $a, b$ .给定序列 $s_0, s_1, \dots, s_n,s_i$ 等于 $1$ 或 $-1$,并且已知 $s$ 是周期为 $k$ 的序列并且 $k\mid (n+1)$,输入只给出序列 $s$ 的前 $k$ 项. Find out the non-negative remainder of division of $\sum\limits_{i=0}^n s_i a^{n-i}b^i$ by $10^9+9$. 数据范围 $ 1\le n, a, b \le 10^9…
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys greate…
原题链接在这里:https://leetcode.com/problems/two-sum-less-than-k/ 题目: Given an array A of integers and integer K, return the maximum S such that there exists i < j with A[i] + A[j] = S and S < K. If no i, jexist satisfying this equation, return -1. Example…
题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys gre…
Given an array A of integers and integer K, return the maximum S such that there exists i < j with A[i] + A[j] = S and S < K. If no i, j exist satisfying this equation, return -1. Example 1: Input: A = [34,23,1,24,75,33,54,8], K = 60 Output: 58 Expl…
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力求解 日期 题目地址:https://leetcode-cn.com/problems/two-sum-less-than-k/ 题目描述 Given an array A of integers and integer K, return the maximum S such that there exists i < j with A[i] +…
这一场其实有重大的意义,因为是除夕跨年,不过我FST掉大分了(ks) 题意:给你一个n点,m条边的带权图,q次询问,每次给你\(x\),每个边权为\(abs(E[i].w-x)\)答案为所有询问最小生成树边权和.然后输出所有询问结果的异或和. 思路: 因为\(n(n<=50),m(m<=300)\)很小.而\(k(k<=10^7)\)又很大.所以肯定不能每个询问直接求,这里是把询问的x,分块预处理. 如果有两条边\(w1,w2(w1<w2)\)选1条,原来最小生成树,选边一定选w1…
题目链接:http://codeforces.com/problemset/problem/504/E 题意:给出一棵树,每个结点上有一个字母.每个询问给出两个路径,问这两个路径的串的最长公共前缀. 思路:树链剖分,记录每条链的串,正反都记,组成一个大串.记录每条链对应的串在大串中的位置.然后对大串求后缀数组.最后询问就是在一些链上的查询. const int N=600005; int next[N],node[N],head[N],e; void add(int u,int v) { nod…
题意:给一个长度为n的正整数序列,问能不能找到一个不连续的子序列的和可以被m整除. 解法:抽屉原理+dp.首先当m<n时一定是有答案的,因为根据抽屉原理,当得到这个序列的n个前缀和%m时,一定会出现两个相同的数,这两个前缀和相减得到的序列和一定可以被m整除.当n<=m时,dp一下就可以了,类似01背包. 其实可以直接dp,只要滚动数组+在找到答案时break就可以了,同样因为抽屉原理,当枚举到第m+1个物品的时候就一定会得到解,所以最后复杂度O(m^2). 代码: #include<st…
http://codeforces.com/problemset/problem/397/D 题意:v(n) 表示小于等于n的最大素数,u(n)表示比n的大的第一个素数,然后求出: 思路:把分数拆分成两个分数相减,你就会发现规律,等于1/2-1/3+1/3-1/5........,找出v(n)和u(n),答案就出来了. #include <cstdio> #include <cstring> #include <algorithm> #define ll long lo…
预处理每个节点左子树有多少个点. 然后确定值得时候递归下去就可以了. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; struct Node { int…