当前位置:网站首页>P4408 [noi2003] truant children (tree diameter)
P4408 [noi2003] truant children (tree diameter)
2022-07-05 00:05:00 【eva_ can(not)survive】
[NOI2003] Kids playing truant - Luogu https://www.luogu.com.cn/problem/P4408 A good question for learning the diameter of trees .
The longest time required for the topic , Explain that the answer should be the diameter of the tree +max( The distance from the starting point to the nearest friend's home )
So we don't just ask for the diameter of the tree , Record the point on the diameter , And traverse them to find the longest extension distance .
#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 effectively monitor the DC column head cabinet
- 积分商城游戏设置的基本要点
- 打新债开户注册安全吗?有没有风险的?靠谱吗?
- 如果炒股开华泰证券的户,在网上开户安全吗?
- 认识ThreadPoolExecutor
- Skills in analyzing the trend chart of London Silver
- After Microsoft disables the IE browser, open the IE browser to flash back the solution
- PMP证书续证流程
- The input of uniapp is invalid except for numbers
猜你喜欢

Build your own minecraft server with fast parsing

海思3559万能平台搭建:YUV422的踩坑记录

如何在外地外网电脑远程公司项目?

Blue sky nh55 series notebook memory reading and writing speed is extremely slow, solution process record

Continuous modification of business scenario functions

公司要上监控,Zabbix 和 Prometheus 怎么选?这么选准没错!

Hong Kong Jewelry tycoon, 2.2 billion "bargain hunting" Giordano
![[paper reading] cavemix: a simple data augmentation method for brain vision segmentation](/img/41/eb790e7419a158e985fa503bd7dc17.png)
[paper reading] cavemix: a simple data augmentation method for brain vision segmentation

【kotlin】第三天

Consolidated expression C case simple variable operation
随机推荐
How to reduce the stock account Commission and stock speculation commission? Is it safe to open an online account
Jar批量管理小工具
Is the account opening link of Huatai Securities with low commission safe?
Significance of acrel EMS integrated energy efficiency platform in campus construction
Power operation and maintenance cloud platform: open the new mode of "unattended and few people on duty" of power system
企业应用业务场景,功能添加和修改C#源码
Application of multi loop instrument in base station "switching to direct"
XML的解析
Business implementation - the log is written to the same row of data
French scholars: the explicability of counter attack under optimal transmission theory
基于三维gis平台的消防系统运用
Some basic functions of enterprise projects are developed, and important things are saved to online first a
Go step on the pit - no required module provides package: go mod file not found in current directory or any parent
积分商城游戏设置的基本要点
Solve the problem that the virtual machine cannot be remotely connected through SSH service
Pytoch --- use pytoch to realize linknet for semantic segmentation
同事的接口文档我每次看着就头大,毛病多多。。。
S32 design studio for arm 2.2 quick start
如何有效对直流列头柜进行监测
Every time I look at the interface documents of my colleagues, I get confused and have a lot of problems...