zoukankan      html  css  js  c++  java
  • [HNOI2014]米特运输

    显然知道一个节点就可以推出整棵树
    然而直接乘会爆longlong
    所以考虑取log
    最后排序算众数即可

    # include <stdio.h>
    # include <stdlib.h>
    # include <iostream>
    # include <algorithm>
    # include <string.h>
    # include <math.h>
    # define IL inline
    # define RG register
    # define Fill(a, b) memset(a, b, sizeof(a))
    using namespace std;
    typedef long long ll;
    
    IL ll Read(){
        RG char c = getchar(); RG ll x = 0, z = 1;
        for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
        for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + c - '0';
        return x * z;
    }
    
    const int MAXN(500010);
    const double EPS(1e-8);
    int n, cnt, ft[MAXN], a[MAXN], son[MAXN], ans;
    double s[MAXN];
    struct Edge{  int to, nt;  } edge[MAXN << 1];
    /*
      s[i]表示i不变,根的a取对数后的值
      对s排序,取最多的众数个数num
      ans = n - num
    */
    
    IL void Add(RG int u, RG int v){
        edge[cnt] = (Edge){v, ft[u]}; ft[u] = cnt++; son[u]++;
    }
    
    IL void Dfs(RG int u){
        for(RG int e = ft[u]; e != -1; e = edge[e].nt){
            RG int v = edge[e].to;
            s[v] = s[u] + log(son[u]);
            Dfs(v);
        }
        s[u] += log(a[u]);
    }
    
    int main(RG int argc, RG char* argv[]){
        n = Read();
        for(RG int i = 1; i <= n; i++) a[i] = Read(), ft[i] = -1;
        for(RG int i = 1, u, v; i < n; i++) u = Read(), v = Read(), Add(u, v);
        Dfs(1);
        sort(s + 1, s + n + 1);
        for(RG int i = 2, num = 1; i <= n; i++)
            if(s[i] - s[i - 1] < EPS) num++, ans = max(ans, num);
            else num = 1;
        printf("%d
    ", n - ans);
        return 0;
    }
    
  • 相关阅读:
    nginx 正向代理 反向代理 负载均衡
    nginx配置文件常用基本配置指令
    92)http 和https协议入门
    tp6--nginx下pathinfo配置
    tp6省略url里的index.php
    echarts鼠标移上去显示数据
    composer 下载thinkphp6失败
    tp5写入cookie失效
    4.15 Spring Cloud理论基础
    4.14 SpringBoot理论基础
  • 原文地址:https://www.cnblogs.com/cjoieryl/p/8206390.html
Copyright © 2011-2022 走看看