当前位置:网站首页>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;
}
边栏推荐
- [论文阅读] TUN-Det: A Novel Network for Thyroid Ultrasound Nodule Detection
- How to do the project of computer remote company in foreign Internet?
- How to save your code works quickly to better protect your labor achievements
- [论文阅读] CarveMix: A Simple Data Augmentation Method for Brain Lesion Segmentation
- 业务实现-日志写到同一个行数据里面
- 【kotlin】第三天
- ICML 2022 | 3dlinker: e (3) equal variation self encoder for molecular link design
- How to use fast parsing to make IOT cloud platform
- Instructions for go defer
- 【路径规划】RRT增加动力模型进行轨迹规划
猜你喜欢
企业公司项目开发好一部分基础功能,重要的事保存到线上第一a
Why does infographic help your SEO
[论文阅读] TUN-Det: A Novel Network for Thyroid Ultrasound Nodule Detection
[kotlin] the third day
快解析内网穿透帮助企业快速实现协同办公
How to effectively monitor the DC column head cabinet
Hong Kong Jewelry tycoon, 2.2 billion "bargain hunting" Giordano
OSEK standard ISO_ 17356 summary introduction
Netcore3.1 JSON web token Middleware
ICML 2022 | 3dlinker: e (3) equal variation self encoder for molecular link design
随机推荐
How to effectively monitor the DC column head cabinet
Is the account opening link of Huatai Securities with low commission safe?
Paddleocr tutorial
如何在外地外网电脑远程公司项目?
Tester's algorithm interview question - find mode
"Xiaodeng" domain password policy enhancer in operation and maintenance
The company needs to be monitored. How do ZABBIX and Prometheus choose? That's the right choice!
Meet ThreadPoolExecutor
Life is changeable, and the large intestine covers the small intestine. This time, I can really go home to see my daughter-in-law...
如果炒股开华泰证券的户,在网上开户安全吗?
What is the difference between port mapping and port forwarding
蓝天NH55系列笔记本内存读写速度奇慢解决过程记录
Introduction to ACM combination counting
Build your own minecraft server with fast parsing
XML的解析
Cross domain request
Why does infographic help your SEO
微服务(Microservice)那点事儿
Some basic functions of enterprise projects are developed, and important things are saved to online first a
巩固表达式C# 案例简单变量运算