CSES - Tree Distances I

Authors: Nathan Wang, Benjamin Qi

Solution 1

Described in CPH 14.3.

This section is not complete.

Feel free to file a request to complete this using the "Contact Us" button.

Solution 2

Compute a diameter of the tree as described by algorithm 2 here. Once we have a diameter , output for each node .

1#include <bits/stdc++.h>
2
3using namespace std;
4
5// dist[0][i] = distance from node a to node i
6// dist[1][i] = distance from node b to node i
7int dist[2][200000];
8vector<int> adj[200000];
9
10int dfs(int u, int p, int d, int i) {

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!

Give Us Feedback on CSES - Tree Distances I!