zoukankan      html  css  js  c++  java
  • 2017 Multi-University Training Contest

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6035

    题面:

    Colorful Tree

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


    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
     

     思路:很巧妙的树形dp!

      具体看这个博客把,我也是看了这个才懂的http://blog.csdn.net/Bahuia/article/details/76141574

    #include <bits/stdc++.h>
    
    using namespace std;
    
    #define MP make_pair
    #define PB push_back
    typedef long long LL;
    typedef pair<int,int> PII;
    const double eps=1e-7;
    const double pi=acos(-1.0);
    const int K=1e6+7;
    const int mod=1e9+7;
    
    vector<int>mp[K];
    LL n,cnt,ans,sz[K],col[K],vis[K],sum[K];
    
    void dfs(int x,int f)
    {
        LL tmp,add,all=0;
        sz[x]=1;
        for(auto v:mp[x])
        if(v!=f)
        {
            tmp=sum[col[x]];
            dfs(v,x);
            sz[x]+=sz[v];
            add=sum[col[x]]-tmp;
            ans+=(sz[v]-add)*(sz[v]-add-1)/2;
            all+=sz[v]-add;
        }
        sum[col[x]]+=all+1;
    }
    
    int main(void)
    {
        int cs=1;
        while(~scanf("%lld",&n))
        {
            cnt=0,ans=0;
            memset(mp,0,sizeof mp);
            memset(sum,0,sizeof sum);
            memset(sz,0,sizeof sz);
            for(int i=1;i<=n;i++) scanf("%d",col+i),vis[col[i]]=1;
            for(int i=1,x,y;i<n;i++) scanf("%d%d",&x,&y),mp[x].PB(y),mp[y].PB(x);
            dfs(1,0);
            for(int i=1;i<=n;i++)
            if(vis[i])
                ans+=(n-sum[i]-1)*(n-sum[i])/2,vis[i]=0,cnt++;
            printf("Case #%d: %lld
    ",cs++,cnt*n*(n-1LL)/2LL-ans);
        }
        return 0;
    }
  • 相关阅读:
    Linux A机器免密码SSH登录B机器
    Linux 系统命令笔记
    整理笔记有感而发
    Linux(Centos)快速搭建SVN
    APP接口自动化测试JAVA+TestNG(三)之HTTP接口测试实例
    Windows服务器环境下jenkins下载和安装
    Win10系统如何在防火墙里开放端口
    Java 通过地址获取经纬度
    一、springBoot简介与环境搭建
    2018年尚硅谷《全套Java、Android、HTML5前端视频》
  • 原文地址:https://www.cnblogs.com/weeping/p/7248046.html
Copyright © 2011-2022 走看看