HDU 4303 樹形DP

Hourai Jeweled

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 163840/163840 K (Java/Others)
Total Submission(s): 1149    Accepted Submission(s): 457


php

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[300009],tol,pa[300009],cntp[300009],val[300009],S[300009];
ll dp[300009],vc[300009],vw[300009],ans;
struct node{
    int v,w,next;
}nodes[600009];
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]=1;dp[u]=val[u];
    ll sum=0;
    for(int i=head[u];i!=-1;i=nodes[i].next){
        int v=nodes[i].v;
        if(v==fa) continue;
        pa[v]=nodes[i].w;
        dfs(v,u);
    }
    int c=0;
    for(int i=head[u];i!=-1;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]-1-vc[nodes[i].w])+1ll*val[u]*cntp[v]*(cntp[u]-1-vc[nodes[i].w]));//兄弟之間鏈接
    }
    cntp[u]-=vc[pa[u]];//減去顏色衝突的
    while(c){
        vc[S[c]]=0;
        vw[S[c--]]=0;
    }
}
int main()
{
    while(scanf("%d",&n)==1){
        int x,y,z;
        tol=0;
        memset(head,-1,sizeof(head));
        for(int i=1;i<=n;i++) scanf("%d",&val[i]);
        for(int i=1;i<n;i++){
            scanf("%d%d%d",&x,&y,&z);
            Add(x,y,z);
            Add(y,x,z);
        }
        ans=0;pa[1]=0;
        dfs(1,0);
        printf("%I64d\n",ans);
    }
    return 0;
}
相關文章
相關標籤/搜索