-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathLongest Path With Different Adjacent Characters.cpp
63 lines (44 loc) · 1.63 KB
/
Longest Path With Different Adjacent Characters.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
Solution by Rahul Surana
***********************************************************
You are given a tree (i.e. a connected, undirected graph that has no cycles) rooted at node 0 consisting of n nodes numbered from 0 to n - 1.
The tree is represented by a 0-indexed array parent of size n, where parent[i] is the parent of node i.
Since node 0 is the root, parent[0] == -1.
You are also given a string s of length n, where s[i] is the character assigned to node i.
Return the length of the longest path in the tree such that no pair of adjacent nodes on the path have the same character assigned to them.
***********************************************************
*/
#include <bits/stdc++.h>
class Solution {
public:
int ans = 0;
vector<vector<int>> adj;
int df(int x,string &s){
if(adj[x].size() == 0) return 1;
int a = 0,b = 0;
for(int i = 0 ; i < adj[x].size(); i++){
int l = df(adj[x][i],s);
ans = max(ans,l);
if(s[adj[x][i]] == s[x]) { continue; }
if(l> a){
b = a;
a = l;
}
else b = max(l,b);
}
ans = max(a+b+1,ans);
return a+1;
}
int longestPath(vector<int>& parent, string s) {
adj.resize(s.length(),vector<int>());
int f[parent.size()];
memset(f,0,sizeof(f));
for(int i = 1; i < parent.size(); i++){
adj[parent[i]].push_back(i);
f[parent[i]]++;
}
ans = 1;
df(0,s);
return ans;
}
};