当前位置:网站首页>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;
}边栏推荐
- Build your own minecraft server with fast parsing
- ECCV 2022 | 腾讯优图提出DisCo:拯救小模型在自监督学习中的效果
- [IELTS reading] Wang Xiwei reads P4 (matching2 paragraph information matching question [difficult])
- PermissionError: [Errno 13] Permission denied: ‘data. csv‘
- A new method for analyzing the trend chart of London Silver
- 挖财学院开户安全的吗?开户怎么开?
- Microservice
- 如何报考PMP项目管理认证考试?
- How to save your code works quickly to better protect your labor achievements
- Introduction to ACM combination counting
猜你喜欢

业务场景功能的继续修改

取得PMP证书需要多长时间?

Specification for fs4061a boost 8.4v charging IC chip and fs4061b boost 12.6V charging IC chip datasheet

How long does it take to obtain a PMP certificate?

Illustrated network: what is gateway load balancing protocol GLBP?

PMP certificate renewal process

如何用快解析自制IoT云平台

In June, the list of winners of "Moli original author program" was announced! Invite you to talk about the domestic database

企业里Win10 开启BitLocker锁定磁盘,如何备份系统,当系统出现问题又如何恢复,快速恢复又兼顾系统安全(远程设备篇)

Mit-6.824-lab4b-2022 (10000 word idea explanation - code construction)
随机推荐
How to effectively monitor the DC column head cabinet
如何在外地外网电脑远程公司项目?
[IELTS reading] Wang Xiwei reads P4 (matching2 paragraph information matching question [difficult])
How many triangles are there in the golden K-line diagram?
[JS] - [sort related] - Notes
Specification for fs4061a boost 8.4v charging IC chip and fs4061b boost 12.6V charging IC chip datasheet
蓝天NH55系列笔记本内存读写速度奇慢解决过程记录
How to reduce the stock account Commission and stock speculation commission? Is it safe to open an online account
Consolidated expression C case simple variable operation
如何用快解析自制IoT云平台
C language to quickly solve the reverse linked list
ICML 2022 || 3DLinker: 用于分子链接设计的E(3)等变变分自编码器
挖财学院开户安全的吗?开户怎么开?
go踩坑——no required module provides package : go.mod file not found in current directory or any parent
积分商城游戏设置的基本要点
PMP certificate renewal process
快解析——好用的内网安全软件
Chinese verification of JS regular expressions (turn)
业务实现-日志写到同一个行数据里面
45 year old professor, she threw two super unicorns