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. Merkle Patricia Tree (MPT) 以太坊中的默克尔树

    本篇博文是自己学习mpt的过程,边学边记录,很多原理性内容非自己原创,好的博文将会以链接形式进行共享. 一.什么是mpt MPT是以太坊中的merkle改进树,基于基数树,即前缀树改进而来,大大提高了 ...

  2. eos开发指南

    十分钟教你开发EOS智能合约 在CSDN.柏链道捷(PDJ Education).HelloEOS.中关村区块链产业联盟主办的「EOS入门及最新技术解读」专场沙龙上,柏链道捷(PDJ Educatio ...

  3. 5.azkaban权限管理

    权限简介 user 登录azkaban的用户 注意,如果不给用户roles groups,则用户就是普通用户,只能创建\查看\执行\调度自己的任务,不能看别人的 group group:用户的集合,给 ...

  4. 2. socket结构体——表示socket地址

    一.两种通用socket结构体 1. sockaddr struct sockaddr { sa_family_t sa_family; // 地址族 char sa_data[14]; // 存放s ...

  5. LintCode-174.删除链表中倒数第n个节点

    删除链表中倒数第n个节点 给定一个链表,删除链表中倒数第n个节点,返回链表的头节点. 注意事项 链表中的节点个数大于等于n 样例 给出链表 1->2->3->4->5-> ...

  6. OSG学习:转动的小汽车示例

    由于只是简单的示例,所以小汽车的模型也比较简单,是由简单的几何体组成. 代码如下: #include <osg\ShapeDrawable> #include <osg\Animat ...

  7. 如何将PDF的背景色设置为保护眼睛的苹果绿色

      福昕阅读器请戳这里.   Adobe Acrobat请戳这里.

  8. 可用于jquery animate()方法的css属性

    * backgroundPosition * borderWidth * borderBottomWidth * borderLeftWidth * borderRightWidth * border ...

  9. 修改IntelliJ IDEA代码头注释

  10. python 查看趴下来的数据

    #coding=utf-8 import re from lxml import etree import requests def requests_view(response): import w ...