hdu 4303 Hourai Jeweled

http://acm.hdu.edu.cn/showproblem.php?pid=4303

算法有时候不是关键 关键在于思维能力

这个题 一遍DFS就可以 不需要多复杂的算法

但是需要较好的思维能力 才能在一遍DFS的情况下搞定左右情况的值

自己是思维就是不行呀 看了提示才过的

但是 不知道是因为 64位整型 和int型  相乘相加时会出问题 还是题目的一些数据大小的范围出问题啦

纠结了n久 最后把一些int 都改成64位 就过了 是不是其他问题我也不知道

这个题我是没法说思路啦 因为我已经挂了 呜呜呜呜呜~~~~~~~~~

代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<cmath>
#include<stack>
#include<algorithm>
#define LL long long

using namespace std;

const int N=300005;

struct node
{
    int uppath;
    LL num;
    LL upnum;
    struct tt *next;
}mem[N];
struct tt
{
    int j;
    int color;
    struct tt *next;
};
struct aa
{
    int x,y;
    int color;
}edge[N];
int value[N];
bool cmp(aa a,aa b)
{
    return a.color>b.color;
}
void build(int i,int j,int color)
{
    struct tt *t=new tt;
    t->j=j;
    t->color=color;
    t->next=mem[i].next;
    mem[i].next=t;
}
void Dele(int n)
{
    for(int i=1;i<=n;++i)
    mem[i].next=NULL;
}
void Dfs(int x,int pre,int precolor)
{
    struct tt *t=mem[x].next;
    mem[x].num=0;
    mem[x].uppath=1;
    mem[x].upnum=value[x];
    LL ldifsum=0,lsum=0;
    LL updifsum=0,upsum=0;
    LL lnum=0;
    int lcolor=0;
    while(t!=NULL)
    {
        if(t->j==pre)
        {t=t->next;continue;}
        Dfs(t->j,x,t->color);
        if(t->j!=pre&&t->color!=precolor)
        {
            mem[x].uppath+=mem[t->j].uppath;
            mem[x].upnum+=(mem[t->j].upnum+value[x]*mem[t->j].uppath);
        }
        if(t->color!=lcolor)
        {
            ldifsum=lsum;
            updifsum=upsum;
        }
        mem[x].num+=(value[x]*(mem[t->j].uppath*ldifsum)+(mem[t->j].uppath*value[x])+mem[t->j].upnum);
        lnum+=(mem[t->j].uppath*updifsum+mem[t->j].upnum*ldifsum);
        lsum+=mem[t->j].uppath;
        lcolor=t->color;
        upsum+=mem[t->j].upnum;
        t=t->next;
    }
    mem[x].num+=lnum;
}
int main()
{
   //freopen("data.txt","r",stdin);
   int n;
   while(scanf("%d",&n)!=EOF)
   {
       for(int i=1;i<=n;++i)
       {
           scanf("%d",&value[i]);
       }
       for(int i=0;i<n-1;++i)
       {
           scanf("%d %d %d",&edge[i].x,&edge[i].y,&edge[i].color);
       }
       sort(edge,edge+n-1,cmp);
       for(int i=0;i<n-1;++i)
       {
           build(edge[i].x,edge[i].y,edge[i].color);
           build(edge[i].y,edge[i].x,edge[i].color);
       }
       Dfs(1,0,0);
       LL ans=0;
       for(int i=1;i<=n;++i)
       {
           ans+=mem[i].num;
       }
       cout<<ans<<endl;
       Dele(n);
   }
}

  

转载于:https://www.cnblogs.com/liulangye/archive/2012/07/21/2603061.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值