当前位置:网站首页>P3304 [SDOI2013]直径(树的直径)
P3304 [SDOI2013]直径(树的直径)
2022-07-05 00:02:00 【eva_can(not)survive】
[SDOI2013]直径 - 洛谷https://www.luogu.com.cn/problem/P3304本题先跑dfs找任意一条直径,并记录这条路径和其两端端点,首先从左端点开始枚举直径上的每一个点,记录该点走其他路径的最大路径值(非本直径的路),然后开始找这些最大路径值能否等于原来的那一条路(相当于分叉出一条路能否等于原来的那么长),然后找到该点后,然后从这个点开始判断其离右端点的(与上一样)
画的十分抽象,希望对你的理解有帮助
#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;
int ver[MAXN];
int head[MAXN];
int nxt[MAXN];
ll cost[MAXN];
int last[MAXN];
int nxxt[MAXN];
ll dis[MAXN], mmm[MAXN], op;
bool vis[MAXN];
int u, v;
int cnt;
void add(int x, int y, ll c) {
ver[++cnt] = y;
cost[cnt] = c;
nxt[cnt] = head[x];
head[x] = cnt;
}
void dfs1(int o, ll p, int q) {
if (p > op)
op = p, u = o;
for (int i = head[o]; i; i = nxt[i]) {
if (ver[i] == q)
continue;
dfs1(ver[i], p + cost[i], o);
}
}
void dfs2(int o, ll p, int q) {
last[o] = q;
dis[o] = p;
if (p > op)
op = p, v = o;
for (int i = head[o]; i; i = nxt[i]) {
if (ver[i] == q)
continue;
dfs2(ver[i], p + cost[i], o);
}
}
void dfs(int o, ll p, int q) {
if (p > op)
op = p;
for (int i = head[o]; i; i = nxt[i]) {
if (vis[ver[i]] || ver[i] == q)
continue;
dfs(ver[i], p + cost[i], o);
}
}
int main() {
scanf("%d", &n);
int x, y;
ll c;
for (int i = 1; i <= n - 1; i++) {
scanf("%d %d %lld", &x, &y, &c);
add(x, y, c);
add(y, x, c);
}
dfs1(1, 0, 0);
op = 0;
dfs2(u, 0, 0);
ll distance = dis[v];
printf("%lld\n", distance);
for (int i = v; i; i = last[i]) {
vis[i] = 1;
}
for (int i = v; i; i = last[i]) {
op = 0;
dfs(i, 0, 0);
mmm[i] = op;
}
for (int i = last[v], j = v; i; i = last[i]) {
nxxt[i] = j, j = i;
}
int tmp;
for (int i = u; i; i = nxxt[i]) {
if (dis[v] - dis[i] == mmm[i]) {
tmp = i;
break;
}
}
int ans = 0;
for (int i = tmp; i; i = last[i]) {
if (dis[i] == mmm[i])
break;
ans++;
}
printf("%d\n", ans);
return 0;
}
边栏推荐
- Fast parsing intranet penetration helps enterprises quickly achieve collaborative office
- ECCV 2022 | Tencent Youtu proposed disco: the effect of saving small models in self supervised learning
- Continuous modification of business scenario functions
- 图解网络:什么是网关负载均衡协议GLBP?
- Selected cutting-edge technical articles of Bi Ren Academy of science and technology
- Chinese verification of JS regular expressions (turn)
- S32 design studio for arm 2.2 quick start
- Ap8022 switching power supply small household appliances ACDC chip offline switching power supply IC
- 股票账户佣金怎么调低,炒股佣金怎么调低网上开户安全吗
- Significance of acrel EMS integrated energy efficiency platform in campus construction
猜你喜欢
Application of fire fighting system based on 3D GIS platform
Face recognition 5- insight face padding code practice notes
巩固表达式C# 案例简单变量运算
Design of emergency lighting evacuation indication system for urban rail transit station
JS how to realize array to tree
人脸识别5- insight-face-paddle-代码实战笔记
Consolidated expression C case simple variable operation
It's too convenient. You can complete the code release and approval by nailing it!
电力运维云平台:开启电力系统“无人值班、少人值守”新模式
[论文阅读] CarveMix: A Simple Data Augmentation Method for Brain Lesion Segmentation
随机推荐
Skills in analyzing the trend chart of London Silver
[Peking University] tensorflow2.0-1-opening
Actual combat simulation │ JWT login authentication
How to effectively monitor the DC column head cabinet
[JS] - [dynamic planning] - Notes
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
Design of emergency lighting evacuation indication system for urban rail transit station
Solve the problem that the virtual machine cannot be remotely connected through SSH service
如何避免电弧产生?—— AAFD故障电弧探测器为您解决
多回路仪表在基站“转改直”方面的应用
Significance of acrel EMS integrated energy efficiency platform in campus construction
Using fast parsing intranet penetration to realize zero cost self built website
Some basic functions of enterprise projects are developed, and important things are saved to online first a
如何用快解析自制IoT云平台
Pytoch --- use pytoch to realize linknet for semantic segmentation
Robot reinforcement learning synergies between pushing and grassing with self supervised DRL (2018)
[IELTS reading] Wang Xiwei reading P4 (matching1)
Selected cutting-edge technical articles of Bi Ren Academy of science and technology
如何将自己的代码作品快速存证,已更好的保护自己劳动成果
【雅思阅读】王希伟阅读P4(matching1)