zoukankan      html  css  js  c++  java
  • 【atcoder abc163F】 F

    传送门

    题意:
    给定一颗无根树,每个结点上面有一种颜色(c_i)
    假设现在有一条(u ightarrow v)的路径,上面经过了颜色(c_1,c_2,..,c_k),那么这条路径会对每种颜色产生一次贡献(一条路径对一种颜色至多产生一次贡献)。
    最终问任意两点路径对每种颜色产生的贡献为多少。

    思路:

    • 直接将问题转化为对于每种颜色,求有多少条路径不经过该颜色。
    • 那么每种颜色我们可以单独考虑,考虑颜色(i),那么可以发现颜色(i)将树分为了若干个连通块,每个连通块内部的所有路径都不经过颜色(i)。那么对每种颜色求出连通块大小即可。
    • 子树连通块数量具有可减性质,所以可以考虑树上差分,我们用(sz[v]-连通块结点个数和)就行。

    细节见代码:

    /*
     * Author:  heyuhhh
     * Created Time:  2020/5/16 18:38:57
     */
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    #include <vector>
    #include <cmath>
    #include <set>
    #include <map>
    #include <queue>
    #include <iomanip>
    #include <assert.h>
    #define MP make_pair
    #define fi first
    #define se second
    #define pb push_back
    #define sz(x) (int)(x).size()
    #define all(x) (x).begin(), (x).end()
    #define INF 0x3f3f3f3f
    #define Local
    #ifdef Local
      #define dbg(args...) do { cout << #args << " -> "; err(args); } while (0)
      void err() { std::cout << std::endl; }
      template<typename T, typename...Args>
      void err(T a, Args...args) { std::cout << a << ' '; err(args...); }
      template <template<typename...> class T, typename t, typename... A> 
      void err(const T <t> &arg, const A&... args) {
      for (auto &v : arg) std::cout << v << ' '; err(args...); }
    #else
      #define dbg(...)
    #endif
    using namespace std;
    typedef long long ll;
    typedef pair<int, int> pii;
    //head
    const int N = 2e5 + 5;
    
    int n;
    int c[N], cnt[N];
    vector <int> G[N];
    
    int sz[N], sum[N];
    ll ans[N];
    
    ll f(int x) {
        return 1ll * x * (x + 1) / 2;   
    }
    
    void dfs(int u, int fa) {
        sz[u] = 1;
        int t = sum[c[u]];
        for (auto v : G[u]) if (v != fa) {
            sum[c[u]] = 0;
            dfs(v, u);
            sz[u] += sz[v];
            ans[c[u]] -= f(sz[v] - sum[c[u]]);
        }
        sum[c[u]] = t + sz[u];
    }
    
    void run() {
        cin >> n;
        for (int i = 1; i <= n; i++) {
            cin >> c[i];
            ++cnt[c[i]];
        }
        for (int i = 1; i < n; i++) {
            int u, v; cin >> u >> v;
            G[u].push_back(v);
            G[v].push_back(u);
        }
        dfs(1, 0);
        for (int i = 1; i <= n; i++) {
            ans[i] += f(n) - f(n - sum[i]);
        }
        for (int i = 1; i <= n; i++) {
            cout << ans[i] << '
    ';
        }
    }
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(0); cout.tie(0);
        cout << fixed << setprecision(20);
        run();
        return 0;
    }
    
  • 相关阅读:
    RPC细谈
    RPC浅谈
    动态规划
    libco 的定时器实现: 时间轮
    一次HTTP请求的完整过程——协议篇(DNS、TCP、HTTP)
    多个CPU、多核CPU以及超线程(Hyper-Threading)
    Linux下which、whereis、locate、find命令的区别
    warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失
    使用OutputDebugString输出调试信息
    VS或windows用代码产生GUID
  • 原文地址:https://www.cnblogs.com/heyuhhh/p/12911806.html
Copyright © 2011-2022 走看看