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

我采用栈的方式,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. D - Constructing Roads

    D - Constructing Roads 思路:并查集板子 #include<cstdio> #include<cstring> #include<iostream& ...

  2. Eclipse设置jdk相关

    2.window->preferences->java->Compiler->设置右侧的Compiler compliance level 3.window->prefe ...

  3. SDUT 1225-编辑距离(串型dp)

    编辑距离 Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述 如果字符串的基本操作仅为:删除一个字符.插入一个字符和将一个字符改动 ...

  4. ORACLE 參数文件介绍

    ORACLE数据库启动以后.通过select * from v$parameter这个语句能够查看到oracle数据库使用的全部參数. 对于oracle的參数文件,分为spfile 二进制文件和pfi ...

  5. servletConfig和ServletContext 以及servletContextListener介绍

    <servlet>     <servlet-name>BeerParamTests</servlet-name>     <servlet-class> ...

  6. Android recovery UI实现分析

    Android recovery模式为何物? 关于这个问题, baidu上已经有无数的答案.不理解的朋友先补习一下. 从纯技术角度来讲, recovery和android本质上是两个独立的rootfs ...

  7. SGU 531 - Bonnie and Clyde 预处理+二分

    Bonnie and Clyde Description Bonnie and Clyde are into robbing banks. This time their target is a to ...

  8. myeclipse打开jsp页面慢或者卡死

    不知道你们有没有这种经历,反正无论是公司电脑还是自己电脑,myeclipse打开jsp页面卡的不行不行的,又是甚至会出现卡死的现象,几经周折,找到了解决办法,亲测有效 打开window-prefere ...

  9. TP5异常处理

    TP5异常处理 标签(空格分隔): php, thinkphp5 自定义异常处理 namespace app\common\exception; use think\Exception; class ...

  10. Solidworks.2016.SP5下载安装破解图文教程

    安装此软件一定要断网安装!!!下载完成后解压文件,打开破解文件夹,双击文件夹中的SolidWorksSerialNumbers2016.reg进行注册表注册,如下图.   解压软件安装包(或者将软件安 ...