当前位置:网站首页>【集训DAY15】Boring【树形DP】
【集训DAY15】Boring【树形DP】
2022-07-25 22:24:00 【VL——MOESR】

思路:
直接从最大的儿子跳到次大的儿子就完成了转移
c o d e code code
#include<iostream>
#include<cstdio>
using namespace std;
const int MAXN = 1e6 + 10;
int n, m, ru[MAXN], maxx[MAXN], f[MAXN];
int head[MAXN], tot, ans;
struct node {
int to, next;
}b[MAXN * 2];
void add(int x, int y) {
b[++ tot] = (node) {
y, head[x] };
head[x] = tot;
}
void dfs(int x, int fa) {
int d = 0;
for(int i = head[x]; i; i = b[i].next) {
int y = b[i].to;
if(y == fa) continue;
d ++;
dfs(y, x);
}
if(d == 0) {
f[x] = 1; maxx[x] = 1; }
if(d == 1) {
for(int i = head[x]; i; i = b[i].next) {
int y = b[i].to;
if(y == fa) continue;
maxx[x] = maxx[y] + 1;
f[x] = maxx[x];
}
}
if(d >= 2) {
int M = 0, CM = 0;
for(int i = head[x]; i; i = b[i].next) {
int y = b[i].to;
if(y == fa) continue;
if(maxx[y] > M) CM = M, M = maxx[y];
else if(maxx[y] == M) CM = M;
else if(maxx[y] > CM) CM = maxx[y];
}
maxx[x] = M + d;
f[x] = CM + M + d - 1;
}
if(fa != 0)
ans = max(ans, max(maxx[x], f[x] + 1));
else ans = max(ans, max(maxx[x], f[x]));
}
int main() {
scanf("%d%d", &n, &m);
for(int i = 1; i <= m; i ++) {
int x, y;
scanf("%d%d", &x, &y);
add(x, y), add(y, x), ru[x] ++, ru[y] ++;
}
dfs(1, 0);
printf("%d", ans);
return 0;
}
边栏推荐
- Perform Jieba word segmentation on the required content and output EXCEL documents according to word frequency
- How to implement an app application to limit users' time use?
- Div drag effect
- Selenium basic use and use selenium to capture the recruitment information of a website (continuously updating)
- What is class loading? Class loading process?
- 3dslicer importing medical image data
- IPv4 addresses have been completely exhausted, and the Internet can work normally. NAT is the greatest contributor!
- Redis memory elimination mechanism?
- Mitsubishi FX PLC free port RS command realizes Modbus Communication
- Math programming classification
猜你喜欢

What is partition and barrel division?

PySpark数据分析基础:pyspark.sql.SparkSession类方法详解及操作+代码展示

3dslicer introduction and installation tutorial

4day

ML-Numpy

Whether the five distribution methods will produce internal fragments and external fragments

6-18 vulnerability exploitation - backdoor connection

分享两个音乐播放地址

『SignalR』. Net using signalr for real-time communication

Perform Jieba word segmentation on the required content and output EXCEL documents according to word frequency
随机推荐
SQL中in的用法 DQL 查询
Whether the five distribution methods will produce internal fragments and external fragments
Wechat applet application development competition works comprehensive development record - Jinlu cultural tourism (cloud development - Overview)
Xiaobai programmer the next day
TFrecord写入与读取
分割金条的代价
Wechat applet (anti shake, throttling), which solves the problem that users keep pulling down refresh requests or clicking buttons to submit information; Get the list information and refresh the data
[go basics 02] the first procedure
Wkid in ArcGIS
Compile and decompile
核电站在席卷欧洲的热浪中努力保持安全工作
Wechat official account application development (I)
synchronized与volatile
If it is modified according to the name of the framework module
Jenkins+svn configuration
What have I experienced to become a harder tester than development?
访问者模式(visitor)模式
完啦,上班三个月,变秃了
xxl-job中 关于所有日志系统的源码的解读(一行一行源码解读)
Xiaobai programmer's sixth day