当前位置:网站首页>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;
}
边栏推荐
- 端口映射和端口转发区别是什么
- 雅思考试流程、需要具体注意些什么、怎么复习?
- Is it safe to open an account in the College of Finance and economics? How to open an account?
- Go pit - no required module provides Package: go. Mod file not found in current directory or any parent
- IT转测试岗,从迷茫到坚定我究竟付出了什么?
- Mit-6.824-lab4b-2022 (10000 word idea explanation - code construction)
- 【监控】zabbix
- [ODX studio edit PDX] -0.3- how to delete / modify inherited elements in variant variants
- 基于三维gis平台的消防系统运用
- [Peking University] tensorflow2.0-1-opening
猜你喜欢
Blue sky nh55 series notebook memory reading and writing speed is extremely slow, solution process record
Every time I look at the interface documents of my colleagues, I get confused and have a lot of problems...
圖解網絡:什麼是網關負載均衡協議GLBP?
[IELTS reading] Wang Xiwei reads P4 (matching2 paragraph information matching question [difficult])
【kotlin】第三天
如何避免电弧产生?—— AAFD故障电弧探测器为您解决
How to avoid arc generation—— Aafd fault arc detector solves the problem for you
Significance of acrel EMS integrated energy efficiency platform in campus construction
How long does it take to obtain a PMP certificate?
Tester's algorithm interview question - find mode
随机推荐
Fs8b711s14 electric wine bottle opener MCU IC scheme development special integrated IC
用快解析内网穿透实现零成本自建网站
How to reduce the stock account Commission and stock speculation commission? Is it safe to open an online account
Remember to build wheels repeatedly at one time (the setting instructions of obsidian plug-in are translated into Chinese)
Is the account opening link of Huatai Securities with low commission safe?
Some basic functions of enterprise projects are developed, and important things are saved to online first a
Actual combat simulation │ JWT login authentication
C语言中sizeof操作符的坑
业务场景功能的继续修改
OpenHarmony资源管理详解
如果炒股开华泰证券的户,在网上开户安全吗?
Jar batch management gadget
How to apply for PMP project management certification examination?
Parsing of XML
In June, the list of winners of "Moli original author program" was announced! Invite you to talk about the domestic database
Data on the number of functional divisions of national wetland parks in Qinghai Province, data on the distribution of wetlands and marshes across the country, and natural reserves in provinces, cities
[IELTS reading] Wang Xiwei reads P4 (matching2 paragraph information matching question [difficult])
Face recognition 5- insight face padding code practice notes
如何用快解析自制IoT云平台
如何将自己的代码作品快速存证,已更好的保护自己劳动成果