5
int dfs(int node, int par, string &s) {6
// vector to store max length & second max length that we get from children7
// of node at v[1] & v[0] respectively9
for (auto child : g[node]) {11
int x = dfs(child, node, s);12
// if char at node != char at child then node can be included in our ans13
if (s[node] != s[child]) {24
// update our ans every time we return to node25
ans = max(ans, v[0] + v[1] + 1);26
// return the max length up till here including node27
return max(v[0], v[1]) + 1;29
int longestPath(vector<int> &parent, string s) {34
for (int i = 1; i < parent.size(); i++) {35
g[parent[i]].push_back(i);36
g[i].push_back(parent[i]);38
// run dfs and calculate max39
ans = max(ans, dfs(0, -1, s));