hdu6035[dfs+思维] 2017多校1
/*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的更多相关文章
- hdu6074[并查集+LCA+思维] 2017多校4
看了标答感觉思路清晰了许多,用并查集来维护全联通块的点数和边权和. 用另一个up[]数组(也是并查集)来保证每条边不会被重复附权值,这样我们只要将询问按权值从小到大排序,一定能的到最小的边权和与联通块 ...
- HDU6038-Function-数学+思维-2017多校Team01
学长讲座讲过的,代码也讲过了,然而,当时上课没来听,听代码的时候也一脸o((⊙﹏⊙))o 我的妈呀,语文不好是硬伤,看题意看了好久好久好久(死一死)... 数学+思维题,代码懂了,也能写出来,但是还是 ...
- 【构造+DFS】2017多校训练三 HDU 6060 RXD and dividing
acm.hdu.edu.cn/showproblem.php?pid=6060 [题意] 给定一棵以1为根的树,把这颗树除1以外的结点划分为k个集合(可以有空集),把1加入划分后的集合 每个集合的结点 ...
- hdu6059[字典树+思维] 2017多校3
#include <bits/stdc++.h> using namespace std; typedef long long LL; * ][]; * ]; * ]; ][]; ; LL ...
- hdu6073[dfs+删边] 2017多校4
题目中对二分图的定义十分特殊, 指的是 U,V两部分中,U的顶点度数必定为2,V中顶点无限制. 题目要求的是 对于所有匹配,该匹配的权值=该匹配中选中的边的边权的乘积,求所有匹配权值之和. 对于V中的 ...
- hdu6060[贪心+dfs] 2017多校3
/* hdu6060[贪心+dfs] 2017多校3*/ #include <bits/stdc++.h> using namespace std; typedef long long L ...
- 2017 多校5 hdu 6093 Rikka with Number
2017 多校5 Rikka with Number(数学 + 数位dp) 题意: 统计\([L,R]\)内 有多少数字 满足在某个\(d(d>=2)\)进制下是\(d\)的全排列的 \(1 & ...
- 2017 多校5 Rikka with String
2017 多校5 Rikka with String(ac自动机+dp) 题意: Yuta has \(n\) \(01\) strings \(s_i\), and he wants to know ...
- 2017 多校4 Security Check
2017 多校4 Security Check 题意: 有\(A_i\)和\(B_i\)两个长度为\(n\)的队列过安检,当\(|A_i-B_j|>K\)的时候, \(A_i和B_j\)是可以同 ...
随机推荐
- COGS 2794. 爱摔跤的比利海灵顿
★☆ 输入文件:find_k.in 输出文件:find_k.out 简单对比时间限制:1.4 s 内存限制:128 MB [题目描述] B•海灵顿•雷想要和n个巨人比试摔♂跤,他想先和 ...
- BZOJ 1806: [Ioi2007]Miners 矿工配餐
ime Limit: 10 Sec Memory Limit: 64 MBSubmit: 910 Solved: 559[Submit][Status][Discuss] Description ...
- [VC]char 和 wchar_t相互转化
#include <windows.h> #include <stdio.h> //function: charTowchar //purpose:char to WCHAR ...
- topcpder SRM 664 div2 A,B,C BearCheats , BearPlays equalPiles , BearSorts (映射)
A题,熊孩子测视力,水题,题意就是判断一下两个数对应位不相同的数字有多少个. #include<bits/stdc++.h> using namespace std; class Bear ...
- ubuntu 18.04下 配置qt opencv的坑
问题和过程描述: 我按照网上的教程装了qt5.8版本,然后去配置opencv,感觉一切顺利,然后随便写了个 Mat src = imread("xxx") 然后imshow发现编译 ...
- 第011课_串口(UART)的使用
from: 第011课_串口(UART)的使用 第001节_辅线1_硬件知识_UART硬件介绍 1. 串口的硬件介绍 UART的全称是 Universal Asynchronous Receiver ...
- Android开发出现 StackOverflowError
问题:StackOverflowError 在HTC或者摩托罗拉的手机上测试出现 StackOverflowError 的错误. 06-12 10:28:31.750: E/AndroidRuntim ...
- 使用EventLog组件保存Windows系统日志
实现效果: 知识运用: EventLog类的CreateEventSource方法 //用于建立一个应用程序 使用指定的Sourc作为向本机上的日志中写入日志项的有效事件源 CreateEventS ...
- 脚手架创建一个React项目
一.安装 1.安装node.js 官网地址 https://nodejs.org/en/ 进入后点击下载,官方网站会根据你的系统类型推荐最适合你安装的版本.(如果已经安装了node.js跳过此步)如下 ...
- 关于List的remove方法我遇到的坑
结果是下标越界异常,原因是remove方法的参数不是value,而是index 唉~~~ 年少轻狂啊