hdu-6035 Colorful Tree
题目意思是计算所有路径(n*(n-1)/2)经过的不同颜色的数目和。
这个数目和可以转化为每种颜色经过的路径数目的求和,而这个求和又等价于颜色总数*n*(n-1)/2-没有经过某种颜色的边的数量的求和。
没有结果某种颜色的数量等价于由此颜色的节点将树拆分出多个联通块,每个联通块的节点数为k,则边数为k*(k-1)/2。
#include <iostream>
#include <vector>
#include <set>
#define LL long long
using namespace std;
int n;
const int N = ;
vector<int> g[N];
LL sum[N];//维护以某种颜色为根的子树和
int colors[N];
bool vis[N];
LL wayCnt;
void init()
{
fill(vis, vis + n + , );
fill(sum, sum + n + , );
for (int i = ; i <= n; i++) g[i].clear();
wayCnt = ;
}
LL dfs(int now)
{
int col = colors[now];
sum[col]++;
LL preSum = sum[col];//用以计算当前点到某子树之间的联通块大小
vis[now] = ;
LL nowTreeSize = ;
int fuck = ;
for (int i = ; i < g[now].size(); i++)
{
int nx = g[now][i];
if (vis[nx]) continue;
LL siz=dfs(nx);
LL change = sum[col] - preSum;//计算出底层增量
LL blockSize = siz - change;
//cout << now << ": " << blockSize << endl;
sum[col] += blockSize;
preSum = sum[col];
fuck += blockSize;
wayCnt += blockSize*(blockSize - ) / ;
nowTreeSize += siz;
}
return nowTreeSize;
}
int main()
{
cin.sync_with_stdio(false);
int cas = ;
while (cin >> n)
{
set<int> cc;
for (int i = ; i <= n; i++)
cin >> colors[i],cc.insert(colors[i]);
init();
for (int i = ; i < n-; i++)
{
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
dfs();
//cout << wayCnt << endl;
for (int i = ; i <=n; i++)
{
if (cc.find(i)!=cc.end()&&i != colors[])
{
LL blockSize = n - sum[i];
wayCnt += blockSize*(blockSize - ) / ;
}
}
cout << "Case #" << cas++ << ": ";
cout << cc.size()*n*(n-)/-wayCnt << endl;
}
return ;
}
hdu-6035 Colorful Tree的更多相关文章
- HDU 6035 - Colorful Tree | 2017 Multi-University Training Contest 1
/* HDU 6035 - Colorful Tree [ DFS,分块 ] 题意: n个节点的树,每个节点有一种颜色(1~n),一条路径的权值是这条路上不同的颜色的数量,问所有路径(n*(n-1)/ ...
- 2017 Multi-University Training Contest - Team 1 1003&&HDU 6035 Colorful Tree【树形dp】
Colorful Tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- HDU 6035 Colorful Tree(补集思想+树形DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6035 [题目大意] 给出一颗树,一条路径的价值为其上点权的种类数,求路径总价值 [题解] 单独考虑 ...
- HDU 6035 Colorful Tree (树形DP)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6035 [题目大意] 给出一颗树,一条路径的价值为其上点权的种类数,求路径总价值 [题解] 我们计算 ...
- 2017ACM暑期多校联合训练 - Team 1 1003 HDU 6035 Colorful Tree (dfs)
题目链接 Problem Description There is a tree with n nodes, each of which has a type of color represented ...
- HDU 6035 Colorful Tree(dfs)
题意:一棵有n个点的树,树上每个点都有颜色c[i],定义每条路径的值为这条路径上经过的不同颜色数量和.求所有路径的值的和. 可以把问题转化为对每种颜色有多少条不同的路径至少经过这种颜色的点,然后加和. ...
- hdu 6035:Colorful Tree (2017 多校第一场 1003) 【树形dp】
题目链接 单独考虑每一种颜色,答案就是对于每种颜色至少经过一次这种的路径条数之和.反过来思考只需要求有多少条路径没有经过这种颜色即可. 具体实现过程比较复杂,很神奇的一个树形dp,下面给出一个含较详细 ...
- hdu 6035 Colorful Tree(虚树)
考虑到树上操作:首先题目要我们求每条路径上出现不同颜色的数量,并把所有加起来得到答案:我们知道俩俩点之间会形成一条路径,所以我们可以知道每个样例的总的路径的数目为:n*(n-1)/2: 这样单单的求, ...
- HDU 5574 Colorful Tree
• 给出一棵树,每个点有初始的颜色,支持两种操作• 将一个点的子树染成一种给定颜色• 问一个点的子树里有几种不同的颜色 •
- HDU 6035 (虚树)(统计颜色)
HDU 6035 Colorful Tree Problem : 给一棵树,每个结点有一种颜色,定义每条路径的权值为这条路径上颜色的种数,询问所有路径(C(n,2)条)的权值之和. Solution ...
随机推荐
- 5.sql2008分组与嵌套
1.Group by基本介绍;2.Having的使用;3.分组综合应用;4.子查询基本介绍;5.In/Exists/Any/Some/All;6.子查询综合应用; 1.Group by基本介绍:依据B ...
- freeswitch源码安装
1.源码--安装依赖: yum install -y http://files.freeswitch.org/freeswitch-release-1-6.noarch.rpm epel-releas ...
- 配置Jenkins 实现自动发布maven项目至weblogic(svn+maven+weblogic12c)
Jenkins安装完成之后,需要我们对其配置,然后才可以实现自动部署项目. 前提 防火墙开放weblogic的7001端口 Linux(CentOS):firewall-cmd --zone=publ ...
- Python 读写文件 中文乱码 错误TypeError: write() argument must be str, not bytes+
今天写上传文件代码,如下 def uploadHandle(request): pic1=request.FILES['pic1'] picName=os.path.join(settings.MED ...
- 算法总结(转自CS-Notes)
转载地址: 注意要点: 1.希尔排序:实际是将元素按步距h分为几组,每组元素没有关系,是组里每个元素跨步距h得到的一组元素是有序的,那么剩下的问题就是组内有序,再处理好组间边界即可.实际解决的方式是不 ...
- 题解——洛谷P1250 种树(差分约束)
一道看一眼就知道差分约束的题目 但是最短路spfa的时候注意松弛条件是 if(dis[u]+w[i]<dis[v[i]]) dis[v[i]]=dis[u]+w[i]; 不能写成 if(dis[ ...
- (转)Paper list of Meta Learning/ Learning to Learn/ One Shot Learning/ Lifelong Learning
Meta Learning/ Learning to Learn/ One Shot Learning/ Lifelong Learning 2018-08-03 19:16:56 本文转自:http ...
- 关不掉.vbs
创建: 1.在桌面新建一个 关不掉.txt 文本文档 2.打开输入一下内容 do msgbox"信不信你关不掉我" msgbox"哈哈,你相信了吧" msgbo ...
- python + eclipse +pydev
本文重点介绍使用Eclipse+pydev插件来写Python代码, 以及在Mac上配置Eclipse+Pydev 和Windows配置Eclipse+Pydev 编辑器:Python 自带的 ID ...
- PHP直接将文件流转换为字符串
有时候不需要图片直接输出到浏览器,需要如下处理! 输出到浏览器 $qrCode = new QrCode(); $qrCode ->setText('Life is too short to b ...