T1
树型dp 推导如下(回去再挂照片)
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <queue>
using namespace std;
const int N = 5e4 + 5;
const int inf = 1e9;
struct Edge{
int v, next;
}edge[N << 1];
int esize, head[N];
inline void addedge(int x, int y){
edge[++esize] = (Edge){y, head[x]};
head[x] = esize;
}
int w[N];
int f[N], g[N];//f--all 1 g--both 1 and 2
int n;
int anss, ansm;
inline void update(int ss, int mm){
//printf("update %d %d\n", ss, mm);
if(ss * ansm < anss * mm){
anss = ss; ansm = mm;
}
}
void dfs(int x, int ff){
if(w[x] == 1) f[x] = g[x] = 1;
if(w[x] == 2) g[x] = 1;
for(int i = head[x], vv; ~i; i = edge[i].next){
vv = edge[i].v; if(vv == ff) continue;
dfs(vv, x);
update(2, g[x] + f[vv]);
update(2, g[vv] + f[x]);
update(1, f[x] + f[vv]);
if(w[x] == 1){
f[x] = max(f[x], f[vv] + 1);
g[x] = max(g[x], g[vv] + 1);
}
if(w[x] == 2){
g[x] = max(g[x], f[vv] + 1);
}
}
}
int gcd(int x, int y){
return y ? gcd(y, x % y) : x;
}
inline void print(){
int d = gcd(anss, ansm);
anss /= d, ansm /= d;
printf("%d/%d\n", anss, ansm);
}
inline void init(){
memset(head, -1, sizeof(head));
anss = inf, ansm = 1;
esize = 0;
memset(f, 0, sizeof(f));
memset(g, 0, sizeof(g));
}
int main(){
int T;
scanf("%d", &T);
while(T--){
init();
scanf("%d", &n);
for(int i = 1; i <= n; ++i){
scanf("%d", &w[i]);
anss = min(anss, w[i]);
}
for(int i = 1, x, y, z; i < n; ++i){
scanf("%d%d", &x, &y);
addedge(x, y); addedge(y, x);
}
dfs(1, 0);
print();
}
return 0;
}