Kay and Snowflake CodeForces - 686D】的更多相关文章

Kay and Snowflake CodeForces - 686D 题意:给一棵有根树,有很多查询(100000级别的),查询是求以任意一点为根的子树的任意重心. 方法很多,但是我一个都不会 重心几个定义/性质: 1.从树中去掉某点以及和该点相连的所有边后,整棵树变为许多"块".去掉任意一个重心(相比于去掉其他点)可以使得生成的各个"块"的节点数的最大值最少. 类似的一个:对于一棵树n个节点的无根树,找到一个点,使得把树变成以该点为根的有根树时,最大子树的结点数…
After the piece of a devilish mirror hit the Kay's eye, he is no longer interested in the beauty of the roses. Now he likes to watch snowflakes. Once upon a time, he found a huge snowflake that has a form of the tree (connected acyclic graph) consist…
大意:给定有根树, 求每个子树的重心 我太菜了啊, 只能想到暴力树剖, 然而这就是个B题, 感觉树剖+线段树二分还是挺难写的..... 看了题解发现重心一定在重儿子与根的树链上, 重心最多上跳n-1次, 直接暴力就行…
题目链接: D. Kay and Snowflake time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output After the piece of a devilish mirror hit the Kay's eye, he is no longer interested in the beauty of the roses. No…
B. Kay and Snowflake 题目连接: http://www.codeforces.com/contest/685/problem/B Description After the piece of a devilish mirror hit the Kay's eye, he is no longer interested in the beauty of the roses. Now he likes to watch snowflakes. Once upon a time,…
D. Kay and Snowflake     After the piece of a devilish mirror hit the Kay's eye, he is no longer interested in the beauty of the roses. Now he likes to watch snowflakes. Once upon a time, he found a huge snowflake that has a form of the tree (connect…
题目链接: 题目 D. Kay and Snowflake time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output 问题描述 After the piece of a devilish mirror hit the Kay's eye, he is no longer interested in the beauty of the r…
D - Kay and Snowflake 题目大意:给你一棵数q个询问,每个询问给你一个顶点编号,要你求以这个点为根的子树的重心是哪个节点. 定义:一棵树的顶点数为n,将重心去掉了以后所有子树的顶点的个数的两倍不会超过n. 性质 1 :树中所有点到某个点的距离和中,到重心的距离和是最小的,如果有两个距离和,他们的距离和一样. 性质 2 :把两棵树通过某一点相连得到一颗新的树,新的树的重心必然在连接原来两棵树重心的路径上. 性质 3 :一棵树添加或者删除一个节点,树的重心最多只移动一条边的位置.…
D - Kay and Snowflake 思路: 树的重心 利用重心的一个推论,树的重心必定在子树重心的连线上. 然后利用重心的性质,可知,如果有一颗子树的大小超过整棵树的大小的1/2,那么树的重心一定在这颗子树上. 利用以上两条,可知: 如果没有一颗子树的大小超过整棵树的大小的1/2,那么就可以以根节点为树的重心, 不然就从 超过1/2大小的子树 的重心 到 根节点 之间找整棵树的重心. 因为不会找重复的点,所以退化的复杂度为O(n) #include<bits/stdc++.h> usi…
CF685B Kay and Snowflake 链接 CF 题目大意 给你一颗树,询问子树的重心 思路 贪心? 重心肯定是向上走的,所以直接向上跳就好了. 不优秀的时候就不要跳了 ,因为以后也不能更新了. 复杂度O(n)(没大仔细想过) 代码 #include <bits/stdc++.h> using namespace std; const int N=3e5+7; int read() { int x=0,f=1;char s=getchar(); for(;s>'9'||s&l…
Codeforce 685 B. Kay and Snowflake 解析(思維.DFS.DP.重心) 今天我們來看看CF685B 題目連結 題目 給你一棵樹,要求你求出每棵子樹的重心. 前言 完全不知道我怎麼想到的 @copyright petjelinux 版權所有 觀看更多正版原始文章請至petjelinux的blog 想法 首先會感覺是樹狀DP,並且狀態是每個子樹的重心. 在建重心剖分樹的時候,我們找根為\(v\)的樹的重心的方法是看看目前節點有沒有大小大於目前樹的一半(\(size[v…
time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standard output After the piece of a devilish mirror hit the Kay's eye, he is no longer interested in the beauty of the roses. Now he likes to watch snowfla…
分析:就是找到以每个节点为根节点的树的重心 树的重心可以看这三篇文章: 1:http://wenku.baidu.com/link?url=yc-3QD55hbCaRYEGsF2fPpXYg-iO63WtCFbg4RXHjERwk8piK3dgeKKvUBprOW8hJ7aN7h4ZC09QE9x6hYV3lD7bEvyOv_l1E-ucxjHJzqi 2:http://fanhq666.blog.163.com/blog/static/81943426201172472943638/ 3:ht…
树的重心,树形$dp$. 记录以$x$为$root$的子树的节点个数为$sz[x]$,重儿子为$son[x]$,重心为$ans[x]$. 首先要知道一个结论:以$x$为$root$的子树的重心$ans[x]$,一定在$ans[son[x]]$到$x$的路径上,即以$x$的重儿子为根的子树的重心到$x$的路径上. 因此,只要从节点$ans[son[x]]$依次往$father$枚举就可以了. 如果枚举到节点$g$,发现$g$节点满足$sz\left[ {son\left[ g \right]} \…
给你一个树N个点,再给出Q个询问,问以x为根的子树中,重心是哪个?2≤n≤300000,1≤q≤30000 Sol:从下到上,根据性质做一下.1:如果某个点x,其子树y的大小超过总结点个数一半,则重心在y这个子树中.2:如果某个树的重心点,其上方点的个数多于其下方点的,则重心要上移 #include <bits/stdc++.h> using namespace std; const int N = 300000+10; int n,q; vector<int> G[N]; ///…
http://codeforces.com/contest/686/problem/D 给出q个询问,每次要求询问以x为根的子树中,哪一个点是重心. 树的重心:求以cur为根的子树的重心,就是要找一个点,使得删除这个点后,分开来的零散的子树中,节点数的最大值最小.并且最大值最多也只是son[cur] / 2,因为最坏情况(最难分)也就是一条直线,选中间点就可以了. 例如 询问1的时候,就应该删除3,然后得到4个零散分支,2个大小是1,2个是2. 算法思路: 直观来说,应该是删除那个儿子数最多的那…
传送门:http://codeforces.com/problemset/problem/686/D 题意:给你n个节点,其中1为根, 第二行给你2~n的节点的父亲节点编号. 然后是q个询问,求询问的节点为根的重心的节点编号. 思路:利用树的重心的一个性质:树的重心在 根节点 和 根节点的最大子结点的重心 之间.并且2*cnt[v]>cnt[u](u为根节点,v为重心). 代码: #include<iostream> #include<vector> using namesp…
传送门 题意简述:给出一棵树,求每个子树的重心. 首先通过画图可以观察出一个性质,我们从叶子结点向根节点递推重心的话重心的位置是不会下降的. 然后由于一个点的重心要么是自己,要么在重儿子子树内,因此如果重心不是自己,我们从重儿子子树的重心向自己爬统计答案就行了,由于重心不会下降因此时间复杂度O(n)O(n)O(n) 代码: #include<bits/stdc++.h> #define ri register int using namespace std; inline int read()…
题意:给定一棵n个点的树,q次询问,每次询问以某个点为根的子树编号是多少 n,q<=3e5 思路:设sz[u]为以u为根子树的size,v为u的size最大的儿子 若sz[v]*2<sz[u]则u即为重心 否则重心在以v为根的重心到u的路径上,暴力往上走,可以证明是均摊O(n) #include<bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned int uint; typedef u…
A题:Free Ice Cream 注意要使用LL,避免爆int #include <bits/stdc++.h> #define scan(x,y) scanf("%d%d",&x,&y) using namespace std; typedef long long LL; ; int main() { int n,has,x; while(~scan(n,has)) { LL have=has; ; ;i<n;i++) { cin>>o…
Codeforces 686 D. Kay and Snowflake 要求$O(n)$求出以每个节点为根的重心. 考虑对于一个根节点$u$,其重心一定在[各个子树的重心到$u$]这条链上.这样就能够$O(n)$推出来了.证明起来难证易忘.不如记住树的重心的几条奇妙性质: 1. 以重心为根,各子树大小都不超过树的一半. 2. 已知一子树$v$的重心为$x$,则$v$父节点$u$的重心一定在$(x,u)$这条链上. ... 下次用到了再说 Codeforces 842 C. Ilya And Th…
CF79D Password: 差分.两点取反,本质是匹配!最短路+状压DP 取反是套路,匹配是发现可以把操作进行目的化和阶段化,从而第二次转化问题. 且匹配不会影响别的位置答案 sequence 计算最长极长段小于等于j的方案数 突破口是i,k总共对数nlogn级别,干掉j用组合意义大力推导 CF1062F Upgrading Cities DAG考虑topo,关键性质:topo序队列中点两两不可达.只在队列长度<=2时候才关心. CF1060F Shrinking Tree 考虑x是不是rt…
CF685B Kay and Snowflake 题意翻译 输入一棵树,判断每一棵子树的重心是哪一个节点. 题目描述 After the piece of a devilish mirror hit the Kay's eye, he is no longer interested in the beauty of the roses. Now he likes to watch snowflakes. Once upon a time, he found a huge snowflake th…
A. Free Ice Cream 题目连接: http://www.codeforces.com/contest/686/problem/A Description After their adventure with the magic mirror Kay and Gerda have returned home and sometimes give free ice cream to kids in the summer. At the start of the day they hav…
上次简单的说一下:http://www.cnblogs.com/dunitian/p/6041745.html#uid C#版本的国外朋友已经封装了,大家可以去看看:https://github.com/ccollie/snowflake-net 强大的网友出来个简化版本:http://blog.csdn.net/***/article/details/*** (地址我就不贴了,对前辈需要最起码的尊敬) 一开始我用的是这个简化版本,后来发现有重复项...(demo:https://github.…
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题.. 今天,我们来扒一下cf的题面! PS:本代码不是我原创 1. 必要的分析 1.1 页面的获取 一般情况CF的每一个 contest 是这样的: 对应的URL是:http://codeforces.com/contest/xxx 还有一个Complete problemset页面,它是这样的:…
/// <summary> /// From: https://github.com/twitter/snowflake /// An object that generates IDs. /// This is broken into a separate class in case /// we ever want to support multiple worker threads /// per process /// </summary> public class IdW…
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n grid. In this game a ships are placed on the grid. Each of the ships consists of bconsecutive cells. No cell can be part of two ships, however, the shi…
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants to reach cinema. The film he has bought a ticket for starts in t minutes. There is a straight road of length s from the service to the cinema. Let's…
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interview down without punctuation marks and spaces to save time. Thus, the interview is now a string s consisting of n lowercase English letters. There is…