zoukankan      html  css  js  c++  java
  • LG3237 「HNOI2014」米特运输 树形DP

    问题描述

    LG3237


    题解

    问题转化为:

    要求将这棵树,满足

    • 结点 (x) 所有孩子权值相等

    • 结点 (x) 权值等于所有孩子权值和

    将乘法转化为 (log) 加法


    (mathrm{Code})

    #include<bits/stdc++.h>
    using namespace std;
    
    template <typename Tp>
    void read(Tp &x){
    	x=0;char ch=1;int fh;
    	while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();
    	if(ch=='-'){fh=-1;ch=getchar();	}
    	else fh=1;
    	while(ch>='0'&&ch<='9')	x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
    	x*=fh;
    }
    
    const int maxn=500007;
    const double eps=0.00001;
    
    int n;
    double a[maxn],s[maxn],val[maxn];
    int Head[maxn],to[maxn*2],Next[maxn*2],tot;
    int q[maxn],ans;
    
    void add(int x,int y) {
    	to[++tot]=y,Next[tot]=Head[x],Head[x]=tot;
    }
    
    void preprocess(int x,int f){
    	for(int i=Head[x];i;i=Next[i]){
    		int y=to[i];
    		if(y==f) continue;
    		s[y]=s[x]+log(q[x]);
    		preprocess(y,x);
    	}
    }
    
    int main(){
    	read(n);
    	for(int i=1;i<=n;i++) scanf("%lf",&a[i]);
    	for(int i=1,x,y;i<n;i++){
    		read(x);read(y);
    		add(x,y);add(y,x);
    		++q[x],++q[y];
    	}
    	for(int i=2;i<=n;i++) --q[i];
    	s[1]=log(1);preprocess(1,0);
    	for(int i=1;i<=n;i++) val[i]=s[i]+log(a[i]);
    	sort(val+1,val+n+1);
    	for(int i=2,cnt=0;i<=n;i++){
    		if(val[i]-val[i-1]<=eps) ++cnt;
    		else ans=max(ans,cnt),cnt=1;
    		ans=max(ans,cnt);
    	}
    	printf("%d
    ",n-ans);
    	return 0;
    }
    
  • 相关阅读:
    软件工程概论-用户登录界面
    2016.11.25异常处理
    2016.11.18多态
    2016.11.11继承与接口
    11.6数组
    10.28字符串加密等
    python 读写文件
    python可变的类型、不可变的类型
    python 字典练习 记录学生是否交作业的小程序
    python字典
  • 原文地址:https://www.cnblogs.com/liubainian/p/11831856.html
Copyright © 2011-2022 走看看