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;
    }
    
  • 相关阅读:
    Vue基础知识总结(一)
    D3.js系列——布局:弦图和集群图/树状图
    D3.js系列——布局:饼状图和力导向图
    D3.js系列——交互式操作和布局
    SQLServer调试
    SQL Server性能常用语句
    sqlserver索引
    从 datetime2 数据类型到 datetime 数据类型的转换产生一个超出范围的值
    EntityFrame6在本地可以正常使用,部署到IIS后报异常(Additional information: The underlying provider failed on Open.)
    从对象创建和引用小议解耦
  • 原文地址:https://www.cnblogs.com/heyuhhh/p/12911806.html
Copyright © 2011-2022 走看看