当前位置:网站首页>P4408 [NOI2003] 逃学的小孩(树的直径)
P4408 [NOI2003] 逃学的小孩(树的直径)
2022-07-05 00:02:00 【eva_can(not)survive】
[NOI2003] 逃学的小孩 - 洛谷https://www.luogu.com.cn/problem/P4408一道学习树的直径的好题。
题目要求最长的时间,说明答案应该是树的直径+max(起点离最近的朋友家的距离)
所以我们不仅仅要求树的直径,更要记录直径上的点,并遍历它们求最长延申距离。
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cstring>
#include <set>
#include <cmath>
#include <map>
#include <bitset>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int MN = 65005;
const int MAXN = 1e6 + 10;
const int INF = 0x3f3f3f3f;
#define IOS ios::sync_with_stdio(false)
#define lowbit(x) ((x)&(-x))
using P = pair<int, int>;
int n, m;
int head[MAXN];
int nxt[MAXN];
int ver[MAXN];
ll cost[MAXN];
ll dis[MAXN];
int last[MAXN];
int cnt;
void add(int x, int y, ll c) {
ver[++cnt] = y;
nxt[cnt] = head[x];
cost[cnt] = c;
head[x] = cnt;
}
ll ans;
ll maxlen;
int recv, recu;
bool vis[MAXN];
void dfs(int p, ll rec, int fa) {
if (maxlen < rec)
maxlen = rec, recv = p;
for (int i = head[p]; i; i = nxt[i]) {
int v = ver[i];
if (v == fa)
continue;
// dis[v]=dis[p]+cost[i];
dfs(v, rec + cost[i], p);
}
}
void dfs1(int p, int fa) {
last[p] = fa;
if (maxlen < dis[p])
maxlen = dis[p], recu = p;
for (int i = head[p]; i; i = nxt[i]) {
int v = ver[i];
if (v == fa)
continue;
dis[v] = dis[p] + cost[i];
dfs1(v, p);
}
}
ll mxx[MAXN];
ll mx;
void dfs2(int p, ll rec, int fa) {
mx = max(mx, rec);
for (int i = head[p]; i; i = nxt[i]) {
int v = ver[i];
if (vis[v] || v == fa)
continue;
dfs2(v, rec + cost[i], p);
}
}
int main() {
scanf("%d %d", &n, &m);
int x, y;
ll c;
for (int i = 1; i <= m; i++) {
scanf("%d %d %lld", &x, &y, &c);
add(x, y, c);
add(y, x, c);
}
dfs(1, 0, 0);
maxlen = 0;
dfs1(recv, 0);
ll tmp = 0;
for (int i = recu; i; i = last[i]) {
vis[i] = 1;
}
for (int i = recu; i; i = last[i]) {
mx = 0;
dfs2(i, 0, 0);
mxx[i] = mx;
}
mx = 0;
for (int i = recu; i; i = last[i]) {
tmp = min(dis[i], maxlen - dis[i]);
mx = max(mx, mxx[i] + tmp);
}
printf("%lld\n", mx + maxlen);
return 0;
}
边栏推荐
- How to save your code works quickly to better protect your labor achievements
- ICML 2022 | 3dlinker: e (3) equal variation self encoder for molecular link design
- 打新债开户注册安全吗?有没有风险的?靠谱吗?
- Expand your kubecl function
- Best practice case of enterprise digital transformation: introduction and reference of cloud based digital platform system security measures
- 【雅思阅读】王希伟阅读P4(matching1)
- 【北京大学】Tensorflow2.0-1-开篇
- 认识ThreadPoolExecutor
- How to effectively monitor the DC column head cabinet
- js如何实现数组转树
猜你喜欢

【雅思阅读】王希伟阅读P4(matching2段落信息配对题【困难】)
![[IELTS reading] Wang Xiwei reads P4 (matching2 paragraph information matching question [difficult])](/img/83/63296108b47eda37c19b9ff9deb5ec.png)
[IELTS reading] Wang Xiwei reads P4 (matching2 paragraph information matching question [difficult])

uniapp 除了数字,其他输入无效

他做国外LEAD,用了一年时间,把所有房贷都还清了

Design of emergency lighting evacuation indication system for urban rail transit station

How to apply for PMP project management certification examination?

【路径规划】RRT增加动力模型进行轨迹规划

Skills in analyzing the trend chart of London Silver

Why does infographic help your SEO

Pytoch --- use pytoch to realize linknet for semantic segmentation
随机推荐
[paper reading] cavemix: a simple data augmentation method for brain vision segmentation
Why does infographic help your SEO
S32 design studio for arm 2.2 quick start
股票账户佣金怎么调低,炒股佣金怎么调低网上开户安全吗
How long does it take to obtain a PMP certificate?
Pytoch --- use pytoch to realize linknet for semantic segmentation
Etcd database source code analysis - brief process of processing entry records
In June, the list of winners of "Moli original author program" was announced! Invite you to talk about the domestic database
Chinese verification of JS regular expressions (turn)
端口映射和端口转发区别是什么
[论文阅读] TUN-Det: A Novel Network for Thyroid Ultrasound Nodule Detection
快解析——好用的内网安全软件
js如何实现数组转树
电力运维云平台:开启电力系统“无人值班、少人值守”新模式
[IELTS reading] Wang Xiwei reads P4 (matching2 paragraph information matching question [difficult])
[论文阅读] CarveMix: A Simple Data Augmentation Method for Brain Lesion Segmentation
Réseau graphique: Qu'est - ce que le Protocole d'équilibrage de charge de passerelle glbp?
PMP证书续证流程
雅思考试流程、需要具体注意些什么、怎么复习?
【雅思阅读】王希伟阅读P4(matching2段落信息配对题【困难】)