#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<queue>
#include<list>
#include<algorithm>
#include<numeric>
#include<functional>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pair<int, int>> vii;
#define pb push_back
#define mp make_pair
#define to(a,n) for (int i = a; i < n; i++)
#define too(a, n) for (int i = a; i <= n; i++)
#define fro(a,n) for (int i = n; i > a; i--)
#define froo(a,n) for (int i = n; i >= a; i--)
void solve() {
int n;
cin >> n;
string s;
cin >> s;
vector<vector<int>> adj(n);
vector<vector<pair<int, int>>> len(n);
vector<vector<int>> go(n,vector<int>(n));
vector<vector<int>> dp(n, vector<int>(n));
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
adj[--a].push_back(--b);
adj[b].push_back(a);
}
std::function<void(int, int, int, int)> dfs = [&](int cur, int src, int p, int dist) {
go[cur][src] = p;
len[dist].push_back({ src,cur });
for (int edge_node : adj[cur]) {
if (edge_node == p) continue;
dfs(edge_node, src, cur, dist + 1);
}
return;
};
for (int i = 0; i < n; i++) {
dfs(i, i, i, 0);
}
for (int i = 0; i < n; i++) {
for (auto p : len[i]) {
int u = p.first;
int v = p.second;
if (i == 0) dp[u][v] = 1;
else if (i==1) dp[u][v] = 1 + (s[u] == s[v]);
else {
int x = dp[u][go[v][u]];
int y = dp[go[u][v]][v];
int z = dp[go[u][v]][go[v][u]] + 2 * (s[u] == s[v]);
dp[u][v] = max({ x,y,z });
}
}
}
int ans = INT_MIN;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
ans = max(ans, dp[i][j]);
}
}
cout << ans << "\n";
return;
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
Duration | Birthday Party |
e-maze-in | Bricks Game |
Char Sum | Two Strings |
Anagrams | Prime Number |
Lexical Sorting Reloaded | 1514A - Perfectly Imperfect Array |
580A- Kefa and First Steps | 1472B- Fair Division |
996A - Hit the Lottery | MSNSADM1 Football |
MATCHES Playing with Matches | HRDSEQ Hard Sequence |
DRCHEF Doctor Chef | 559. Maximum Depth of N-ary Tree |
821. Shortest Distance to a Character | 1441. Build an Array With Stack Operations |
1356. Sort Integers by The Number of 1 Bits | 922. Sort Array By Parity II |
344. Reverse String | 1047. Remove All Adjacent Duplicates In String |
977. Squares of a Sorted Array | 852. Peak Index in a Mountain Array |
461. Hamming Distance | 1748. Sum of Unique Elements |
897. Increasing Order Search Tree | 905. Sort Array By Parity |