mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
Modified comments
This commit is contained in:
parent
ff8a511413
commit
5a2238d8ef
@ -1,6 +1,6 @@
|
|||||||
/// C++ Program to find height of the tree using bottom - up DP.
|
// C++ Program to find height of the tree using bottom - up DP.
|
||||||
|
|
||||||
/**
|
/*
|
||||||
* Given a rooted tree with node 1.
|
* Given a rooted tree with node 1.
|
||||||
* Task is to find the height of the tree.
|
* Task is to find the height of the tree.
|
||||||
* Example: -
|
* Example: -
|
||||||
@ -9,15 +9,15 @@
|
|||||||
* 1 3
|
* 1 3
|
||||||
* 2 4
|
* 2 4
|
||||||
* Height of the tree : - 3
|
* Height of the tree : - 3
|
||||||
**/
|
*/
|
||||||
|
|
||||||
#include<iostream>
|
#include<iostream>
|
||||||
#include<vector>
|
#include<vector>
|
||||||
|
|
||||||
/// global declarations
|
// global declarations
|
||||||
/// no of nodes max limit.
|
// no of nodes max limit.
|
||||||
const int MAX = 1e5;
|
const int MAX = 1e5;
|
||||||
/// adjacency list
|
// adjacency list
|
||||||
std::vector<int> adj[MAX];
|
std::vector<int> adj[MAX];
|
||||||
std::vector<bool> visited;
|
std::vector<bool> visited;
|
||||||
std::vector<int> dp;
|
std::vector<int> dp;
|
||||||
@ -29,31 +29,31 @@ void dp_with_dfs(int u) {
|
|||||||
if (!visited[v]) {
|
if (!visited[v]) {
|
||||||
dp_with_dfs(v);
|
dp_with_dfs(v);
|
||||||
|
|
||||||
/// selected maximum subtree height from all childs.
|
// selected maximum subtree height from all child.
|
||||||
child_height = std::max(child_height, dp[v]+1);
|
child_height = std::max(child_height, dp[v]+1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/// assigned the max child height to current visited node.
|
// assigned the max child height to current visited node.
|
||||||
dp[u] = child_height;
|
dp[u] = child_height;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
/// number of nodes
|
// number of nodes
|
||||||
int n;
|
int n;
|
||||||
std::cin >> n;
|
std::cin >> n;
|
||||||
int u, v;
|
int u, v;
|
||||||
/// Tree contains exactly n-1 edges where n denotes the nodes.
|
// Tree contains exactly n-1 edges where n denotes the nodes.
|
||||||
for (int i = 0; i < n-1; i++) {
|
for (int i = 0; i < n-1; i++) {
|
||||||
std::cin >> u >> v;
|
std::cin >> u >> v;
|
||||||
/// undirected tree u -> v and v -> u.
|
// undirected tree u -> v and v -> u.
|
||||||
adj[u].push_back(v);
|
adj[u].push_back(v);
|
||||||
adj[v].push_back(u);
|
adj[v].push_back(u);
|
||||||
}
|
}
|
||||||
/// initialize all nodes as unvisited.
|
// initialize all nodes as unvisited.
|
||||||
visited.assign(n+1, false);
|
visited.assign(n+1, false);
|
||||||
/// initialize depth of all nodes to 0.
|
// initialize depth of all nodes to 0.
|
||||||
dp.assign(n+1, 0);
|
dp.assign(n+1, 0);
|
||||||
/// function call which will initialize the height of all nodes.
|
// function call which will initialize the height of all nodes.
|
||||||
dp_with_dfs(1);
|
dp_with_dfs(1);
|
||||||
std::cout << "Height of the Tree : " << dp[1] << std::endl;
|
std::cout << "Height of the Tree : " << dp[1] << std::endl;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user