Hourai Jeweled

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 163840/163840 K (Java/Others)
Total Submission(s): 1149    Accepted Submission(s): 457

Problem Description
Kaguya Houraisan was once a princess of the Lunarians, a race of people living on the Moon. She was exiled to Earth over a thousand years ago for the crime of using the forbidden Hourai Elixir to make herself immortal. Tales of her unearthly beauty led men from all across the land to seek her hand in marriage, but none could successfully complete her trial of the Five Impossible Requests. 

One of these requests is to reckon the value of "Hourai Jeweled (蓬莱の玉の枝)". The only one real treasure Kaguya has, in her possession. As showed in the picture, Hourai Jeweled is a tree-shaped twig. In which, each node is ornamented with a valuable diamond and each edge is painted with a briliant color (only bright man can distinguish the difference). Due to lunarians' eccentric taste, the value of this treasure is calculated as all the gorgeous roads' value it has. The road between two different nodes is said to be gorgeous, if and only if all the adjacent edges in this road has diffenrent color. And the value of this road is the sum of all the nodes' through the road.
Given the value of each node and the color of each edge. Could you tell Kaguya the value of her Hourai Jeweled?
 
Input
The input consists of several test cases. 
The first line of each case contains one integer N (1 <= N <= 300000), which is the number of nodes in Hourai Jeweled.
The second line contains N integers, the i-th of which is Vi (1 <= Vi <= 100000), the value of node i.
Each of the next N-1 lines contains three space-separated integer X, Y and Z (1<=X,Y<=N, 1 <= Z <= 100000), which represent that there is an edge between X and Y painted with colour Z. 
 
Output
For each test case, output a line containing the value of Hourai Jeweled.
 
Sample Input
6
6 2 3 7 1 4
1 2 1
1 3 2
1 4 3
2 5 1
2 6 2
 
Sample Output
134

Hint

gorgeous roads are :
1-2 Value: 8
1-3 Value: 9
1-4 Value:13
1-2-6 Value:12
2-1-3 Value:11
2-1-4 Value:15
2-5 Value:3
2-6 Value:6
3-1-4 Value:16
3-1-2-6 Value:15
4-1-2-6 Value:19
5-2-6 Value:7

 
Author
BUPT
 
Source
 题意:
n个节点的树,每个点有权值,每条边有一种颜色,问所有美丽路径的权值之和,美丽路径是相邻的两条边的颜色不同的路径。
代码:
//做法很好想但实现很难,两条边以上的路径有两种,一种是儿子节点及其后代和父亲连接的路径
//另一种是兄弟之间连接的路径但处理兄弟之间组成的路径不好处理。dp[i]存i节点以及其后代的
//权值和,cntp[i]存i节点以及其后代中共有多少个节点。好难。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
int n,head[],tol,pa[],cntp[],val[],S[];
ll dp[],vc[],vw[],ans;
struct node{
int v,w,next;
}nodes[];
void Add(int x,int y,int z){
nodes[tol].v=y;
nodes[tol].w=z;
nodes[tol].next=head[x];
head[x]=tol++;
}
void dfs(int u,int fa){
cntp[u]=;dp[u]=val[u];
ll sum=;
for(int i=head[u];i!=-;i=nodes[i].next){
int v=nodes[i].v;
if(v==fa) continue;
pa[v]=nodes[i].w;
dfs(v,u);
}
int c=;
for(int i=head[u];i!=-;i=nodes[i].next){
int v=nodes[i].v;
if(v==fa) continue;
cntp[u]+=cntp[v];
if(!vc[nodes[i].w]) S[++c]=nodes[i].w;
vc[nodes[i].w]+=cntp[v];
vw[nodes[i].w]+=dp[v];
if(nodes[i].w!=pa[u])//更新u
dp[u]+=dp[v]+1ll*cntp[v]*val[u];
ans+=dp[v]+1ll*cntp[v]*val[u];//和父亲连接
sum+=dp[v];
ans+=((sum-vw[nodes[i].w])*cntp[v]+dp[v]*(cntp[u]--vc[nodes[i].w])+1ll*val[u]*cntp[v]*(cntp[u]--vc[nodes[i].w]));//兄弟之间连接
}
cntp[u]-=vc[pa[u]];//减去颜色冲突的
while(c){
vc[S[c]]=;
vw[S[c--]]=;
}
}
int main()
{
while(scanf("%d",&n)==){
int x,y,z;
tol=;
memset(head,-,sizeof(head));
for(int i=;i<=n;i++) scanf("%d",&val[i]);
for(int i=;i<n;i++){
scanf("%d%d%d",&x,&y,&z);
Add(x,y,z);
Add(y,x,z);
}
ans=;pa[]=;
dfs(,);
printf("%I64d\n",ans);
}
return ;
}

