HDU60 子树路径问题

Colorful Tree

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


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
 
题意:在一颗树上,有不同颜色的节点,以数字代表,而从一个点到另一个点的权值等于这条路径上经过的颜色数量
题目要求这颗数的所有路径价值总和
要直接去求价值总和的话非常困难,于是我们可以知道对于n个点的树,有n * (n - 1) / 2 条路径,那么它最大化情况下的价值就是 总颜色种类 * n * (n - 1) / 2 ;
算出了最大值后,去算每种颜色不能经过的路径数全部加起来,然后用最大值去减掉不存在的就得出了存在的最大值了
用dfs去跑树,但是由于相同颜色的点会多算边,于是应该对于两个相同的颜色的点之间的路径刨去

#include<stdio.h>
#include<vector>
#include<string.h>
using namespace std;
#define ll long long
#define MAXN 200005

vector<ll>vec[MAXN];
ll col[MAXN],size[MAXN],sum[MAXN],mark[MAXN];
ll ans = 0;

void dfs(ll u,ll v){
	ll all = 0;
	size[u] = 1;
	for(int i = 0; i < vec[u].size();i++){
		ll to;
		to = vec[u][i];
		if(to == v)
			continue;
		ll s = sum[col[u]];  // s先保存当前颜色u的截断值 
		dfs(to,u);
		size[u] += size[to];  
//		printf("size[%lld] += size[%lld]  -----  = %lld\n",u,to,size[u]); 
		ll step = sum[col[u]] - s;  // 儿子里面出现了重复的,记录下来,要剪掉 
//		printf("step = %lld\n",step);
		all += step;		//所有的重复记录,最后从左子树到右子树部分,要剪掉所有重复的 
//		printf("all = %lld\n",all);
		ans += (size[to] - step) * (size[to] - step - 1) / 2;
	}
	sum[col[u]] += size[u] - all;  //减去重复 
//	printf("sum[col[%lld]] += size[%lld] - all (%lld - %lld = %lld) = %lld\n",u,u,size[u],all,size[u] - all,sum[col[u]]);
}

int main(){
	ll n,Case = 1;
	while(scanf("%lld",&n) != EOF){
		memset(mark,0,sizeof(mark));
		memset(size,0,sizeof(size));
		memset(sum,0,sizeof(sum));
		for(int i = 0;i < MAXN;i++){
			vec[i].clear();
		}
		ll totCol = 0;
		ans = 0;
		for(int i = 1;i <= n;i++){
			scanf("%lld",&col[i]);
			totCol += mark[col[i]] ^ 1;
			mark[col[i]] = 1;
		}
		for(int i = 0;i < n - 1;i++){
			ll u,v;
			scanf("%lld %lld",&u,&v);
			vec[u].push_back(v);
			vec[v].push_back(u);
		} 
		dfs(1,-1);
		ll ANS = n * (n - 1) / 2 * totCol;
		for(int i = 1;i <= n;i++){
			if(totCol == col[1] || !mark[i])
				continue;
			ans += (n - sum[i]) * (n - sum[i] - 1) / 2; 
		}
		printf("Case #%d: %lld\n",Case++,ANS - ans);
	}
	return 0;
}
机器人用英语怎么说英文翻译及例句(1)全文共2页,当前为第1页。机器人用英语怎么说英文翻译及例句(1)全文共2页,当前为第1页。机器人用英语怎么说英文翻译及例句 机器人用英语怎么说英文翻译及例句(1)全文共2页,当前为第1页。 机器人用英语怎么说英文翻译及例句(1)全文共2页,当前为第1页。 机器人是指能够自动执行工作的机器装置,如今关于机器人的科技已经越来越发达了,那么你知道机器人用英语怎么说吗?下面跟店铺一起学习机器人的英语知识吧。 机器人英语说法 robot automaton android 机器人的相关短语 智能机器人 intelligent robot ; the robot ; brain bot jr ; IR 水下机器人 ROV ; underwater robot ; Remote Operated Vehicle ; underwater vehicle 机器人战线 Robot Battle Tactics 机器人向导 robots 机器人劫难 Android Apocalypse ; Android ; Paul Ziller 机器人控制技术 Robot Control Technology 机器人的英语例句 1. Most mobile robots are still in the design stage. 大多数移动机器人仍处在设计阶段。 2. The robots have been on trial for the past year. 这些机器人过去一年都在试用当中。 3. These cars are built by robots. 这些汽车是由机器人制造的。 4. In the story human beings were replaced by robots. 在这个故事中,人类被机器人代替了. 5. They built a robot capable of understanding spoken commands. 他们制造了一个能懂口头指令的机器人. 6. Science fiction stories often mention robots that can talk. 机器人用英语怎么说英文翻译及例句(1)全文共2页,当前为第2页。机器人用英语怎么说英文翻译及例句(1)全文共2页,当前为第2页。科幻小说常提到会说话的机器人. 机器人用英语怎么说英文翻译及例句(1)全文共2页,当前为第2页。 机器人用英语怎么说英文翻译及例句(1)全文共2页,当前为第2页。 7. Robots can relieve people of dull and repetitive work. 单调重复的工作,机器人可以代劳. 8. She worked like a robot. 她工作起来如同一个机器人. 9. These robots are capable of shape discrimination. 这些机器人能辨别形状. 10. The robot is a marvel of modern engineering. 机器人是现代工程技术的奇迹. 11. Simon's book provides a succinct outline of artificial intelligence and its application to robotics. 西蒙的书简明扼要地概括了人工智能及其在机器人技术方面的应用。 12. What's coming next are robots that do the actual cutting. 下一步机器人要做的就是亲自主刀做手术了. 13. " Aesop, move right, " Colvin says, and the robot obeys. " 伊索, 向右移动, " 科尔文说.结果, 这架机器人就遵从命令移动. 14. Do you think it is possible for a robot to think for itself? 你认为机器人可能会独立思考吗? 15. Lots of pilots plot to dot the rotten robot. 大批领航员策划给腐烂的机器人打点。 机器人用英语怎么说英文翻译及例句(1)
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值