-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhi..cpp
More file actions
43 lines (43 loc) · 1.38 KB
/
hi..cpp
File metadata and controls
43 lines (43 loc) · 1.38 KB
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
class Solution {
public:
vector<vector<int>> adj;
int maxLen(int n, vector<vector<int>>& edges, string label) {
adj.resize(n);
for(auto& e : edges) {
adj[e[0]].push_back(e[1]);
adj[e[1]].push_back(e[0]);
}
int ans = 1;
for(int i=0; i<n; i++) {
// odd length palindrome
for(int j : adj[i]) {
for(int k : adj[i]) {
vector<bool> visited(n, false);
visited[i] = true;
if(j != k and label[j] == label[k])
ans = max(ans, 1 + fun(j, k, label, visited));
}
}
// even length palindrome
for(int j : adj[i]) {
vector<bool> visited(n, false);
visited.assign(n, false);
if(label[i] == label[j])
ans = max(ans, fun(i, j, label, visited));
}
}
return ans;
}
int fun(int i, int j, const string& label, vector<bool>& visited) {
visited[i] = visited[j] = true;
int ret = 2;
for(int e : adj[i]) {
for(int f : adj[j]) {
if(e != f and label[e] == label[f] and !visited[e] and !visited[f]) {
ret = max(ret, 2 + fun(e, f, label, visited));
}
}
}
return ret;
}
};