码迷,mamicode.com
首页 > 其他好文 > 详细

noip2014T2树形dp/分拆优化

时间:2015-10-27 21:55:55      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

我的方法还是很笨的吧,虽然想到是树形dp但是一直推不出来,原因应该是没有认真分析出他要怎么转移过来。下次要清醒的模拟一下应该不难。

 

3728 联合权值

 

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 黄金 Gold
 
 

 

 

 

题目描述 Description

技术分享

输入描述 Input Description

技术分享

输出描述 Output Description

技术分享

样例输入 Sample Input

技术分享

样例输出 Sample Output

技术分享

技术分享

 

-----------------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
#define ll long long
const int nmax=200001;
vector<int>f[nmax];
ll w[nmax];
ll read(){
  ll x=0;
  char c=getchar();
  while(!isdigit(c)) c=getchar();
  while(isdigit(c)){
    x=x*10+c-‘0‘;
    c=getchar();
  }
  return x;
}
int main(){
   int n=read();
   for(int i=1;i<n;i++){
     int u=read(),v=read();
     f[u].push_back(v);
     f[v].push_back(u);
   }
   for(int i=1;i<=n;i++){
     w[i]=read();
   }
   ll ans1=-1;
   for(int i=1;i<=n;i++){
      ll Max=0;
      for(int j=0;j<f[i].size();j++){
          ans1=max(ans1,w[f[i][j]]*Max);
          Max=max(w[f[i][j]],Max);
      }
   }
   ll ans=0;
   for(int i=1;i<=n;i++){
      ll tmp=0;
      for(int j=0;j<f[i].size()-1;j++){
         tmp+=w[f[i][j]];
         ans+=tmp*w[f[i][j+1]];
      }
   }
   printf("%lld %lld\n",ans1,ans*2%10007);
   return 0;
}

-----------------------------------------------------------------------------------

下面是师兄的做法:

-----------------------------------------------------------------------------------

void dfs(int x, int fa = -1) {
    mx[x] = cnt[x] = 0;
    for(edge* e = head[x]; e; e = e->next) if(e->to != fa) {
        dfs(e->to, x);
        ans = max(ans, w[x] * mx[e->to]);
        ans = max(ans, w[e->to] * mx[x]);
        tot = (tot + w[x] * cnt[e->to] + w[e->to] * cnt[x]) % MOD;
        (cnt[x] += w[e->to]) %= MOD;
        mx[x] = max(mx[x], w[e->to]);
    }
}

-----------------------------------------------------------------------------------

noip2014T2树形dp/分拆优化

标签:

原文地址:http://www.cnblogs.com/fighting-to-the-end/p/4915387.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!