说实话,挺复杂的一道题。

我采用栈的方式,DFS在搜索完一个节点的所有子结点后,通过排序,加快计算该结点所有可能的路径:子结点与子结点的连通,子结点与父结点的连通,通过父结点与各祖先结点的连通。同时记录路径数计算。思路清晰就能写出来了。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define LL __int64
const int N=300010;
using namespace std; struct e{
int u,v;
int col;
int next;
}edge[N*2];
int head[N];
int tot;
int val[N];
struct p{
LL valsum;
LL route;
int col;
}DFST[N],pre,aft;
LL tmp; void addedge(int u,int v,int col){
edge[tot].u=u;
edge[tot].v=v;
edge[tot].col=col;
edge[tot].next=head[u];
head[u]=tot++;
edge[tot].u=v;
edge[tot].v=u;
edge[tot].col=col;
edge[tot].next=head[v];
head[v]=tot++;
} bool cmp(p a, p b){
if(a.col<b.col) return true;
return false;
} void dfs(LL &ans,int parent,int now,int parent_col,LL &route,int pos){
int k=-1; LL son_val,son_route;
for(int ei=head[now];ei!=-1;ei=edge[ei].next){
if(edge[ei].v==parent) continue;
k++; son_val=son_route=0;
dfs(son_val,now,edge[ei].v,edge[ei].col,son_route,pos+k);
DFST[pos+k].valsum=son_val; DFST[pos+k].route=son_route;
DFST[pos+k].col=edge[ei].col;
}
if(k>=0){
sort(DFST+pos,DFST+pos+k+1,cmp);
for(int i=0;i<=k;i++){
if(parent!=-1)
tmp+=((LL)DFST[pos+i].valsum+(LL)val[now]*DFST[pos+i].route);
if(DFST[pos+i].col!=parent_col){
ans+=((LL)DFST[pos+i].valsum+(LL)val[now]*DFST[pos+i].route);
route+=(LL)DFST[pos+i].route;
}
}
if(parent!=-1){
ans+=val[now];
route++;
}
if(DFST[pos+k].col!=DFST[pos].col){
pre=DFST[pos];
int c=DFST[pos].col;
for(int i=1;i<=k;i++){
if(DFST[pos+i].col==c){
pre.valsum=pre.valsum+DFST[pos+i].valsum;
pre.route=pre.route+DFST[pos+i].route;
}
else{
aft=DFST[pos+i];
int si=i+1;
while(aft.col==DFST[pos+si].col&&si<=k){
aft.valsum+=DFST[pos+si].valsum;
aft.route+=DFST[pos+si].route;
si++;
}
i=si-1;
tmp+=(pre.route*aft.valsum+aft.route*pre.valsum+(pre.route*aft.route)*val[now]);
pre.route+=aft.route;
pre.valsum+=aft.valsum;
c=aft.col;
}
}
}
}
else{
ans=val[now];
route=1;
}
} int main(){
int n,u,v,c;
while(scanf("%d",&n)!=EOF){
tmp=0;
for(int i=1;i<=n;i++)
scanf("%d",&val[i]);
memset(head,-1,sizeof(head));
tot=0;
for(int i=1;i<n;i++){
scanf("%d%d%d",&u,&v,&c);
addedge(u,v,c);
}
LL ans=0,route=0;
dfs(ans,-1,1,-1,route,0);//sum,parent,nownode,parent_col,route,beginpos
printf("%I64d\n",ans+tmp);
}
return 0;
}

  

HDU 4303 Contest 1的更多相关文章

  1. HDU 4303 Hourai Jeweled 解题报告

    HDU 4303 Hourai Jeweled 解题报告 评测地址: http://acm.hdu.edu.cn/showproblem.php?pid=4303 评测地址: https://xoj. ...

  2. HDU 4303 Hourai Jeweled(树形DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=4303 题意:给出一棵树,树上的每一个节点都有一个权值,每条边有一个颜色,如果一条路径上相邻边的颜色都是不同的,那 ...

  3. HDU 5045 Contest(状压DP)

    Problem Description In the ACM International Collegiate Programming Contest, each team consist of th ...

  4. hdu - 5045 - Contest(国家压缩dp)

    意甲冠军:N个人M通过主打歌有自己的期望,每个问题发送人玩.它不能超过随机播放的次数1,追求最大业绩预期 (1 ≤ N ≤ 10,1 ≤ M ≤ 1000). 主题链接:pid=5045" ...

  5. [ACM] hdu 5045 Contest (减少国家Dp)

    Contest Problem Description In the ACM International Collegiate Programming Contest, each team consi ...

  6. HDU 4303 树形DP

    Hourai Jeweled Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 163840/163840 K (Java/Others) ...

  7. HDU–5988-Coding Contest(最小费用最大流变形)

    Coding Contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  8. hdu 5045 Contest(状态压缩DP)

    题解:我们使用一个二位数组dp[i][j]记录进行到第i个任务时,人组合为j时的最大和(这里的j我们用二进制的每位相应一个人). 详细见代码: #include <iostream> #i ...

  9. HDU 4303 Hourai Jeweled 树dp 所有权利和航点 dfs2次要

    意甲冠军: long long ans = 0; for(int i = 1; i <= n; i++) for(int j = i+1; j <= n; j++) ans += F(i, ...

随机推荐

  1. codevs——T1048 石子归并

     http://codevs.cn/problem/1048/  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解       题目描述 Descriptio ...

  2. java中继承关系学习小结

    继承:把多个类中同样的内容提取出来.定义到一个类中,其它类仅仅须要继承该类.就能够使用该类公开的属性和公开的方法.   继承的优点:提高代码的复用性.提高代码的可维护性.让类与类之间产生关系,是多态存 ...

  3. Linux Kernel(Android) 加密算法总结(一)(cipher、compress、digest)

    1. Linux内核支持哪些加密算法 ? 内核支持的加密算法非常多,包含: 对称加密算法.如AES,3DES. 对称password体制的发展趋势将以分组password为重点. 分组password ...

  4. jQuery验证所有输入合法后才干提交

    大学三年里所有在专注后台编码.学会不知多少种,servlet.ssh,springMVC,web.py...... 最后每次碰到前端自己要写点东西就满目愁抑, 干脆自己好好理解一段前端代码, 特地拿出 ...

  5. 石子合并(区间dp)

    石子合并(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描写叙述     有N堆石子排成一排,每堆石子有一定的数量.现要将N堆石子并成为一堆.合并的过程仅仅能每次将相邻 ...

  6. LinkedList 方法知识点

    package test_day_9; import java.util.Iterator; import java.util.LinkedList; public class LinkedListD ...

  7. ftk学习记(首篇)

    [ 声明:版权全部,欢迎转载,请勿用于商业用途.  联系信箱:feixiaoxing @163.com] 非常早之前就知道ftk了,当时主要是由于买了李先静的书,所以知道了这么一个项目.由于对这样的g ...

  8. poj--3678--Katu Puzzle(2-sat 建模)

    Katu Puzzle Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit S ...

  9. 【转】Docker基础

    一.简介 Docker是一个开源的应用容器引擎,使用Go语言开发,基于Linux内核的CGroup.Namespace.Union FS等技术实现的一种系统级虚拟化技术. 特性 更高效的利用系统资源: ...

  10. docker初安装的血泪史

    最近docker很火,不管是朋友圈内还是公司内聊天都离不开docker,于是对docker产生了极大的好奇心,凭着一颗程序猿的好奇心开始了docker的安装血泪史. 首先我有一台从公司退役的本本x22 ...