zoukankan      html  css  js  c++  java
  • Codeforces 1088E Ehab and a component choosing problem

    Ehab and a component choosing problem

    如果有多个连接件那么这几个连接件一定是一样大的, 所以我们先找到值最大的连通块这个肯定是分数的答案。

    dp[ i ]表示对于 i 这棵子树包含 i 这个点的连通块的最大值, 就能求出答案, 然后知道最大值之后再就能求出几个连接件。

    #include<bits/stdc++.h>
    #define LL long long
    #define fi first
    #define se second
    #define mk make_pair
    #define PLL pair<LL, LL>
    #define PLI pair<LL, int>
    #define PII pair<int, int>
    #define SZ(x) ((int)x.size())
    #define ull unsigned long long
    
    using namespace std;
    
    const int N = 3e5 + 7;
    const int inf = 0x3f3f3f3f;
    const LL INF = 0x3f3f3f3f3f3f3f3f;
    const int mod = 1e9 + 7;
    const double eps = 1e-8;
    const double PI = acos(-1);
    
    int n, ans, a[N];
    LL dp[N], mx = -INF;
    vector<int> G[N];
    
    void getMax(int u, int fa) {
        dp[u] = a[u];
        for(auto& v : G[u]) {
            if(v == fa) continue;
            getMax(v, u);
            if(dp[v] > 0) dp[u] += dp[v];
        }
    }
    
    void getAns(int u, int fa) {
        dp[u] = a[u];
        for(auto& v : G[u]) {
            if(v == fa) continue;
            getAns(v, u);
            if(dp[v] > 0) dp[u] += dp[v];
        }
        if(dp[u] == mx) ans++, dp[u] = 0;
    }
    
    int main() {
        scanf("%d", &n);
        for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
        for(int i = 2; i <= n; i++) {
            int u, v; scanf("%d%d", &u, &v);
            G[u].push_back(v);
            G[v].push_back(u);
        }
        getMax(1, 0);
        for(int i = 1; i <= n; i++) mx = max(mx, dp[i]);
        getAns(1, 0);
        printf("%lld %d
    ", mx * ans, ans);
        return 0;
    }
    
    /*
    */
  • 相关阅读:
    leetcode 刷题日志 2018-03-26
    WebForms UnobtrusiveValidationMode 需要“jquery”ScriptResourceMapping
    sublime wrong
    SSM框架使用-wrong
    C++设计实现算法时易犯错误
    CodeBlocks wrong
    leetcode 刷题日志 2018-3-28
    CountDownLatch
    类加载器和双亲委派
    GC的一个面试题
  • 原文地址:https://www.cnblogs.com/CJLHY/p/10421955.html
Copyright © 2011-2022 走看看