传送门

考虑构造一些区间使得树尽可能的 "大"

发现这棵树最多就是一条链加上链上出去的其他边连接的点

构造的区间大概长这样(图比较丑请谅解..$qwq$,图中每一个 "└┘" 都是一段区间):

发现树其实就是个 "毛毛虫":传送门

所以直接求最大的毛毛虫即可

设毛毛虫的主链集合为 $S$ ,那么毛毛虫的点数就是 $\sum_{u \in S} (du[u])-(size-2)$

其中 $du[u]$ 表示点 $u$ 的度数,$size$ 为 $S$ 的点集大小,$-(size-2)$ 是因为除了主链两端的两点以外主链其他点都被算了两次

那么化一下即为 $\sum_{u \in S}(du[u]-1)+2$ 直接求树的带权直径即可

注意这个式子是在主链有两个端点的情况下成立的,所以对于只有一个点的情况要特判

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
const int N=3e5+;
int Q,n,val[N];
int fir[N],from[N<<],to[N<<],cntt;
inline void add(int a,int b) { from[++cntt]=fir[a]; fir[a]=cntt; to[cntt]=b; }
int mx,p;
void dfs(int x,int fa,int dis)
{
if(dis>mx) mx=dis,p=x;
for(int i=fir[x];i;i=from[i])
if(to[i]!=fa)
dfs(to[i],x, dis + val[to[i]] );
}
int main()
{
Q=read();
while(Q--)
{
n=read(); int a,b;
if(n==) { printf("1\n"); continue; }
for(int i=;i<=n;i++)
fir[i]=,val[i]=-;
cntt=p=; mx=-;
for(int i=;i<n;i++)
{
a=read(),b=read();
add(a,b); add(b,a);
val[a]++; val[b]++;
}
dfs(,,val[]); mx=-; dfs(p,,val[p]);
printf("%d\n",mx+);
}
return ;
}

Codeforces 1238F. The Maximum Subtree的更多相关文章

  1. [CF 1238F] The Maximum Subtree 树DP

    题意 给定一颗树,求这个树的最大子树,且这个子树是一个good-tree. good-tree的定义是:每个节点可以表示成一个数值区间,而树上的边表示两个点表示的数值区间相交. 题解 通过分析可以发现 ...

  2. CodeForces 173C Spiral Maximum 记忆化搜索 滚动数组优化

    Spiral Maximum 题目连接: http://codeforces.com/problemset/problem/173/C Description Let's consider a k × ...

  3. CodeForces 173C Spiral Maximum (想法、模拟)

    Spiral Maximum Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Sub ...

  4. CodeForces 1060 B Maximum Sum of Digits

    Maximum Sum of Digits You are given a positive integer n. Let S(x)S(x) be sum of digits in base 10 r ...

  5. codeforces 484B B. Maximum Value(二分)

    题目链接: B. Maximum Value time limit per test 1 second memory limit per test 256 megabytes input standa ...

  6. Codeforces 900 E. Maximum Questions (DP,技巧)

    题目链接:900 E. Maximum Questions 题意: 给出一个长度为n只含有a和b还有'?'的串s,且'?'可以被任意替换为a或b.再给出一个字符串t (奇数位上为a,偶数位上为b,所以 ...

  7. Codeforces 1038 E - Maximum Matching

    E - Maximum Matching 思路: 欧拉图 定理:一个度数为奇数的点的个数小于等于2的联通图存在欧拉回路 对于这道题目的图,点的个数为4,所以最坏的情况下4个点的度数都为奇数,在这种情况 ...

  8. Codeforces 1082 D. Maximum Diameter Graph-树的直径-最长链-构造题 (Educational Codeforces Round 55 (Rated for Div. 2))

    D. Maximum Diameter Graph time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  9. codeforces 702A A. Maximum Increase(水题)

    题目链接: A. Maximum Increase time limit per test 1 second memory limit per test 256 megabytes input sta ...

随机推荐

  1. JAVA之自动内存管理机制

    一.内存分配 1.JVM体系结构 2.运行时数据区域 3.内存分配二.内存回收 1.垃圾收集算法 2.垃圾收集器三.相关参考一.内存分配JVM体系结构 在了解自动内存管理的内存分配之前,我们先看下JV ...

  2. RocketMQ使用记录

    ---恢复内容开始--- he following softwares are assumed installed: 64bit OS, Linux/Unix/Mac is recommended; ...

  3. 从JMS到KafKa

    从JMS到KafKa JMS (1)JMS概念 JMS(Java Message Service,java消息服务)API是一个消息服务的标准或者说是规范,允许应用程序组件基于JavaEE平台创建.发 ...

  4. Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x0000000794500000, 576716800, 0)

    linux基于tomcat部署的web应用程序报 Java HotSpot(TM) 64-Bit Server VM warning: INFO: os::commit_memory(0x000000 ...

  5. shell脚本备份当前日期文件

    #!/bin/bash #一月前 historyTime=$(date "+%Y-%m-%d %H" -d '1 month ago') echo ${historyTime} h ...

  6. mysql数据库引擎——MyISAM,InnoDB

    作为一个java web开发人员,对于mysql数据库掌握到具体比较这两类引擎的差异也蛮拼的,下面就介绍一下我在工作中积累的对这两类引擎的理解. MyISAM: 如果不更改mysql配置文件(my.i ...

  7. vue-cli3.0 初体验

    vue-cli3.0 自我记录 其实在2018年8月10号,vue-cli3.0就已经面世了,由于项目中应用的全是2.x版本,所以并不了解3.0的vue-cli发生了什么变化,那今天尝试了下遇见的问题 ...

  8. 理解MyCat分库分表

    1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18.

  9. LC 781. Rabbits in Forest

    In a forest, each rabbit has some color. Some subset of rabbits (possibly all of them) tell you how ...

  10. 模型压缩-Learning Efficient Convolutional Networks through Network Slimming

    Zhuang Liu主页:https://liuzhuang13.github.io/ Learning Efficient Convolutional Networks through Networ ...