意甲冠军:

long long ans = 0;

for(int i = 1; i <= n; i++)

for(int j = i+1; j <= n; j++)

ans += F(i,j);

F(i,j)表示i点到j点路径上全部的点权和。

若i->j路径上存在2条相邻边边权同样则 F(i,j) = 0

问:ans的值。

int乘法爆掉了我也醉了。

。。

思路:

和网上的统计边方法不同,这里是用统计点出现的次数来计算

我们计算每一个点i 出现的次数,则答案就是 i的次数*i的点权 => dp[i] * a[i]

而i出现的路径起点和终点有4种

1、i的子孙->i的子孙

2、i的子孙->i

3、i到 (非i的子孙( 即i的祖先节点,兄弟节点和兄弟节点的子孙

4、i的子孙->非i的子孙

所以先计算1,2的情况 ,用dp1[i]记录

3,4的情况用dp2[i]记录

则答案就是 for(int i = 1; i <= n; i++) ans += a[i] * (dp1[i]+dp2[i]);

siz[u] 表示以u为根的子树中有效的节点数,若 u -> v(col = 1) && v -> k(col=1), 则以k为根的子树都不是有效节点

(当中v是u的儿子,k是v的儿子)

mp[u][col]表示以u为根。有效节点中 用颜色为col的边相连的节点个数

#include <map>
using namespace std;
#define N 300100
struct Edge{
int to, col, nex;
}edge[N<<1];
int head[N], edgenum;
void init(){memset(head, -1, sizeof head); edgenum = 0;}
void add(int u, int v, int col){
Edge E = {v, col, head[u]};
edge[edgenum] = E;
head[u] = edgenum++;
}
typedef long long ll;
template <class T>
inline bool rd(T &ret) {
char c; int sgn;
if(c=getchar(),c==EOF) return 0;
while(c!='-'&&(c<'0'||c>'9')) c=getchar();
sgn=(c=='-')?-1:1;
ret=(c=='-')?0:(c-'0');
while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
ret*=sgn;
return 1;
}
template <class T>
inline void pt(T x) {
if(x>9) pt(x/10);
putchar(x%10+'0');
}
int n, k;
ll dp1[N], dp2[N], a[N];
int siz[N];
map<int, int> mp[N], mp2[N];//mp[u][col]表示u子树下 边颜色=col 的有效的点数
void dfs1(int u, int fa){
siz[u] = 0; dp1[u] = 0;
for(int i = head[u]; ~i; i = edge[i].nex) {
int v = edge[i].to; if(v == fa)continue;
dfs1(v, u);
mp[u][edge[i].col] += siz[v] - mp[v][edge[i].col];
siz[u] += siz[v] - mp[v][edge[i].col];
}
ll dou = 0;
for(int i = head[u]; ~i; i = edge[i].nex) {
int v = edge[i].to; if(v == fa)continue;
dou += (ll)(siz[v] - mp[v][edge[i].col]) * (ll)(siz[u] - mp[u][edge[i].col]);
dp1[u] += siz[v] - mp[v][edge[i].col];
}
dp1[u] += dou >> 1;
siz[u]++;
}
void dfs2(int u, int ok, int col, int fa) {
dp2[u] = (ll)(siz[u] - mp[u][col]) * (ll)ok;
for(int i = head[u]; ~i; i = edge[i].nex) {
int v = edge[i].to; if(v == fa)continue;
if(u != fa && edge[i].col == col)
dfs2(v, siz[u] - mp[u][edge[i].col], edge[i].col, u);
else
dfs2(v, ok + siz[u] - mp[u][edge[i].col], edge[i].col, u);
}
}
void solve(){
init();
for(int i = 1; i <= n; i++)rd(a[i]), mp[i].clear(), mp2[i].clear();
for(int i = 1, u, v, d; i < n; i++) {
rd(u);rd(v);rd(d);
add(u,v,d); add(v,u,d);
}
dfs1(1, 1);
dfs2(1, 0, -1, 1);
}
int main() {
while(rd(n)){
solve();
ll ans = 0;
for(int i = 1; i <= n; i++)
ans += a[i]*(dp1[i]+dp2[i]);
pt( ans );putchar('\n');
}
return 0;
}
/*
4
1 10 100 1000
1 2 1
2 3 1
3 4 1 5
1 10 100 1000 10000
1 2 1
2 3 1
3 4 1
2 5 2 11
1 2 3 4 5 6 7 8 9 111 123
1 2 1
1 3 2
2 4 3
2 5 1
3 6 3
3 7 3
5 8 1
5 9 2
8 10 1
11 8 2 14
1 2 3 4 5 6 7 8 9 111 123 235 66 1000
1 2 1
1 3 2
2 4 3
2 5 1
3 6 3
3 7 3
5 8 1
5 9 2
8 10 1
11 8 2
12 11 2
8 13 1
8 14 2 10
1 1 1 1 1 1 1 1 1 1
1 2 1
1 7 1
1 10 2
2 3 5
2 6 4
3 4 1
3 5 8
7 8 2
7 9 1 14
1 2 5 10 20 30 70 80 100 1000 2000 5000 100000 1000000
1 2 2
1 3 1
1 4 1
2 5 2
2 8 3
3 9 3
3 6 2
4 7 1
4 10 3
3 11 3
6 12 2
13 1 2
14 3 3 */

版权声明:本文博客原创文章,博客,未经同意,不得转载。

HDU 4303 Hourai Jeweled 树dp 所有权利和航点 dfs2次要的更多相关文章

  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 1011 Starship Troopers (树dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1011 题意: 题目大意是有n个房间组成一棵树,你有m个士兵,从1号房间开始让士兵向相邻的房间出发,每个 ...

  4. HDU 4661 Message Passing ( 树DP + 推公式 )

    参考了: http://www.cnblogs.com/zhsl/archive/2013/08/10/3250755.html http://blog.csdn.net/chaobaimingtia ...

  5. HDU 4085 斯坦纳树+DP

    https://cn.vjudge.net/problem/HDU-4085 给你n,m,k ,分别表示有n个点,m条边,每条边有一个权值,表示修复这条边需要的代价 从前k个点中任取一个使其和后k个点 ...

  6. HDU 4303 树形DP

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

  7. HDU 3016 Man Down (线段树+dp)

    HDU 3016 Man Down (线段树+dp) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja ...

  8. 【HDU 5647】DZY Loves Connecting(树DP)

    pid=5647">[HDU 5647]DZY Loves Connecting(树DP) DZY Loves Connecting Time Limit: 4000/2000 MS ...

  9. HDU 2196 Computer (树dp)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2196 给你n个点,n-1条边,然后给你每条边的权值.输出每个点能对应其他点的最远距离是多少 ...

随机推荐

  1. WEB 3D SVG CAD 向量 几个实施

    一.他们所有的发展.从地上爬起来 VML+SVG发展矢量地图.你并不需要导入第三方的图片作为背景,直接在地图编辑器可以在底图内容编辑,由于岩石.巷道.煤层.画水.础地图样子再在其上面画出智慧线等设备, ...

  2. redis预切片技术,实现

    平时,我们将创建多个redis实例,以缓解单redis压力范例.但,作为高速缓存的数量增加.对redis对于扩展是一种非 - 不要无所谓.对redis有几种方法用于扩张可能.让我们添加每个redis最 ...

  3. [LeetCode145]Binary Tree Postorder Traversal

    题目: Given a list, rotate the list to the right by k places, where k is non-negative. For example:Giv ...

  4. [LeetCode226]Invert Binary Tree

    题目: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 反转二叉树,左右儿子值交换 代码: / ...

  5. OpenCV:Mat元素访问方法、演出、代码的复杂性和安全性分析

    欢迎转载.尊重原创,因此,请注明出处: http://blog.csdn.net/bendanban/article/details/30527785 本文讲述了OpenCV中几种訪问矩阵元素的方法, ...

  6. 玩转Web之Jsp(一)-----jsp中的静态包含(<%@include file="url"%>)与动态包含(<jsp:include>)

    在jsp中include有两种形式,其中<%@include file="url"%>是指令元素,<jsp:include page="" f ...

  7. LeetCode Solutions : Reorder List

    →-→Ln-1→Ln, reorder it to: L→Ln-2→- You must do this in-place without altering the nodes' values. Fo ...

  8. .NET应用架构设计—工作单位模式(摆脱程序代码的重要思想,反击DDD)

    阅读文件夹: 1.背景介绍 2.过程式代码的真正困境 3.工作单元模式的简单演示样例 4.总结 1.背景介绍 一直都在谈论面向对象开发.可是开发企业应用系统时.使用面向对象开发最大的问题就是在于,多个 ...

  9. Shell学问: 调用脚本之间

    于Java,Python于,您可以使用import该方法使脚本或模块之间的呼叫,例如: >>> import math >>> math.sqrt(4) 2.0 在 ...

  10. cocos2D(五岁以下儿童)---- CCNode

    本将主要介绍下CCNode这个类.CCNode是全部节点的基类,当中包含我们经常使用的CCScene(场景).CCLayer(图层).CCSprite(精灵)等.它是一个不可以可视化显示的抽象类,仅仅 ...