当前位置:网站首页>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;
}
边栏推荐
- "Xiaodeng" domain password policy enhancer in operation and maintenance
- 青海省国家湿地公园功能区划数数据、全国湿地沼泽分布数据、全国省市县自然保护区
- The pit of sizeof operator in C language
- OSEK standard ISO_ 17356 summary introduction
- Skills in analyzing the trend chart of London Silver
- 股票账户佣金怎么调低,炒股佣金怎么调低网上开户安全吗
- Remember to build wheels repeatedly at one time (the setting instructions of obsidian plug-in are translated into Chinese)
- go踩坑——no required module provides package : go.mod file not found in current directory or any parent
- Nine Qi single chip microcomputer ny8b062d single key control four LED States
- Réseau graphique: Qu'est - ce que le Protocole d'équilibrage de charge de passerelle glbp?
猜你喜欢
Design of emergency lighting evacuation indication system for urban rail transit station
[kotlin] the third day
How many triangles are there in the golden K-line diagram?
Face recognition 5- insight face padding code practice notes
Acrel-EMS综合能效平台在校园建设的意义
Mit-6.824-lab4b-2022 (10000 word idea explanation - code construction)
How to effectively monitor the DC column head cabinet
OSEK standard ISO_ 17356 summary introduction
Consolidated expression C case simple variable operation
uniapp微信小程序拿来即用的瀑布流布局demo2(方法二)(复制粘贴即可使用,无需做其他处理)
随机推荐
Expand your kubecl function
Jar批量管理小工具
Build your own minecraft server with fast parsing
取得PMP證書需要多長時間?
[IELTS reading] Wang Xiwei reading P3 (heading)
[monitoring] ZABBIX
模板的进阶
城市轨道交通站应急照明疏散指示系统设计
He worked as a foreign lead and paid off all the housing loans in a year
French scholars: the explicability of counter attack under optimal transmission theory
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
P4408 [NOI2003] 逃学的小孩(树的直径)
js正则表达式之中文验证(转)
如何在外地外网电脑远程公司项目?
多回路仪表在基站“转改直”方面的应用
P4281 [AHOI2008]紧急集合 / 聚会(LCA)
[论文阅读] TUN-Det: A Novel Network for Thyroid Ultrasound Nodule Detection
Fs8b711s14 electric wine bottle opener MCU IC scheme development special integrated IC
Advanced template
[论文阅读] CarveMix: A Simple Data Augmentation Method for Brain Lesion Segmentation