显然确定一个点的权值后整棵树权值确定。只要算出根节点的权值就能知道两种改法是否等价。
乘的话显然会炸,取log即可。map似乎会出一些问题,sort即可。
#include#include #include #include #include #include using namespace std;int read(){ int x=0,f=1;char c=getchar(); while (c<'0'||c>'9') { if (c=='-') f=-1;c=getchar();} while (c>='0'&&c<='9') x=(x<<1)+(x<<3)+(c^48),c=getchar(); return x*f;}#define N 500010#define double long doubleconst double eps=1E-10;int n,a[N],p[N],degree[N],t=0;double f[N];struct data{ int to,nxt;}edge[N<<1];void addedge(int x,int y){t++;edge[t].to=y,edge[t].nxt=p[x],p[x]=t;}void dfs(int k,int from){ for (int i=p[k];i;i=edge[i].nxt) if (edge[i].to!=from) degree[k]++; for (int i=p[k];i;i=edge[i].nxt) if (edge[i].to!=from) { f[edge[i].to]=f[k]+log(degree[k]); dfs(edge[i].to,k); }}int main(){#ifndef ONLINE_JUDGE freopen("bzoj3573.in","r",stdin); freopen("bzoj3573.out","w",stdout); const char LL[]="%I64d\n";#else const char LL[]="%lld\n";#endif n=read(); for (int i=1;i<=n;i++) a[i]=read(); for (int i=1;i