bzoj 3573: [Hnoi2014]米特运输
一个根节点的权值会决定一棵树全部的权值是显然的(一开始也想,枚举一下??呵呵,这么sb的做法怎么可能对,然后就想各种各样的乱搞)
在扒到题解之后,发现还还有取log这个奇巧淫技,,
那么这样对每个点算一下,这个点的权值不变的话,根节点的权值会是多少,那么找一下相同权值最多的就好
(这种乘法会炸的可以尝试去log,,,)
1 #include <bits/stdc++.h> 2 #define LL long long 3 #define lowbit(x) x&(-x) 4 #define inf 0x3f3f3f3f 5 #define eps 1e-5 6 #define N 100005 7 using namespace std; 8 inline int ra() 9 { 10 int x=0,f=1; char ch=getchar(); 11 while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();} 12 while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();} 13 return x*f; 14 } 15 int n,cnt; 16 double a[500005],s[500005],val[500005]; 17 int d[500005],head[500005]; 18 struct edge{ 19 int to,next; 20 }e[1000005]; 21 void insert(int x, int y){ 22 e[++cnt].next=head[x]; e[cnt].to=y; head[x]=cnt; 23 } 24 void dfs(int x, int fa) 25 { 26 for (int i=head[x];i;i=e[i].next) 27 if (e[i].to!=fa) 28 { 29 s[e[i].to]=s[x]+log(d[x]); 30 dfs(e[i].to,x); 31 } 32 } 33 int main(int argc, char const *argv[]) 34 { 35 n=ra(); 36 for (int i=1; i<=n; i++) a[i]=ra(); 37 for (int i=1; i<n; i++) 38 { 39 int x=ra(),y=ra(); 40 insert(x,y); insert(y,x); 41 d[x]++; d[y]++; 42 } 43 for (int i=2; i<=n; i++) d[i]--; 44 s[1]=log(1); 45 dfs(1,0); 46 for (int i=1; i<=n; i++) val[i]=s[i]+log(a[i]); 47 // for (int i=1; i<=n; i++) 48 // printf("%.2lf\n",val[i]); 49 sort(val+1,val+n+1); 50 int ans=0,tmp=1; 51 for (int i=2; i<=n; i++) 52 if (val[i]-val[i-1]<eps) tmp++; 53 else ans=max(ans,tmp),tmp=1; 54 ans=max(ans,tmp); 55 cout<<n-ans; 56 return 0; 57 }