当前位置:网站首页>Graph theory (tree diameter)
Graph theory (tree diameter)
2022-06-23 23:26:00 【D αī s ч】
One 、 Basics
The distance between the two farthest nodes in a tree is called the diameter of the tree , The path connecting these two points is called the longest chain of the tree ( Also called diameter )
Two 、 Twice traversal to find the diameter
1、 Starting from any node , adopt bfs perhaps dfs Traverse the tree once , Find the node farthest from the starting point , Write it down as p, This is the diameter end of the tree
2、 From the node p set out , adopt bfs and dfs Traverse the tree again , Work out with p The farthest node , Write it down as q
3、 that p To q The path of is a diameter of the tree , because p It must be the end of the diameter , Otherwise, a longer chain will always be found . In the second step of traversal , You can record the precursor node of each point when it is accessed for the first time , Finally from the q Go back to p The specific scheme of diameter can be obtained
4、 notes : The problem of negative edge weight cannot be solved by searching
int to[maxn << 1], nex[maxn << 1], edge[maxn << 1], head[maxn];
int cnt, n, m, r;
ll dis[maxn], maxd, maxv;
void add(int u, int v, int w)
{
to[++cnt] = v;
edge[cnt] = w;
nex[cnt] = head[u];
head[u] = cnt;
}
void dfs(int u, int fa)
{
if (maxd < dis[u])
{
maxd = dis[u];
maxv = u;
}
for (int i = head[u]; i; i = nex[i])
{
int v = to[i];
if (fa == v)
continue;
dis[v] = dis[u] + edge[i];
dfs(v, u);
}
}
void solve()
{
cin >> n;
for (int i = 0; i < n - 1; i++)
{
int u, v, w;
cin >> u >> v >> w;
add(u, v, w);
add(v, u, w);
}
dfs(1, 0);
maxd = 0;
dis[maxv] = 0;
dfs(maxv, 0);
cout << maxd;
}3、 ... and 、 Tree form dp Find the diameter
1、 set up d[x] Represents a slave node x Set out to x Root subtree , The distance of the farthest node that can be reached , namely x The longest chain that goes down , Obviously there is
![d[x]=\underset {1\leq i \leq t}{max}\{d[y_i]+w(x,y_i)]\}](http://img.inotgo.com/imagesLocal/202206/23/202206232006247781_0.gif)
2、 So we can think about finding each point through x The length of the longest chain f[x], Then the diameter of the whole tree is the largest f[x]. about x Any two nodes of yi,yj, Through the nodes x The length of the longest chain consists of four parts , as follows
![f[x]=\underset {1\leq j<i\leq t}{max}\{d[y_i]+d[y_j]+w(x,y_i)+w(x,y_j)\}](http://img.inotgo.com/imagesLocal/202206/23/202206232006247781_3.gif)
3、 When child nodes are enumerated to i when ,d[x] Just saved from x Departure direction yi Is the distance of the farthest node that can be reached by the subtree of the root , This distance is
, So let's use
to update f[x], Reuse
to update d[x] that will do
int to[maxn << 1], nex[maxn << 1], edge[maxn << 1], head[maxn];
int vis[maxn];
int cnt, n, m, r;
ll ans = 0, d[maxn];
void add(int u, int v, int w)
{
to[++cnt] = v;
edge[cnt] = w;
nex[cnt] = head[u];
head[u] = cnt;
}
void dp(int u)
{
vis[u] = 1;
for (int i = head[u]; i; i = nex[i])
{
int v = to[i];
if (vis[v])
continue;
dp(v);
ans = max(ans, d[u] + d[v] + edge[i]);
d[u] = max(d[u], d[v] + edge[i]);
}
}
void solve()
{
cin >> n;
for (int i = 0; i < n - 1; i++)
{
int u, v, w;
cin >> u >> v >> w;
add(u, v, w);
add(v, u, w);
}
dp(1);
cout << ans << '\n';
}边栏推荐
- Installation and use of qingscan scanner
- The Sandbox 归属周来啦!
- How can manufacturing enterprises go to the cloud?
- Application of clock synchronization system in banking system
- C # read the occupied size of memory module and hard disk
- Ant group's self-developed tee technology has passed the national financial technology product certification
- What to check for AIX system monthly maintenance (II)
- 【观察】戴尔科技+英特尔傲腾技术:以“纳秒之速”领跑存储创新
- Payment industry tuyere project: smart digital operation 3.0
- WebService client request failed can not create a secure xmlinputfactory
猜你喜欢

Ant group's self-developed tee technology has passed the national financial technology product certification

CS5213 HDMI转VGA带音频信号输出方案

Develop synergy and efficiently manage | community essay solicitation

C # read the occupied size of memory module and hard disk

什么是免疫组织化学实验? 免疫组织化学实验

谈谈数字化转型晓知识
Several cases of index invalidation caused by MySQL

数据解读!理想L9冲刺「月销过万」,从BBA手中抢份额

Save: software analysis, verification and test platform

对不起,你的USB走线可能搞错了!
随机推荐
Go deep: the evolution of garbage collection
Come on, touch and write a hook
【观察】戴尔科技+英特尔傲腾技术:以“纳秒之速”领跑存储创新
国家邮政局等三部门:加强涉邮政快递个人信息安全治理,推行隐私面单、虚拟号码等个人信息去标识化技术
Eight models of data analysis: detailed PEST model
Giants end up "setting up stalls" and big stalls fall into "bitter battle"
The Sandbox 与 BAYZ 达成合作,共同带动巴西的元宇宙发展
Bilibili × Blue bridge cloud course | online programming practice competition is new!
Isolement des transactions MySQL
Analysis on the advantages and disadvantages of the best 12 project management systems at home and abroad
What is an applet container
Debian change source and uninstall useless services
The technical design and practice of decrypting the red envelopes of Tiktok Spring Festival
HAOGE's blog Road
ASM文件系统 数据如何写和读数据
What to check for AIX system monthly maintenance (II)
Notes to nodejs (III)
The sandbox and bayz have reached cooperation to jointly drive the development of metauniverse in Brazil
Data interpretation! Ideal L9 sprints to "sell more than 10000 yuan a month" to grab share from BBA
Several cases of index invalidation caused by MySQL