longest path of the tree

longest path of the tree

Given a tree, the tree contains n nodes (numbered 1~n) and n−1 undirected edges, and each edge has a weight. Now please find the longest path in the tree. In other words, find a path such that the points at both ends of the path are farthest apart.

Note: A path can contain only one point.

input format
The first line contains the integer n
Next n−1 lines, each line contains three integers ai, bi, ci, indicating that there is an edge with weight ci between points ai and bi.

output format
Output an integer representing the length of the longest path in the tree.

data range
1≤n≤10000
1≤ai,bi≤n
−105≤ci≤105
Input sample:
6
5 1 6
1 4 5
6 3 9
2 6 8
6 1 7
Sample output:
twenty two

Solution

non-negative edge

The code above is a C++ program that finds the longest path in a tree using DFS (depth-first search). The program reads in the number of nodes in the tree from the standard input, followed by the edges and weights of the tree.

The program starts by defining a constant integer N with a value of 100010, which represents the maximum number of nodes in the tree. It then defines an integer n to store the number of nodes in the tree, and an integer fi to store the index of the farthest node from the root.

Next, the program defines a struct Edge that represents an edge in the tree. The Edge struct has two integer members: to, which represents the index of the node that the edge points to, and w, which represents the weight of the edge.

The program then defines an integer array d of size N to store the distance from the root to each node in the tree, and a vector of Edge objects G of size N to represent the adjacency list of the tree.

The program then defines a function named dfs that implements the DFS algorithm to find the longest path in the tree. The function takes two arguments: an integer u representing the current node, and an integer fa representing the parent of the current node. The function initializes the distance of the current node to the root to be the distance of its parent plus the weight of the edge connecting them, and updates the farthest node if the distance of the current node is greater than the distance of the farthest node. The function then recursively calls itself on each child of the current node, passing the current node as the parent.

In the main function, the program reads in the number of nodes in the tree from the standard input, and then reads in the edges and weights of the tree. For each edge, the program adds the edge to the adjacency list of the tree.

The program then calls the dfs function on the root node with a parent of 0, which initializes the distance of each node to the root and updates the farthest node. The program then calls the dfs function again on the farthest node with a parent of 0, which finds the longest path in the tree.

Finally, the program prints the length of the longest path to the standard output.

Overall, the program finds the longest path in a tree using DFS by computing the distance of each node to the root and finding the farthest node, and then computing the distance of each node to the farthest node and finding the longest path.

#include <bits/stdc++.h>

using namespace std;

const int N = 100010;

int n, fi;

struct Edge
{
    int to, w;
};
int d[N];
vector<Edge> G[N];

void dfs(int u, int fa) {
    for (auto x : G[u]) {
        if (x.to == fa) continue;
        else {
            d[x.to] = d[u] + x.w;
            if (d[x.to] > d[fi]) fi = x.to;
            dfs(x.to, u);
        }
    }
}

int main()
{
    cin.tie(nullptr);
    cout.tie(nullptr);
    ios::sync_with_stdio(false);
    cin >> n;
    for (int i = 0; i < n - 1; i++) {
        int a, b, w;
        cin >> a >> b >> w;
        G[a].push_back({b, w});
        G[b].push_back({a, w});
    }
    dfs(1, 0);
    memset(d, 0, sizeof d);
    dfs(fi, 0);
    cout << d[fi] << endl;
    return 0;
}

The code above is a C++ program that finds the longest path in a tree using DFS (depth-first search). The program reads in the number of nodes in the tree from the standard input, followed by the edges and weights of the tree.

The program starts by defining a constant integer N with a value of 100010, which represents the maximum number of nodes in the tree. It then defines an integer n to store the number of nodes in the tree, and an integer ans to store the length of the longest path in the tree.

Next, the program defines a struct Edge that represents an edge in the tree. The Edge struct has two integer members: to, which represents the index of the node that the edge points to, and w, which represents the weight of the edge.

The program then defines a vector of Edge objects G of size N to represent the adjacency list of the tree.

The program then defines a function named dfs that implements the DFS algorithm to find the longest path in the tree. The function takes two arguments: an integer u representing the current node, and an integer fa representing the parent of the current node. The function initializes two integer variables dist and dmax1 to 0, which represent the distance of the current node to the farthest node in its subtree and the distance of the current node to the farthest node in its subtree that is not its direct child, respectively. The function then iterates over the children of the current node, and recursively calls itself on each child, passing the current node as the parent. For each child, the function updates dmax1 if the distance of the child to the farthest node in its subtree is greater than the distance of the current node to the farthest node in its subtree that is not its direct child. The function then updates dist to be the maximum of the distance of the current node to the farthest node in its subtree and the sum of the distance of the child to the farthest node in its subtree and the weight of the edge connecting the child to the current node. Finally, the function updates ans to be the maximum of ans and the sum of dmax1 and the distance of the current node to the farthest node in its subtree.

In the main function, the program reads in the number of nodes in the tree from the standard input, and then reads in the edges and weights of the tree. For each edge, the program adds the edge to the adjacency list of the tree.

The program then calls the dfs function on the root node with a parent of 0, which finds the longest path in the tree.

Finally, the program prints the length of the longest path to the standard output.

Overall, the program finds the longest path in a tree using DFS by computing the distance of each node to the farthest node in its subtree and the distance of each node to the farthest node in its subtree that is not its direct child, and finding the maximum sum of these distances.

#include <bits/stdc++.h>

using namespace std;

const int N = 100010;

int n;
int ans;

struct Edge
{
    int to, w;
};
int d[N];
vector<Edge> G[N];

int dfs(int u, int fa) {
    int dmax1 = 0, dmax2 = 0, dist = 0;
    for (auto x : G[u]) {
        if (x.to == fa) continue;
        else {
            int s = dfs(x.to, u) + x.w;
            dist = max(dist, s);
            if (s > dmax1) dmax2 = dmax1, dmax1 = s;
            else if (s > dmax2) dmax2 = s;
        }
    }
    ans = max(ans, dmax1 + dmax2);
    return dist;
}
 
int main()
{
    cin.tie(nullptr);
    cout.tie(nullptr);
    ios::sync_with_stdio(false);
    cin >> n;
    for (int i = 0; i < n - 1; i++) {
        int a, b, w;
        cin >> a >> b >> w;
        G[a].push_back({b, w});
        G[b].push_back({a, w});
    }
    dfs(1, 0);
    cout << ans << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值