HDU 4303 树形DP的更多相关文章

  1. hdu 4123 树形DP+RMQ

    http://acm.hdu.edu.cn/showproblem.php? pid=4123 Problem Description Bob wants to hold a race to enco ...

  2. HDU 1520 树形dp裸题

    1.HDU 1520  Anniversary party 2.总结:第一道树形dp,有点纠结 题意:公司聚会,员工与直接上司不能同时来,求最大权值和 #include<iostream> ...

  3. HDU 1561 树形DP入门

    The more, The Better Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  4. HDU 2196树形DP(2个方向)

    HDU 2196 [题目链接]HDU 2196 [题目类型]树形DP(2个方向) &题意: 题意是求树中每个点到所有叶子节点的距离的最大值是多少. &题解: 2次dfs,先把子树的最大 ...

  5. HDU 1520 树形DP入门

    HDU 1520 [题目链接]HDU 1520 [题目类型]树形DP &题意: 某公司要举办一次晚会,但是为了使得晚会的气氛更加活跃,每个参加晚会的人都不希望在晚会中见到他的直接上司,现在已知 ...

  6. codevs 1380/HDU 1520 树形dp

    1380 没有上司的舞会 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 回到问题 题目描述 Description Ural大学有N个职员 ...

  7. HDU 5834 [树形dp]

    /* 题意:n个点组成的树,点和边都有权值,当第一次访问某个点的时候获得利益为点的权值 每次经过一条边,丢失利益为边的权值.问从第i个点出发,获得的利益最大是多少. 输入: 测试样例组数T n n个数 ...

  8. hdu 4267 树形DP

    思路:先dfs一下,找出1,n间的路径长度和价值,回溯时将该路径长度和价值清零.那么对剩下的图就可以直接树形dp求解了. #include<iostream> #include<al ...

  9. hdu 4607 (树形DP)

    当时比赛的时候我们找出来只要求出树的最长的边的节点数ans,如果要访问点的个数n小于ans距离直接就是n-1 如果大于的话就是(n-ans)*2+ans-1,当时求树的直径难倒我们了,都不会树形dp ...

随机推荐

  1. 【转载】appium 操作汇总

    '''.appium api第二弹 锋利的python,这是初稿,2015/1/5 如有错误的地方,请同学们进行留言,我会及时予以修改,尽量整合一份ok的api 作者:Mads Spiral QQ:7 ...

  2. MongoDB3.2 集群搭建

    一.集群的理论知识 1.1 集群成员 MongoDB的集群类似于GreenPlum集群,由一个入口节点负责任务分发与结果统计,分片结节负责执行任务.不同GP,多了一个config servers. 集 ...

  3. 搭建独立域名博客 -- 独立域名博客上线了 www.hanshuliang.com

    博客是安装在阿里云的服务器上. 小结 : -- 进入数据库命令 :mysql -uroot -p123456 ; -- 检查nginx配置语法 :.../nginx/sbin/nginx -t; -- ...

  4. Java中I/O流之轮换流

    Java 中的轮换流: 非常有用,可以把一个字节流转换成字符流. inputStreamReader, outputStreamReader Demo_1: import java.io.*; cla ...

  5. 常用排序算法--java版

    package com.whw.sortPractice; import java.util.Arrays; public class Sort { /** * 遍历一个数组 * @param sor ...

  6. 3ds max启动慢怎么办?

      有时候启动3ds max的时候一直卡在启动界面进不去怎么办?   在百度上搜到了下面这个解决方案,试了下还真有用:   具体就是进到这个文件夹,然后分别进入第一个和第三个文件夹删掉autodesk ...

  7. PAT-2018年冬季考试-乙级

    1091 N-自守数 代码: #include <bits/stdc++.h> using namespace std; int T; int A(int a) { ; while(a) ...

  8. 一个比较典型的WMI查询

    Get-WmiObject win32_bios -ComputerName server1, server2 | Format-Table ` @{n='Hostname';e={$_.__serv ...

  9. Android 布局方式学习

    一.LinearLayout线性布局: 线性布局是程序中最常见的一种布局方式,线性布局可以分为水平线性布局和垂直线性布局两种, 通过android:orientation属性可以设置线性布局的方向 1 ...

  10. Android基础------通知栏

    前言:Android通知栏提示笔记 通知几乎是每一款app都拥有的功能 1.发送通知 发送一个通知栏必须用到两个类:  NotificationManager . Notification. Noti ...