zoukankan      html  css  js  c++  java
  • Colorful Tree

    Colorful Tree

    Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
    Total Submission(s): 3373    Accepted Submission(s): 1461


    Problem Description
    There is a tree with n nodes, each of which has a type of color represented by an integer, where the color of node i is ci.

    The path between each two different nodes is unique, of which we define the value as the number of different colors appearing in it.

    Calculate the sum of values of all paths on the tree that has n(n1)2 paths in total.
     
    Input
    The input contains multiple test cases.

    For each test case, the first line contains one positive integers n, indicating the number of node. (2n200000)

    Next line contains n integers where the i-th integer represents ci, the color of node i(1cin)

    Each of the next n1 lines contains two positive integers x,y (1x,yn,xy), meaning an edge between node x and node y.

    It is guaranteed that these edges form a tree.
     
    Output
    For each test case, output "Case #xy" in one line (without quotes), where x indicates the case number starting from 1 and y denotes the answer of corresponding case.
     
    Sample Input
    3 1 2 1 1 2 2 3 6 1 2 1 3 2 1 1 2 1 3 2 4 2 5 3 6
     
    Sample Output
    Case #1: 6 Case #2: 29
     
    Source
    #pragma GCC optimize(2)
    
    #include <bits/stdc++.h>
    
    #define lowbit(x) x&(-x)
    typedef long long ll;
    using namespace std;
    const int maxn = 2e5 + 100000;
    ll n;
    ll siz[maxn], sum[maxn], cnt;
    ll color[maxn], vis[maxn];
    struct node {
        int to, nx;
    } o[maxn << 1];
    int head[maxn];
    ll res;
    
    inline void add_edge(int u, int v) {
        o[++cnt] = (node) {v, head[u]};
        head[u] = cnt;
    }
    
    inline void solve(int cur, int fa) {
        siz[cur] = 1;
        ll pre = sum[color[cur]];
        ll e = 0;
        for (register int i = head[cur]; i; i = o[i].nx) {
            int to = o[i].to;
            if (to == fa)continue;
            solve(to, cur);
            siz[cur] += siz[to];
            ll nx = sum[color[cur]] - pre;
            ll fur = nx;
            nx = siz[to] - nx;
            res -= nx * (nx - 1) / 2;
            pre = sum[color[cur]];
            e += fur;
        }
        sum[color[cur]] += siz[cur] - e;
    }
    
    int Case;
    
    int main() {
    #ifndef ONLINE_JUDGE
        freopen("1.txt", "r", stdin);
    #endif
        while (scanf("%lld", &n) != EOF) {
            ll tot = 0;
            cnt = 0;
            memset(head, 0, sizeof(head));
            memset(vis, 0, sizeof(vis));
            memset(sum,0,sizeof(sum));
            for (register int i = 1; i <= n; ++i) {
                scanf("%d", &color[i]);
                if (!vis[color[i]]) {
                    ++tot;
                    vis[color[i]] = 1;
                }
            }
            for (register int i = 1, u, v; i < n; ++i) {
                scanf("%d%d", &u, &v);
                add_edge(u, v);
                add_edge(v, u);
            }
            res = tot * n * (n - 1) / 2;
            solve(1, -1);
            for (register int i = 1; i <= n; ++i) {
                if (!sum[i])continue;
                ll leave = n - sum[i];
                res -= leave * (leave - 1) / 2;
            }
            printf("Case #%d: ", ++Case);
            printf("%lld
    ", res);
        }
        return 0;
    }
  • 相关阅读:
    Linux 下安装配置 JDK7
    win7下virtualbox装linux共享win7文件问题(已测试可用)
    Linix常用命令
    JAVA命令大全
    virtualbox 不能为虚拟电脑打开一个新任务/VT-x features locked or unavailable in MSR.
    VirtualBox下安装rhel5.5 linux系统
    redhat RHEL 5.5 下载地址
    ios开发@selector的函数如何传参数/如何传递多个参数
    U盘10分钟安装linux系统
    史上最浅显易懂的Git分布式版本控制系统教程
  • 原文地址:https://www.cnblogs.com/czy-power/p/11451657.html
Copyright © 2011-2022 走看看