codeforces 767C - Garland】的更多相关文章

题目链接:http://codeforces.com/contest/767/problem/C 问能否将一棵带点权的书分成点权$3$块,求任意方案. 其实考虑一棵以$x$为根的子树权值为${\frac{1}{3}\sum val[i]}$之后它就一定要作为单独的一块了,那么DFS一遍即可. #include<iostream> #include<cstdio> #include<algorithm> #include<vector> #include<…
C. Garland time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Once at New Year Dima had a dream in which he was presented a fairy garland. A garland is a set of lamps, some pairs of which are…
[题目链接]:http://codeforces.com/contest/767/problem/C [题意] 一棵树; 树上的每个节点都有一个权值; 让你把一棵树切掉两条边; 然后把这棵树分成了3个部分; 要求这3个部分,每个部分的权值和相同; 即sum1=sum2=sum3 [题解] 树形DP; 一开始累加所有节点的权值和sum; 如果不是3的倍数则直接输出无解; 用cnt[x]记录x节点下方的子树和(权值和); 假设dfs到了第x个节点 考虑两种情况; ① 有两个节点y,z; 且sum/3…
传送门 题意:在一个树上,问能否切两刀,使得三块的节点值的和相同. 思路: 由于这个总的节点和是不变的,每块的节点值和sum固定,dfs搜索,和等于sum/3,切.若不能分成三块(不能被3整除,-1). 还要判断,切掉的不能是根节点(一条链的情况),还要虽然是3的倍数,但不能切成三个的情况. ac代码 #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #…
花了6个小时,终于成功ac...... 两边dfs,第一遍求子树和,第二遍判断有没有2*t[s]/3和t[s]/3,因为要求的节点可能是在同一条线上,同时要有2*t[s]/3和t[s]/3的情况,且2*t[s]/3距根节点更近 还要注意的是如果同一条线上有两个t[s]/3的点是行不通的,必须要在两个不同分支上 结果跑了1996ms,险过=-= #include<map> #include<set> #include<cmath> #include<queue>…
题目链接:http://codeforces.com/problemset/problem/1108/D time limit per test 1 secondmemory limit per test 256 megabytesinput standard inputoutput standard output You have a garland consisting of n lamps. Each lamp is colored red, green or blue. The colo…
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard output Nothing is eternal in the world, Kostya understood it on the 7-th of January when he saw partially dead four-color garland. Now he has a goal to…
C. Nice Garland time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ii-th lamp is sisi ('R',…
D. Diverse Garland time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ii-th lamp is sisi ('…
You have a garland consisting of nn lamps. Each lamp is colored red, green or blue. The color of the ii-th lamp is sisi ('R', 'G' and 'B' — colors of lamps in the garland). You have to recolor some lamps in this garland (recoloring a lamp means chang…