HDU 4303 Hourai Jeweled 解题报告(树状DP+统计)
来源:互联网 发布:udp端口号可选范围 编辑:程序博客网 时间:2024/06/05 03:40
Hourai Jeweled
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 163840/163840 K (Java/Others)Total Submission(s): 868 Accepted Submission(s): 315
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?
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.
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
66 2 3 7 1 41 2 11 3 21 4 32 5 12 6 2
Sample Output
134Hintgorgeous roads are :1-2 Value: 81-3 Value: 91-4Value:131-2-6 Value:122-1-3 Value:112-1-4 Value:152-5Value:32-6 Value:63-1-4 Value:163-1-2-6 Value:154-1-2-6 Value:195-2-6Value:7
Author
BUPT
Source
2012 Multi-University Training Contest 1
解题报告:树型DP。
值有两种。一种是以父节点到子节点的路径,一种是子节点通过父节点到另一子节点的值。
分析,易知每个节点应该保存当前节点的可用子路径数量,当前节点及其可用子节点的值得和。
要注意的是,直接枚举儿子是否两两不同色会超时。可以用map(哈希应该也可以)保存某一颜色的值得和,或者先对边的颜色进行排序并统计。
使用map的代码:
#include <cstdio>#include <cstring>#include <algorithm>#include <map>using namespace std;typedef long long LL;const int maxn=600010;int first[maxn/2],vv[maxn],ww[maxn],nxt[maxn];LL dp[maxn/2];int num[maxn/2],node[maxn/2];int n;LL res;int eNum;void addEdge(int a,int b,int w){ nxt[eNum]=first[a],vv[eNum]=b,ww[eNum]=w,first[a]=eNum++; nxt[eNum]=first[b],vv[eNum]=a,ww[eNum]=w,first[b]=eNum++;}void init(){ res=0; eNum=2; memset(first,-1,sizeof(first));}void dfs(int u,int p,int c){ num[u]=1; dp[u]=node[u]; map<int,LL> mpNum,mpDp; LL tmpNum=0,tmpDp=0,temp=0; for(int a=first[u];~a;a=nxt[a]) if(vv[a]!=p) { int v=vv[a]; int col=ww[a]; dfs(v,u,col); temp=dp[v]+(LL)node[u]*num[v]; if(col!=c) { num[u]+=num[v]; dp[u]+=temp; } tmpNum+=num[v]; tmpDp+=temp; mpNum[col]+=num[v]; mpDp[col]+=temp; } res+=tmpDp; temp=0; for(int a=first[u];~a;a=nxt[a]) if(vv[a]!=p) temp+=dp[vv[a]]*(tmpNum-mpNum[ww[a]])+num[vv[a]]*(tmpDp-mpDp[ww[a]]); res+=temp/2;}int main(){ while(~scanf("%d",&n)) { init(); for(int i=1;i<=n;i++) scanf("%d",node+i); for(int i=1;i<n;i++) { int a,b,w; scanf("%d%d%d",&a,&b,&w); addEdge(a,b,w); } dfs(1,0,0); printf("%I64d\n",res); }}
边排序的代码,注意指向父节点的边可能影响到连续相同颜色的统计。
#include <cstdio>#include <cstring>#include <algorithm>#include <map>#include <vector>using namespace std;typedef long long LL;const int maxn=300010;LL dp[maxn];int num[maxn],node[maxn];int n;LL res;struct Edge{int v,c;Edge(int _v,int _c){v=_v;c=_c;}bool operator<(const Edge& cmp) const{return c<cmp.c;}};vector<Edge> edge[maxn];void addEdge(int a,int b,int w){edge[a].push_back(Edge(b,w));edge[b].push_back(Edge(a,w));}void init(){res=0;for(int i=0;i<=n;i++)edge[i].clear();}void dfs(int u,int p,int c){num[u]=1;dp[u]=node[u];if(edge[u].size()==1) return;LL sumDp=0,nowDp=0,nowTemp=0,temp=0;int nowNum=0,sumNum=0;sort(edge[u].begin(),edge[u].end());for(vector<Edge>::iterator it=edge[u].begin();it!=edge[u].end();){if(it->v==p) {it++;continue;}int col=it->c;for(;it!=edge[u].end() && it->c==col;it++) if(it->v!=p){int v=it->v;dfs(v,u,col);temp=dp[v]+(LL)node[u]*num[v];if(col!=c){num[u]+=num[v];dp[u]+=temp;}res+=temp;nowNum+=num[v];nowDp+=dp[v];nowTemp+=temp;}res+=sumNum*nowDp+sumDp*nowNum;sumNum+=nowNum;sumDp+=nowTemp;nowNum=0;nowDp=0;nowTemp=0;}}int main(){while(~scanf("%d",&n)){init();for(int i=1;i<=n;i++)scanf("%d",node+i);for(int i=1;i<n;i++){int a,b,w;scanf("%d%d%d",&a,&b,&w);addEdge(a,b,w);}dfs(1,0,0);printf("%I64d\n",res);}}
- HDU 4303 Hourai Jeweled 解题报告(树状DP+统计)
- hdu 4303 Hourai Jeweled(树型DP+统计答案)
- HDU 4303 Hourai Jeweled bt树形统计
- hdu - 4303 - Hourai Jeweled 树dp
- hdu 4303 Hourai Jeweled 树形DP
- hdu 4303 Hourai Jeweled(树形dp)
- hdu - 4303 - Hourai Jeweled 树dp
- hdu 4303 Hourai Jeweled
- Hdu 4303 Hourai Jeweled
- hdu 4303 Hourai Jeweled(树形DP,5级)
- hdu4303 Hourai Jeweled 树形dp,统计
- HDU 4303 Hourai Jeweled 树形dp 所有路径点权和 dfs2次
- Hourai Jeweled
- MUTC 1 B - Hourai Jeweled 树形dp?
- HDU4303 Hourai Jeweled
- HDU 1251 统计难题(解题报告)
- BZOJ 1264 树状数组+DP 解题报告
- BZOJ 1227 DP+树状数组 解题报告
- .net提取内容中的图片地址
- Adobe CS5 绿色汉化精简版系列套装
- Lucene java 的全文检索引擎
- JSTL函数标签库 fn标签
- [libgdx]项目通过RoboVm编译到ios平台并运行的环境配置
- HDU 4303 Hourai Jeweled 解题报告(树状DP+统计)
- js 实现复选框(checkbox)类似单选钮(radio)点击互斥效果
- Perl 实现远程机器操作
- Java中多线程的同步机制
- s:property标签
- 二叉树的建立及遍历
- linux_shell读文件,然后排序
- ASA 5525X NAT 问题
- uc/os-II源码分析