当前位置:网站首页>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;
}边栏推荐
- Is the account opening link of Huatai Securities with low commission safe?
- 打新债开户注册安全吗?有没有风险的?靠谱吗?
- 如何用快解析自制IoT云平台
- Enterprise application business scenarios, function addition and modification of C source code
- Every time I look at the interface documents of my colleagues, I get confused and have a lot of problems...
- Business implementation - the log is written to the same row of data
- [论文阅读] TUN-Det: A Novel Network for Thyroid Ultrasound Nodule Detection
- Instructions for go defer
- Blue sky nh55 series notebook memory reading and writing speed is extremely slow, solution process record
- [binary tree] the maximum difference between a node and its ancestor
猜你喜欢

业务场景功能的继续修改

In the enterprise, win10 turns on BitLocker to lock the disk, how to back up the system, how to recover when the system has problems, and how to recover quickly while taking into account system securi

Fast analysis -- easy to use intranet security software

Selected cutting-edge technical articles of Bi Ren Academy of science and technology

Robot reinforcement learning synergies between pushing and grassing with self supervised DRL (2018)

Some basic functions of enterprise projects are developed, and important things are saved to online first a

uniapp 除了数字,其他输入无效

QT addition calculator (simple case)

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

How many triangles are there in the golden K-line diagram?
随机推荐
香港珠宝大亨,22亿“抄底”佐丹奴
The pit of sizeof operator in C language
企业应用业务场景,功能添加和修改C#源码
It's too convenient. You can complete the code release and approval by nailing it!
How to effectively monitor the DC column head cabinet
Solve the problem that the virtual machine cannot be remotely connected through SSH service
图解网络:什么是网关负载均衡协议GLBP?
Using the uniapp rich text editor
Blue sky nh55 series notebook memory reading and writing speed is extremely slow, solution process record
机器人强化学习——Learning Synergies between Pushing and Grasping with Self-supervised DRL (2018)
Continuous modification of business scenario functions
IELTS examination process, what to pay attention to and how to review?
How many triangles are there in the golden K-line diagram?
[paper reading] Tun det: a novel network for meridian ultra sound nodule detection
Summary of week 22-07-02
业务场景功能的继续修改
股票账户佣金怎么调低,炒股佣金怎么调低网上开户安全吗
In June, the list of winners of "Moli original author program" was announced! Invite you to talk about the domestic database
How to do the project of computer remote company in foreign Internet?
Chinese verification of JS regular expressions (turn)