zoukankan      html  css  js  c++  java
  • POJ 3585 Accumulation Degree (算竞赛进阶习题)

    树形dp二次扫描换根

    先dp一次求出每个点为根的流量最大值。

    然后用f[i]表示以i为源点整体流量的最大值,可以发现f[root] = dp[root]

    然后考虑访问子节点,假设子节点为u,那么dp[u]肯定是f[u]的一部分,还有一部分就是流向父亲节点所在子树的流量。

    在访问该节点前我们如果已知f[s],那么从u流向父亲节点的流量显然就是min(f[s] - min(w(s, u), dp[u]), w(s, u))

    因此我们也可以一次dfs递推得到答案,如果父亲节点度数为1,需要特判,这时候流量直接就是w(s, u),因为没有别的边有流量流出了

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #define INF 0x3f3f3f3f
    #define full(a, b) memset(a, b, sizeof a)
    #define FAST_IO ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
    using namespace std;
    typedef long long ll;
    inline int lowbit(int x){ return x & (-x); }
    inline int read(){
        int ret = 0, w = 0; char ch = 0;
        while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
        while(isdigit(ch)) ret = (ret << 3) + (ret << 1) + (ch ^ 48), ch = getchar();
        return w ? -ret : ret;
    }
    inline int gcd(int a, int b){ return b ? gcd(b, a % b) : a; }
    inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
    template <typename T>
    inline T max(T x, T y, T z){ return max(max(x, y), z); }
    template <typename T>
    inline T min(T x, T y, T z){ return min(min(x, y), z); }
    template <typename A, typename B, typename C>
    inline A fpow(A x, B p, C lyd){
        A ans = 1;
        for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
        return ans;
    }
    const int N = 200005;
    int _, n, cnt, head[N], dp[N], d[N], f[N];
    struct Edge { int v, next, cpct; } edge[N<<1];
    
    void addEdge(int a, int b, int c){
        edge[cnt].v = b, edge[cnt].cpct = c, edge[cnt].next = head[a], head[a] = cnt ++;
    }
    
    void init(){
        cnt = 0;
        full(head, -1), full(d, 0);
    }
    
    void dfs(int s, int fa){
        dp[s] = 0;
        for(int i = head[s]; i != -1; i = edge[i].next){
            int u = edge[i].v;
            if(u == fa) continue;
            dfs(u, s);
            if(d[u] == 1) dp[s] += edge[i].cpct;
            else dp[s] += min(dp[u], edge[i].cpct);
        }
    }
    
    void calc(int s, int fa){
        for(int i = head[s]; i != -1; i = edge[i].next){
            int u = edge[i].v;
            if(u == fa) continue;
            if(d[s] == 1) f[u] = dp[u] + edge[i].cpct;
            else f[u] = dp[u] + min(f[s] - min(dp[u], edge[i].cpct), edge[i].cpct);
            calc(u, s);
        }
    }
    
    int main(){
    
        for(_ = read(); _; _ --){
            init();
            n = read();
            for(int i = 1; i <= n - 1; i ++){
                int u = read(), v = read(), c = read();
                d[u] ++, d[v] ++;
                addEdge(u, v, c), addEdge(v, u, c);
            }
            dfs(1, 0), f[1] = dp[1], calc(1, 0);
            int ans = 0;
            for(int i = 1; i <= n; i ++) ans = max(ans, f[i]);
            printf("%d
    ", ans);
        }
        return 0;
    }
    
  • 相关阅读:
    Linux 安装apache2时候遇到的问题
    ubuntu安装php时出现error: xml2config not found. Please check your libxml2 installation 的解决办法
    Linux一块网卡绑定2个IP地址
    php 满足条件后跳转的几种方法.
    Linux 安装php 遇到 libtool: link: `ext/date/php_date.lo' is not a valid libtool object
    js 光标移动到输入框最后位置函数
    Linux iptables 防火墙相关命令介绍及使用
    tcpdump的简单选项介绍
    子网掩码转换函数
    Linux 十六进制转换十进制的函数
  • 原文地址:https://www.cnblogs.com/onionQAQ/p/11203217.html
Copyright © 2011-2022 走看看