【Codeforces 161D】Distance in Tree】的更多相关文章

[链接] 我是链接,点我呀:) [题意] 问你一棵树上有多少条长度为k的路径 [题解] 树形dp 设 size[i]表示以节点i为根节点的子树的节点个数 dp[i][k]表示以i为根节点的子树里面距离节点i的距离为k的节点有多少个. 长度为k的路径有两种情况. 1.这个路径从x开始,只经过x的一个子树. 2.这个路径经过x,横跨x的两个子树. 第一种情况直接累加dp[x][k]即可 第二种情况,可以枚举其中一棵子树y包括的路径长度i,另外一棵子树(其余size[x]-1棵子树都可以作为另外一棵子…
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard output Anton is growing a tree in his garden. In case you forgot, the tree is a connected acyclic undirected graph. There are n vertices in the tree, e…
[题目链接]:http://codeforces.com/contest/791/problem/D [题意] 你可以从树上的节点一次最多走k条边. (称为跳一次); 树为无权树; 然后问你任意两点之间的条的次数的和为多少; [题解] 如果k=1的话; 问题就是求任意两点之间的距离的和了; 这个可以在O(N)的复杂度下搞出来; 即 枚举每一条边; 看这条边左边的点的数目和右边的点的数目分别为多少->num1和num2 num1*num2就是经过这条边的路径个数; 累加所有的边的这个值就是任意两点…
[CF161.D] Distance in Tree time limit per test 3 seconds memory limit per test 512 megabytes A tree is a connected graph that doesn't contain any cycles. The distance between two vertices of a tree is the length (in edges) of the shortest path betwee…
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output A group of n cities is connected by a network of roads. There is an undirected road between every pair of cities, so there are roads in total. I…
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output A tree is an undirected connected graph without cycles. Let's consider a rooted undirected tree with n vertices, numbered 1 through n. There are…
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 统计叶子节点个数m 把每条和叶子节点相邻的边权设置成s/cnt就可以了 这样答案就是2*s/m(直径最后肯定是从一个叶子节点开始,到另外一个叶子节点结束) 证明: 设dis(i,j)表示节点i和节点j之间的权值和 设a[1],a[2]..a[m]是m个叶子节点 则 \(max(dis(a[i],a[j])) >= ∑\frac{dis(a[i],a[j]) }{ \frac{m*(m-1)}{2|} }\) 即\(\frac{m*(m-1…
题目链接: http://codeforces.com/problemset/problem/698/B 题解: 还是比较简单的.因为每个节点只有一个父亲,可以直接建反图,保证出现的环中只有一条路径. 然后发现,有多少个环,就需要改多少条边.然后设出现连向自己的节点为x,这些也要改边,对答案的贡献应为:$max(x-1,0)$.对于最后的根节点,有自环选一个,没自环在其他环上任选一个点就行. #include<cstdio> inline int min(int a,int b){return…
dfs找出联通块个数cnt,当形成环时,令指向已访问过节点的节点变成指向-1,即做一个标记.把它作为该联通图的根. 把所有联通的图变成一颗树,如果存在指向自己的点,那么它所在的联通块就是一个树(n-1条边),选择这样一个点,其它联通块的根指向它,就需要cnt-1次改变.如果都是环(没有指向自己的),那任意选定一个环,拆开,其它环拆开再连到此环上,就需要cnt次改变. #include <cstdio> #define N 200005 int a[N],v[N],h[N],fa[N],q[N]…
题 题意 给你一个区间,求里面有多少个数是k的次方. 分析 暴力,但是要注意这题范围会爆long long,当k=1e8: l=1:r=1e18时 k²=1e16,判断了是≤r,然后输出,再乘k就是1e24 , ULL 都存不下,就爆了. 所以当 t > r / k 时跳出循环 代码 AC代码 #include <stdio.h> unsigned long long l,r,k,t; int ok; int main(){ scanf("%lld%lld%lld",…
[codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=2000),问满足[数列长度是k && 数列中每一个元素arr[i]在1~n之间 && 数列中元素可以重复]的数列有多少个?结果对10^9+7取余 解题思路:dp[i][j]表示长度是j,最后一位是i的种数 if(kk%i==0) dp[kk][j+1]+=dp[i][j] #inc…
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Roman planted a tree consisting of n vertices. Each vertex contains a lowercase English letter. Vertex 1 is the root of the tree, each of the n …
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Alyona has a tree with n vertices. The root of the tree is the vertex 1. In each vertex Alyona wrote an positive integer, in the vertex i she wr…
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard output Bogdan has a birthday today and mom gave him a tree consisting of n vertecies. For every edge of the tree i, some number xi was written on it. I…
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Each New Year Timofey and his friends cut down a tree of n vertices and bring it home. After that they paint all the n its vertices, so that the…
[题目链接]:http://codeforces.com/contest/792/problem/D [题意] 给你一棵满二叉树; 给你初始节点; 给你若干个往上走,左走,右走操作; 让你输出一系列操作结束之后节点的位置; [题解] 这个节点的标志方式类似树状数组; 用树状数组左走右走就好; L->x-=lowbit(x)/2; R->x+=lowbit(x)/2; U->先假设x是左儿子,然后依照规则求出父亲F,然后看看F的左儿子是不是真的是x,是的话爸爸就是F,否则x是右儿子,然后根…
[题目链接]:http://codeforces.com/problemset/problem/514/E [题意] 无限节点的树; 每个节点都有n个儿子节点; 且每个节点与其第i个节点的距离都是ai; 问你与根节点的距离不超过x的节点个数; [题解] 考虑一个非常不靠谱的DP方程 f[i]=∑(f[i-j]*cnt[j]); 这里f[i]表示与根节点的距离为i的节点个数; cnt[j]表示ai的值中为j的ai的个数;(即与儿子节点距离为j的边的个数); 因为ai最大值为100,所以j∈[1..…
[题目链接]:http://codeforces.com/problemset/problem/765/E [题意] 给你一棵树; 可以把一个节点的两条相同长度的链合并成一条链; 且这两条相同长度的链上的点不能有"分叉"; 问你最后是否能形成一条链; 然后让你求链的最短值; [题解] dfs; 类似树形DP的东西; 对于每个节点x; 看看它下面的链的长度有多少种->把链的长度存在set里面最后看看set的大小,来确定链的种类; 如果链的种类为0,则这个节点为叶子节点,直接返回0…
[题目链接]:http://codeforces.com/contest/723/problem/F [题意] 给你一张图; 让你选择n-1条边; 使得这张图成为一颗树(生成树); 同时s的度数不超过ds且t的度数不超过dt [题解] 先把s和t隔离开; 考虑其他点形成的若干个联通块; 因为一开始的图是联通的; 所以,那些不同的联通块,肯定是通过s或通过t而联通的; 这样就只要考虑每个联通块和s.t的联通性了; 对于只能和s或t之中的一个相连的块; 直接连一条边到s或t就好,然后ds-=1或是d…
[题目链接]:http://codeforces.com/problemset/problem/348/B [题意] 给你一棵树; 叶子节点有权值; 对于非叶子节点: 它的权值是以这个节点为根的子树上的叶子节点的权值的和; 定义一棵树是平衡的,当且仅当,每个节点的所有直系儿子的权值都相等; 问你要使得这棵树平衡,最少需要删除掉多少叶子节点上的权值; [题解] 在第一个dfs里面求出d[i]和s[i]; 设d[i]表示以i为根的子树要平衡的话最少需要多少权值(注意这里不是说最少要删去多少权值,而是…
[题目链接]:http://codeforces.com/contest/514/problem/C [题意] 给你n个字符串; 然后给你m个询问;->m个字符串 对于每一个询问字符串 你需要在n个字符串里面找到和它的长度相同,且只有一个位置的字符不同的字符串; 或者告知这是不存在的; [题解] 写个字典树; 在每一位多了两种选择; (即更改这个字符) 然后前面已经确定匹配的不要重新匹配;->不然会超时 有改和不改两种可能. 注意最后必须要改一个字符. 字典树以后还是用0作为根节点吧.不然to…
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Generous sponsors of the olympiad in which Chloe and Vladik took part allowed all the participants to choose a prize for them on their own. Chri…
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output Amr lives in Lala Land. Lala Land is a very beautiful country that is located on a coordinate line. Lala Land is famous with its apple trees grow…
time limit per test3 seconds memory limit per test256 megabytes inputstandard input outputstandard output Rikhail Mubinchik believes that the current definition of prime numbers is obsolete as they are too complex and unpredictable. A palindromic num…
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output PolandBall lives in a forest with his family. There are some trees in the forest. Trees are undirected acyclic graphs with k vertices and k - 1 e…
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output This is an interactive problem. In the interaction section below you will find the information about flushing the output. The New Year tree of h…
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Student Vladislav came to his programming exam completely unprepared as usual. He got a question about some strange algorithm on a graph - somet…
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output There are n beacons located at distinct positions on a number line. The i-th beacon has position ai and power level bi. When the i-th beacon is…
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard output Running with barriers on the circle track is very popular in the country where Dasha lives, so no wonder that on her way to classes she saw the…
[题目链接] 点击打开链接 [算法] 线段树,注意数据量大,要动态开点 [代码] #include<bits/stdc++.h> using namespace std; ; ,root = ; struct Node { int lc,rc,tag,sum; } Tree[MAXN]; template <typename T> inline void read(T &x) { ; x = ; char c = getchar(); for (; !isdigit(c);…