当前位置:网站首页>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 long does it take to obtain a PMP certificate?
- 他做国外LEAD,用了一年时间,把所有房贷都还清了
- Every time I look at the interface documents of my colleagues, I get confused and have a lot of problems...
- 香港珠宝大亨,22亿“抄底”佐丹奴
- Go pit - no required module provides Package: go. Mod file not found in current directory or any parent
- 如何在外地外网电脑远程公司项目?
- Using the uniapp rich text editor
- Expand your kubecl function
- go踩坑——no required module provides package : go.mod file not found in current directory or any parent
猜你喜欢
MIT-6.824-lab4B-2022(万字思路讲解-代码构建)
电力运维云平台:开启电力系统“无人值班、少人值守”新模式
Pytoch --- use pytoch to realize linknet for semantic segmentation
Font design symbol combination multifunctional wechat applet source code
It's too convenient. You can complete the code release and approval by nailing it!
[IELTS reading] Wang Xiwei reading P4 (matching1)
QT addition calculator (simple case)
Mit-6.824-lab4b-2022 (10000 word idea explanation - code construction)
IELTS examination process, what to pay attention to and how to review?
Application of fire fighting system based on 3D GIS platform
随机推荐
After Microsoft disables the IE browser, open the IE browser to flash back the solution
go踩坑——no required module provides package : go.mod file not found in current directory or any parent
Is it safe to open and register new bonds? Is there any risk? Is it reliable?
Illustrated network: what is gateway load balancing protocol GLBP?
[ODX studio edit PDX] -0.3- how to delete / modify inherited elements in variant variants
如何避免电弧产生?—— AAFD故障电弧探测器为您解决
图解网络:什么是网关负载均衡协议GLBP?
[kotlin] the third day
如果炒股开华泰证券的户,在网上开户安全吗?
Blue sky nh55 series notebook memory reading and writing speed is extremely slow, solution process record
如何将自己的代码作品快速存证,已更好的保护自己劳动成果
Tester's algorithm interview question - find mode
Nine Qi single chip microcomputer ny8b062d single key control four LED States
Ap8022 switching power supply small household appliances ACDC chip offline switching power supply IC
[binary tree] the maximum difference between a node and its ancestor
圖解網絡:什麼是網關負載均衡協議GLBP?
PermissionError: [Errno 13] Permission denied: ‘data. csv‘
How to apply for PMP project management certification examination?
如何有效对直流列头柜进行监测
How to do the project of computer remote company in foreign Internet?