Digression:
sol: Routine
I:? ? ? What is immortal ideas? ,
Has given a \ (n \) tree nodes, you can choose some point, these points can not be adjacent
The total value of what you get is the product of the weights of these nodes
Seeking the most value \ (mod 998244353 \)
sol
Consider a magical thing: log
Obviously we have:
If
\ [ab = c \]
so
\ [log_x (a) + log_x
(b) = log (c) \] Therefore, the maximum weight problem into independent set of the tree
Classic question, can a tree DP
code
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N=200010;
const int M=N;
const int mod=998244353;
inline void read(int &x) {
x=0;
int f=1;
char ch=getchar();
while(ch<'0'||ch>'9') {
if (ch=='-') {
f=-1;
}
ch=getchar();
}
while(ch>='0'&&ch<='9') {
x=x*10+ch-'0';
ch=getchar();
}
x*=f;
}
struct note {
int t;
int next;
};
int cnt;
int head[N];
note e[M<<1];
int n;
inline void add(int x,int y) {
e[++cnt].t=y;
e[cnt].next=head[x];
head[x]=cnt;
}
double f[N][2];
double val[N];
int v[N];
void dfs(int p,int fa) {
f[p][0]=0;
f[p][1]=val[p];
for(int i=head[p];i+1;i=e[i].next) {
int t=e[i].t;
if (t==fa) {
continue;
}
dfs(t,p);
f[p][1]+=f[t][0];
f[p][0]+=max(f[t][1],f[t][0]);
}
}
int ans=1;
void dfs2(int p,int fa,bool d) {
if (d) {
ans=(1ll*v[p]*ans)%mod;
}
for(int i=head[p];i+1;i=e[i].next) {
int t=e[i].t;
if (t==fa) {
continue;
}
if (d) {
dfs2(t,p,0);
} else {
if (f[t][1]>f[t][0]) {
dfs2(t,p,1);
} else {
dfs2(t,p,0);
}
}
}
}
int main() {
read(n);
memset(head,-1,sizeof(head));
for(int i=1;i<=n;i++) {
read(v[i]);
val[i]=log10(v[i]);
}
for(int i=1;i<n;i++) {
int x,y;
read(x),read(y);
add(x,y);
add(y,x);
}
dfs(1,-1);
if (f[1][0]<f[1][1]) {
dfs2(1,-1,1);
} else {
dfs2(1,-1,0);
}
printf("%d\n",ans);
return 0;
}