Meaning of the questions: to give you a tree n dots, each dot random [1, n] weights, ask you how many trees there is a palindrome string.
Thinking: random weights are [1, n]. Consider the probability of a bold guess palindrome string length must be very short. The length of the violence at less than equal to the number 3 on the list.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector<int> q[100005];
int a[100005];
int vis[100005];ll ans=0;
void dfs(int u,int fa)
{
vis[a[u]]++;
for(int i=0;i<q[u].size();i++)
{
int v=q[u][i];
if(v==fa) continue;
ans+=vis[a[v]];
vis[a[v]]++;
if(fa!=-1&&a[v]==a[fa]) ans++;
}
for(int i=0;i<q[u].size();i++)
{
int v=q[u][i];
if(v==fa) continue;
vis[a[v]]--;
}
vis[a[u]]--;
for(int i=0;i<q[u].size();i++)
{
int v=q[u][i];
if(v==fa) continue;
dfs(v,u);
}
}
int main()
{
int t;scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++) q[i].clear();
for(int i=1;i<=n;i++) scanf("%d",&a[i]);
memset(vis,0,sizeof(vis));
ans=0;
for(int i=0;i<n-1;i++)
{
int x,y;
scanf("%d%d",&x,&y);q[x].push_back(y);q[y].push_back(x);
}
dfs(1,-1);
printf("%d\n",ans+n);
}
return 0;
}