题解 卷
作者:互联网
大水题,因为状态不怎么好炸成60pts
发现它要求什么「乘积最大」,需要我们比较几个很大的乘积
那很套路的,long double+log莽上
然而我不知道是困傻了还是怎么取了log之后接着用乘法
显然 \(log_2^a + log_2^b = log_2^{ab}\) 对数的加法对应原数的乘法
于是我挂了
Code:
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 200010
#define ll long long
#define ld long double
//#define int long long
char buf[1<<21], *p1=buf, *p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf, 1, 1<<21, stdin)), p1==p2?EOF:*p1++)
inline int read() {
int ans=0, f=1; char c=getchar();
while (!isdigit(c)) {if (c=='-') f=-f; c=getchar();}
while (isdigit(c)) {ans=(ans<<3)+(ans<<1)+(c^48); c=getchar();}
return ans*f;
}
int n;
int head[N], size;
ll w[N];
const ll p=1e9+7;
struct edge{int to, next;}e[N<<1];
inline void add(int s, int t) {e[++size].to=t; e[size].next=head[s]; head[s]=size;}
namespace force{
ld dp1[N][2];
ll dp2[N][2];
void dfs(int u, int fa) {
dp1[u][0]=0; dp1[u][1]=log(w[u]);
dp2[u][0]=1; dp2[u][1]=w[u];
for (int i=head[u],v; ~i; i=e[i].next) {
v = e[i].to;
if (v==fa) continue;
dfs(v, u);
dp1[u][1]+=dp1[v][0]; dp2[u][1]=dp2[u][1]*dp2[v][0]%p;
if (dp1[v][1]>dp1[v][0]) dp1[u][0]+=dp1[v][1], dp2[u][0]=dp2[u][0]*dp2[v][1]%p;
else dp1[u][0]+=dp1[v][0], dp2[u][0]=dp2[u][0]*dp2[v][0]%p;
}
}
void solve() {
dfs(1, 0);
printf("%lld\n", dp1[1][0]>dp1[1][1]?dp2[1][0]:dp2[1][1]);
exit(0);
}
}
signed main()
{
memset(head, -1, sizeof(head));
n=read();
for (int i=1; i<=n; ++i) w[i]=read();
for (int i=1,u,v; i<n; ++i) {
u=read(); v=read();
add(u, v); add(v, u);
}
force::solve();
return 0;
}
标签:head,dp2,dp1,题解,long,log,define 来源: https://www.cnblogs.com/narration/p/15154038.html