2017 Multi-University Training Contest - Team 1 03Colorful Tree

地址: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\n",cs++,cnt*n*(n-1LL)/2LL-ans);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/weeping/p/7248046.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值