zoukankan      html  css  js  c++  java
  • F. Maximum White Subtree

    You are given a tree consisting of nn vertices. A tree is a connected undirected graph with n1n−1 edges. Each vertex vv of this tree has a color assigned to it (av=1av=1 if the vertex vv is white and 00 if the vertex vv is black).

    You have to solve the following problem for each vertex vv: what is the maximum difference between the number of white and the number of black vertices you can obtain if you choose some subtree of the given tree that contains the vertex vv? The subtree of the tree is the connected subgraph of the given tree. More formally, if you choose the subtree that contains cntwcntw white vertices and cntbcntb black vertices, you have to maximize cntwcntbcntw−cntb.

    Input

    The first line of the input contains one integer nn (2n21052≤n≤2⋅105) — the number of vertices in the tree.

    The second line of the input contains nn integers a1,a2,,ana1,a2,…,an (0ai10≤ai≤1), where aiai is the color of the ii-th vertex.

    Each of the next n1n−1 lines describes an edge of the tree. Edge ii is denoted by two integers uiui and vivi, the labels of vertices it connects (1ui,vin,uivi(1≤ui,vi≤n,ui≠vi).

    It is guaranteed that the given edges form a tree.

    Output

    Print nn integers res1,res2,,resnres1,res2,…,resn, where resiresi is the maximum possible difference between the number of white and black vertices in some subtree that contains the vertex ii.

    Examples
    input
    Copy
    9
    0 1 1 1 0 0 0 0 1
    1 2
    1 3
    3 4
    3 5
    2 6
    4 7
    6 8
    5 9
    
    output
    Copy
    2 2 2 2 2 1 1 0 2 
    
    input
    Copy
    4
    0 0 1 0
    1 2
    1 3
    1 4
    
    output
    Copy
    0 -1 1 -1 
    
    Note

    The first example is shown below:

    The black vertices have bold borders.

    In the second example, the best subtree for vertices 2,32,3 and 44 are vertices 2,32,3 and 44 correspondingly. And the best subtree for the vertex 11 is the subtree consisting of vertices 11 and 33.

     两次dfs

    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    #include <deque>
    #include <bitset>
    //#include <unordered_set>
    //#include <unordered_map>
    #define ll              long long
    #define pii             pair<int, int>
    #define rep(i,a,b)      for(int  i=a;i<=b;i++)
    #define dec(i,a,b)      for(int  i=a;i>=b;i--)
    #define forn(i, n)      for(int i = 0; i < int(n); i++)
    using namespace std;
    int dir[4][2] = { { 1,0 },{ 0,1 } ,{ 0,-1 },{ -1,0 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = 3.14159265358979323846;
    const double eps = 1e-6;
    const int mod = 1e9 + 7;
    const int N = 2e5 + 5;
    //if(x<0 || x>=r || y<0 || y>=c)
    
    inline ll read()
    {
        ll x = 0; bool f = true; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
        while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
        return f ? x : -x;
    }
    ll gcd(ll m, ll n)
    {
        return n == 0 ? m : gcd(n, m % n);
    }
    ll lcm(ll m, ll n)
    {
        return m * n / gcd(m, n);
    }
    bool prime(int x) {
        if (x < 2) return false;
        for (int i = 2; i * i <= x; ++i) {
            if (x % i == 0) return false;
        }
        return true;
    }
    ll qpow(ll m, ll k, ll mod)
    {
        ll res = 1, t = m;
        while (k)
        {
            if (k & 1)
                res = res * t % mod;
            t = t * t % mod;
            k >>= 1;
        }
        return res;
    }
    vector<int> a(N),dp(N),ans(N),node[N];
    void dfs1(int u, int fa)
    {
        dp[u] = a[u];
        for (auto v : node[u])
        {
            if (v == fa)
                continue;
            dfs1(v, u);
            dp[u] += max(dp[v], 0);
        }
    }
    void dfs2(int u, int fa, int sum)
    {
        ans[u] = dp[u] + sum;
        for (auto v : node[u])
        {
            if (v == fa)
                continue;
            dfs2(v, u, max(0, ans[u] - max(0, dp[v])));
        }
    }
    int main()
    {
        int n;
        cin >> n;
        rep(i, 1, n)
        {
            cin >> a[i];
            if (a[i] == 0)
                a[i] = -1;
        }
        rep(i, 1, n-1)
        {
            int u, v;
            cin >> u >> v;
            node[u].push_back(v);
            node[v].push_back(u);
        }
        dfs1(1, -1);
        dfs2(1, -1, 0);
        rep(i, 1, n)
            cout << ans[i] << " ";
        return 0;
    }
  • 相关阅读:
    【代码笔记】iOS-JQIndicatorViewDemo
    【代码笔记】iOS-plist获得城市列表
    【代码笔记】iOS-HTTPQueue下载图片
    【代码笔记】iOS-请求去掉url中的空格
    【代码笔记】iOS-collectionView实现照片删除
    【代码笔记】iOS-自定义loading(IanAlert)
    Foundation框架-NSCalendar
    iOS Xcode全面剖析
    Mac下Git的基础操作
    iOS开发UI篇—常见的项目文件介绍
  • 原文地址:https://www.cnblogs.com/dealer/p/13233136.html
Copyright © 2011-2022 走看看