当前位置:网站首页>P3304 [sdoi2013] diameter (diameter of tree)
P3304 [sdoi2013] diameter (diameter of tree)
2022-07-05 00:05:00 【eva_ can(not)survive】
[SDOI2013] The diameter of - Luogu https://www.luogu.com.cn/problem/P3304 Run first on this topic dfs Find any diameter , And record the path and the endpoints at both ends , First enumerate each point on the diameter starting from the left end point , Record the maximum path value of other paths at this point ( A road with a different diameter ), Then start to find out whether these maximum path values are equal to the original one ( It's equivalent to whether the forked road is as long as the original ), Then find the point , Then judge the distance from the right endpoint from this point ( Same as above )
The painting is very abstract , I hope it will help you understand
#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;
}
边栏推荐
- OSEK standard ISO_ 17356 summary introduction
- Specification for fs4061a boost 8.4v charging IC chip and fs4061b boost 12.6V charging IC chip datasheet
- 【路径规划】RRT增加动力模型进行轨迹规划
- XML的解析
- 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
- ECCV 2022 | Tencent Youtu proposed disco: the effect of saving small models in self supervised learning
- 快解析内网穿透帮助企业快速实现协同办公
- Every time I look at the interface documents of my colleagues, I get confused and have a lot of problems...
- JS 将伪数组转换成数组
- PermissionError: [Errno 13] Permission denied: ‘data. csv‘
猜你喜欢
使用快解析搭建自己的minecraft服务器
[论文阅读] TUN-Det: A Novel Network for Thyroid Ultrasound Nodule Detection
"Xiaodeng" domain password policy enhancer in operation and maintenance
Réseau graphique: Qu'est - ce que le Protocole d'équilibrage de charge de passerelle glbp?
Verilog tutorial (11) initial block in Verilog
Robot reinforcement learning synergies between pushing and grassing with self supervised DRL (2018)
Jar batch management gadget
Some basic functions of enterprise projects are developed, and important things are saved to online first a
Date time type and format in MySQL
如何有效对直流列头柜进行监测
随机推荐
Go pit - no required module provides Package: go. Mod file not found in current directory or any parent
The pit of sizeof operator in C language
Ap8022 switching power supply small household appliances ACDC chip offline switching power supply IC
基于三维gis平台的消防系统运用
go踩坑——no required module provides package : go.mod file not found in current directory or any parent
Using the uniapp rich text editor
香港珠宝大亨,22亿“抄底”佐丹奴
Go step on the pit - no required module provides package: go mod file not found in current directory or any parent
Fs8b711s14 electric wine bottle opener MCU IC scheme development special integrated IC
Is it safe to open an account in the College of Finance and economics? How to open an account?
跨域请求
AcWing164. 可达性统计(拓扑排序+bitset)
端口映射和端口转发区别是什么
[ODX studio edit PDX] -0.3- how to delete / modify inherited elements in variant variants
Netcore3.1 JSON web token Middleware
Illustrated network: what is gateway load balancing protocol GLBP?
Etcd database source code analysis - brief process of processing entry records
js如何实现数组转树
How to avoid arc generation—— Aafd fault arc detector solves the problem for you
如何在外地外网电脑远程公司项目?