BZOJ 3573 米特运输

http://www.lydsy.com/JudgeOnline/problem.php?id=3573

思路:如果固定了一个位置的米特容量,那其他位置的米特容量也就是确定的,我们分别假设当前n个装置都是固定的,然后这个装置的所有祖先的度数之积乘上当前装置的容量,就等于确定当前米特容量,得到根节点的米特容量,这样我们只要分别固定n个装置,算出根节点的容量,比较有几个根节点容量是相等的,用n减去即是答案。

 1 #include<algorithm>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<iostream>
 6 const double eps=1e-5;
 7 double s[500005];
 8 int tot,go[1000005],next[1000005],first[1000005],du[500005];
 9 int a[500005],n;
10 int read(){
11     char ch=getchar();int t=0,f=1;
12     while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
13     while ('0'<=ch&&ch<='9'){t=t*10+ch-'0';ch=getchar();}
14     return t*f;
15 }
16 void insert(int x,int y){
17     tot++;
18     go[tot]=y;
19     next[tot]=first[x];
20     first[x]=tot;
21 }
22 void add(int x,int y){
23     insert(x,y);insert(y,x);
24 }
25 void dfs(int x,int fa){
26     for (int i=first[x];i;i=next[i]){
27         int pur=go[i];
28         if (pur==fa) continue;
29         s[pur]=s[x]+log(du[x]);
30         dfs(pur,x);
31     }
32 }
33 int main(){
34     n=read();
35     for (int i=1;i<=n;i++)
36      a[i]=read();
37     for (int i=1;i<n;i++){
38         int x=read(),y=read();
39         add(x,y);du[x]++;du[y]++;
40     } 
41     for (int i=2;i<=n;i++)
42      du[i]--;
43     s[1]=log(1);
44     dfs(1,0);
45     for (int i=1;i<=n;i++)
46      s[i]+=log(a[i]);
47     std::sort(s+1,s+n+1);
48     int tmp=1,ans=0;
49     for (int i=2;i<=n;i++)
50      if (fabs(s[i]-s[i-1])<=eps) tmp++;
51      else ans=std::max(ans,tmp),tmp=1;
52     ans=std::max(ans,tmp);
53     printf("%d
",n-ans); 
54 }
原文地址:https://www.cnblogs.com/qzqzgfy/p/5589705.html