随笔分类 - 树上乱搞---树的直径
摘要:题意:给定N点N边的无向连通图,现在让你在图中找一点作为餐厅,使得最远点距离这点最近。 思路:为了保留整数,我们求最小直径,最后去除2。 直径来源于两部分: 1,在外向树中; 那么就是树的直接,一棵树求直径直接跑一边DP就可以了。(或者两遍BFS吧,麻烦一点而已),假设最大值为ans1。 2,可能通
阅读全文
摘要:Description 从前有棵树。 找出K个点A1,A2,…,Ak。 使得∑dis(AiAi+1),(1<=i<=K-1)最小。 从前有棵树。 找出K个点A1,A2,…,Ak。 使得∑dis(AiAi+1),(1<=i<=K-1)最小。 Input 第一行两个正整数n,k,表示数的顶点数和需要选出
阅读全文
摘要:Ari the monster is not an ordinary monster. She is the hidden identity of Super M, the Byteforces’ superhero. Byteforces is a country that consists of
阅读全文
摘要:Your friend is developing a computer game. He has already decided how the game world should look like — it should consist of nn locations connected by
阅读全文
摘要:Eagle (AKA Mohamed Ahmed) lives in a city consists of n intersections connected by n-1 roads, in a way that can go from any intersection to any other
阅读全文
摘要:The northern part of the Pyramid contains a very large and complicated labyrinth. The labyrinth is divided into square blocks, each of them either fil
阅读全文
摘要:题意:求出的树中距离最远的两个结点之间相隔的距离。 水题一道,以前只会用路的直径来解。 代码如下: 树形DP:
阅读全文
摘要:肯定会想到树的直径: 如果直径够长,就在直径(1+8)上面找路径,ans=k。 如果不够长,肯定会在有点分叉点(如3,4,5)回溯,然后我们把路径拉直,把其中一条的作为主线(有机化学,ORZ),主线是线走一遍,而分支走两遍,所以要想答案越小,主线就要求越长,也就是树的直径d,ans=d+(k-d)*
阅读全文
摘要:首先把无向图变成一棵树,直径肯定由叶子组成。 有以下两种情况: 第一种:经过根节点,则找两个最远的叶子肯定是直径,也就是B+D。 第二种:不经过根节点,则目标的两个叶子肯定有一个不为根的公共祖先,如红点O,则在红点O下面找两个最远的叶子作为直径,找到了C+F。而很明显,这两个目标叶子中的其中一个(F
阅读全文