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

    题面

    洛谷

    题解

    img

    代码

    #include <iostream> 
    #include <cstdio> 
    #include <cstdlib> 
    #include <cstring> 
    #include <cmath> 
    #include <algorithm> 
    using namespace std;
    inline int gi() {
        register int data = 0, w = 1;
        register char ch = 0;
        while (!isdigit(ch) && ch != '-') ch = getchar(); 
        if (ch == '-') w = -1, ch = getchar();
        while (isdigit(ch)) data = 10 * data + ch - '0', ch = getchar();
        return w * data; 
    } 
    const double eps = 1e-8;
    const int MAX_N = 5e5 + 5; 
    struct Graph { int to, next; } e[MAX_N << 1];
    int fir[MAX_N], e_cnt;
    void clearGraph() { memset(fir, -1, sizeof(fir)); } 
    void Add_Edge(int u, int v) { e[e_cnt] = (Graph){v, fir[u]}, fir[u] = e_cnt++; } 
    int N, a[MAX_N], deg[MAX_N]; 
    double f[MAX_N];
    
    void dfs(int x, double s) {
        f[x] = s + log(a[x]);
        for (int i = fir[x]; ~i; i = e[i].next) dfs(e[i].to, s + log(deg[x]));
    }
    
    int main() {
    #ifndef ONLINE_JUDGE
    	freopen("cpp.in", "r", stdin); 
    #endif
    	clearGraph(); 
        N = gi(); 
        for (int i = 1; i <= N; i++) a[i] = gi(); 
        for (int i = 1, u, v; i < N; i++) u = gi(), v = gi(), ++deg[u], Add_Edge(u, v); 
        dfs(1, 0);
        sort(&f[1], &f[N + 1]); 
        int ans = 1; 
        for (int i = 2, cnt = 1; i <= N; i++) { 
            if (f[i] - f[i - 1] <= eps) ans = max(ans, ++cnt); 
            else cnt = 1; 
        } 
        printf("%d
    ", N - ans); 
        return 0;
    } 
    
  • 相关阅读:
    冒泡算法
    Eclipse 安装插件(aptana、svn 、git、Java EE、JSHint)
    smtp和pop3
    EJB 3.0 + JBOSS 5.1
    有状态EJBBean和无状态的EJBBean
    EJB3.0在发布时出现异常javax.naming.NameNotFoundException: myqueue not bound
    Django之路由系统
    Mysql安装与连接
    Django之Form组件
    jinja2模板
  • 原文地址:https://www.cnblogs.com/heyujun/p/10406332.html
Copyright © 2011-2022 走看看