当前位置:网站首页>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;
}边栏推荐
- 【雅思阅读】王希伟阅读P4(matching2段落信息配对题【困难】)
- 圖解網絡:什麼是網關負載均衡協議GLBP?
- 企业公司项目开发好一部分基础功能,重要的事保存到线上第一a
- Five papers recommended for the new development of convolutional neural network in deep learning
- Hash table, hash function, bloom filter, consistency hash
- Data on the number of functional divisions of national wetland parks in Qinghai Province, data on the distribution of wetlands and marshes across the country, and natural reserves in provinces, cities
- Solution record of jamming when using CAD to move bricks in high configuration notebook
- Chinese verification of JS regular expressions (turn)
- uniapp 除了数字,其他输入无效
- Introduction to ACM combination counting
猜你喜欢

雅思考试流程、需要具体注意些什么、怎么复习?

Microservice

同事的接口文档我每次看着就头大,毛病多多。。。

How to effectively monitor the DC column head cabinet
![[IELTS reading] Wang Xiwei reading P4 (matching1)](/img/91/1b3f85410035f65acb0c205185f698.png)
[IELTS reading] Wang Xiwei reading P4 (matching1)

「运维有小邓」域密码策略强化器

XML的解析

45 year old professor, she threw two super unicorns

It's too convenient. You can complete the code release and approval by nailing it!

取得PMP证书需要多长时间?
随机推荐
股票账户佣金怎么调低,炒股佣金怎么调低网上开户安全吗
How to use fast parsing to make IOT cloud platform
Ap8022 switching power supply small household appliances ACDC chip offline switching power supply IC
[JS] - [sort related] - Notes
Specification for fs4061a boost 8.4v charging IC chip and fs4061b boost 12.6V charging IC chip datasheet
取得PMP證書需要多長時間?
C语言中sizeof操作符的坑
Solve the problem that the virtual machine cannot be remotely connected through SSH service
企业公司项目开发好一部分基础功能,重要的事保存到线上第一a
js如何实现数组转树
雅思考试流程、需要具体注意些什么、怎么复习?
How long does it take to obtain a PMP certificate?
企业应用业务场景,功能添加和修改C#源码
How to reduce the stock account Commission and stock speculation commission? Is it safe to open an online account
Go pit - no required module provides Package: go. Mod file not found in current directory or any parent
用快解析内网穿透实现零成本自建网站
打新债开户注册安全吗?有没有风险的?靠谱吗?
城市轨道交通站应急照明疏散指示系统设计
Summary of week 22-07-02
uniapp上传头像