/*hdu6035[dfs+思维] 2017多校1*/
//合并色块, 妙啊妙啊
#include<bits/stdc++.h>
using namespace std;
const double eps=1e-;
const int inf=0x3f3f3f3f;
typedef long long LL;
vector<int>G[];
LL sum[];
int c[],son[],mark[];
int n,u,v,cnt=,kase=;
LL ans,res;
void dfs(int u,int fa){
son[u]=;
LL y=sum[c[u]];
LL x=sum[c[u]];
for(int i=;i<G[u].size();++i){
int v=G[u][i];
if(v!=fa){
dfs(v,u);
son[u]+=son[v];
LL temp=son[v]-sum[c[u]]+x;
res+=(temp-1LL)*temp/2LL;
x=sum[c[u]];
}
}
sum[c[u]]+=son[u]-(sum[c[u]]-y);
//printf("Node #%d, Color:%d, Sum[%d]:%lld, Son:%d\n",u,c[u],c[u],sum[c[u]],son[u]);
}
void solve(){
res=ans=;
dfs(,-);
//cout<<"Res: "<<res<<endl;
ans=cnt*1LL*n*(n-)/2LL-res;
for(int i=;i<=n;i++){
if(mark[i]){
ans-=(n-sum[i])*(n-sum[i]-)/2LL;
//printf("Color:%d, Sum[%d]:%d, Ans:%d\n",i,i,sum[i],ans);
}
}
printf("Case #%d: %lld\n",kase++,ans);
}
int main(){
while(~scanf("%d",&n)){
cnt=;
memset(sum,,sizeof(sum));
memset(son,,sizeof(son));
memset(mark,,sizeof(mark));
for(int i=;i<=n+;i++)
G[i].clear();
for(int i=;i<=n;i++){
scanf("%d",&c[i]);
if(!mark[c[i]]){
mark[c[i]]++;
cnt++;
}
}
for(int i=;i<=n-;i++){
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
}
solve();
}
return ;
}
/*
8
1 2 1 1 2 1 2 1
1 2
1 3
2 4
2 5
3 6
4 7
5 8
12
1 2 1 1 2 1 2 2 1 2 2 2
1 2
1 3
2 4
2 5
3 6
3 7
4 8
5 9
5 10
7 11
7 12
13
1 2 1 1 2 1 2 2 1 1 2 2 2
1 2
1 3
2 4
2 5
3 6
3 7
4 8
4 9
5 10
5 11
7 12
7 13
*/

hdu6035[dfs+思维] 2017多校1的更多相关文章

  1. hdu6074[并查集+LCA+思维] 2017多校4

    看了标答感觉思路清晰了许多,用并查集来维护全联通块的点数和边权和. 用另一个up[]数组(也是并查集)来保证每条边不会被重复附权值,这样我们只要将询问按权值从小到大排序,一定能的到最小的边权和与联通块 ...

  2. HDU6038-Function-数学+思维-2017多校Team01

    学长讲座讲过的,代码也讲过了,然而,当时上课没来听,听代码的时候也一脸o((⊙﹏⊙))o 我的妈呀,语文不好是硬伤,看题意看了好久好久好久(死一死)... 数学+思维题,代码懂了,也能写出来,但是还是 ...

  3. 【构造+DFS】2017多校训练三 HDU 6060 RXD and dividing

    acm.hdu.edu.cn/showproblem.php?pid=6060 [题意] 给定一棵以1为根的树,把这颗树除1以外的结点划分为k个集合(可以有空集),把1加入划分后的集合 每个集合的结点 ...

  4. hdu6059[字典树+思维] 2017多校3

    #include <bits/stdc++.h> using namespace std; typedef long long LL; * ][]; * ]; * ]; ][]; ; LL ...

  5. hdu6073[dfs+删边] 2017多校4

    题目中对二分图的定义十分特殊, 指的是 U,V两部分中,U的顶点度数必定为2,V中顶点无限制. 题目要求的是 对于所有匹配,该匹配的权值=该匹配中选中的边的边权的乘积,求所有匹配权值之和. 对于V中的 ...

  6. hdu6060[贪心+dfs] 2017多校3

    /* hdu6060[贪心+dfs] 2017多校3*/ #include <bits/stdc++.h> using namespace std; typedef long long L ...

  7. 2017 多校5 hdu 6093 Rikka with Number

    2017 多校5 Rikka with Number(数学 + 数位dp) 题意: 统计\([L,R]\)内 有多少数字 满足在某个\(d(d>=2)\)进制下是\(d\)的全排列的 \(1 & ...

  8. 2017 多校5 Rikka with String

    2017 多校5 Rikka with String(ac自动机+dp) 题意: Yuta has \(n\) \(01\) strings \(s_i\), and he wants to know ...

  9. 2017 多校4 Security Check

    2017 多校4 Security Check 题意: 有\(A_i\)和\(B_i\)两个长度为\(n\)的队列过安检,当\(|A_i-B_j|>K\)的时候, \(A_i和B_j\)是可以同 ...

随机推荐

  1. jsp四大作用域之Application

    <%@ page language="java" contentType="text/html; charset=utf-8"pageEncoding=& ...

  2. hihoCoder #1165 : 益智游戏 (挑战赛11 B题)

    题意:在一个序列中找到两个数a和b,使得a*b的因子个数最多,输出最多的因子个数. 思路:数据较多,处理会很慢.对序列中每个数字进行质数分解求因子个数,然后按照因子个数降序排列,对前50个因子最多的数 ...

  3. last命令

    last——列出目前与过去登入系统的用户信息 命令所在路径:/usr/bin/last 示例1: $ last

  4. 全站变灰的CSS代码-适合特殊时刻哀悼

    将下面CSS代码应用到HTML标签或者是BODY标签 filter: url("data:image/svg+xml;utf8,<svg xmlns=\'http://www.w3.o ...

  5. rhythmbox插件开发笔记3:安装 makefile && schema && po

    本篇主要讲通过makefile方式来安装rhythmbox插件的相关知识. makefile 如果makefile是什么,请自行谷歌 参考了pandasunny同学的rhythmbox-baidu-m ...

  6. Beta版本发布

    这个作业属于哪个课程 https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1/?page=2 这个作业要求在哪里 <作业要求的 ...

  7. python_89_configparser模块

    用于生成和修改常见配置文档,当前模块的名称在 python 3.x 版本中变更为 configparser.在python2.x版本中为ConfigPsresr 来看一个好多软件的常见文档格式如下 [ ...

  8. 动态代理--Cglib

    JDK 的Proxy 实现,需要代理对象实现接口: package com.utils; import java.lang.reflect.InvocationHandler; import java ...

  9. UIDeviceOrientation 和 UIInterfaceOrientation

    有时候,我们处理自动布局时,需要获取到屏幕旋转方向: 以下为本人亲测: UIInterfaceOrientation: 我们需要在- (void)viewDidLoad或其他方法中添加观察者,检测屏幕 ...

  10. const,static,extern,#define

    一.const // 简单定义变量,可以修改变量的值 ; a = ; // const的用法 // 用法一: ; ; // 不允许修改,因为 const 修饰 b/c,指定 b/c 为常量!! // ...