当前位置:网站首页>【集训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;
}
边栏推荐
- Virtual memory and disk
- What is the difference between minor GC and full GC?
- 编译和反编译
- Data governance under data platform
- Three ways to allocate disk space
- Matrix of C language
- Call of addition, subtraction, multiplication and division of integer type only
- IPv4 addresses have been completely exhausted, and the Internet can work normally. NAT is the greatest contributor!
- Build commercial projects based on ruoyi framework
- Based on if nesting and function call
猜你喜欢

Title: give a group of arrays, arranged from large to small and from small to large.

How to resolve a domain name to multiple IP addresses?

xss-工具-Beef-Xss安装以及使用

Imitation Tiktok homepage interface

Win10 set up a flutter environment to step on the pit diary

数据库进阶·如何针对所有用户数据中没有的数据去加入随机的数据-蜻蜓Q系统用户没有头像如何加入头像数据-优雅草科技kir

Smart S7-200 PLC channel free mapping function block (do_map)

Flex layout

Interpretation of the source code of all logging systems in XXL job (line by line source code interpretation)

ThreadLocal 总结(未完待续)
随机推荐
Synchronized and volatile
How is it most convenient to open an account for stock speculation? Is it safe for online account managers to open an account
『Skywalking』.NET Core快速接入分布式链路追踪平台
分割金条的代价
3dslicer importing medical image data
对需求的内容进行jieba分词并按词频排序输出excel文档
H5幸运刮刮乐抽奖 免公众号+直运营
IFLYTEK smart office book air e-book reader makes my work life healthier
Imitation Tiktok homepage interface
Don't know mock test yet? An article to familiarize you with mock
Build commercial projects based on ruoyi framework
MapGIS格式转ArcGIS方法
According to the use and configuration of data permissions in the open source framework
QML module not found
Ffmpeg plays audio and video, time_ Base solves the problem of audio synchronization and SDL renders the picture
It's over. I went to work for three months and became bald
El expression improves JSP
Xiaobai programmer's sixth day
mysql: error while loading shared libraries: libncurses.so.5: cannot open shared object file: No suc
3day