Fair and Square
Mier_Samuelle · · 题解
画图易知,对于任意三元组
考虑在交点处计算贡献。对于每个
其中
:::success[Code]{open}
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MAXN = 2e5 + 10;
vector <int> adj[MAXN];
int a[MAXN], siz[MAXN], n;
int ans;
void dfs(int u, int fa){
siz[u] = 1;
for (int v : adj[u]){
if (v == fa){
continue;
}
dfs(v, u);
siz[u] += siz[v];
}
int t = sqrt(a[u] * a[u] * a[u]);
if (t * t != a[u] * a[u] * a[u]){
return;
}
if ((int)adj[u].size() <= 1){
return;
}
int tot1 = 0, tot2 = 0;
for (int v : adj[u]){
int csiz = (v == fa ? n - siz[u] : siz[v]);
tot1 += csiz * (n - 1);
tot2 += csiz * (n - 1) * (n - 1);
tot1 -= csiz * csiz;
tot2 -= csiz * csiz * (n - 1 - csiz) * 3;
tot2 -= csiz * csiz * csiz;
}
tot1 /= 2;
tot2 /= 6;
ans += tot1;
if (adj[u].size() >= 3){
ans += tot2;
}
return;
}
void solve(){
cin >> n;
for (int i = 1; i <= n; i++){
adj[i].clear();
cin >> a[i];
}
for (int i = 1; i < n; i++){
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
ans = 0;
dfs(1, 0);
cout << ans << "\n";
return;
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while (t--){
solve();
}
return 0;
}
:::