D. The Fair Nut and the Best Path 树形dp (终于会了)

Posted andromeda-galaxy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了D. The Fair Nut and the Best Path 树形dp (终于会了)相关的知识,希望对你有一定的参考价值。

技术分享图片
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=3e5+10;
int a[maxn];
int dp[maxn];
int visit[maxn];
vector<int> vs[maxn];
map<pair<int,int>,int> mp;
int ans=0;
void dfs(int x)
{
    visit[x]=1;
    int max1=0;
    int max2=0;
    for(int i=0;i<vs[x].size();i++)
    {

        int p=vs[x][i];
        if(visit[p]) continue;//cout<<p<<endl;
        dfs(p);
        int k=dp[p]-mp[{x,p}];
        if(k>=max1) max2=max1,max1=k;
        else if(k>=max2) max2=k;
    }
    //cout<<x<<"  "<<dp[x]<<endl;
    dp[x]=a[x]+max1;
    ans=max(ans,dp[x]);
    ans=max(ans,max1+max2+a[x]);
}
int32_t main()
{
    int n; cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=n;i++) dp[i]=a[i];
    for(int i=1;i<n;i++)
    {
        int x,y,z; cin>>x>>y>>z;
        vs[x].push_back(y); mp[{x,y}]=z;
        vs[y].push_back(x); mp[{y,x}]=z;
    }
    dfs(1);
    cout<<ans<<endl;
    return 0;
}
2994ms
技术分享图片
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=3e5+10;
int a[maxn];
int dp[maxn];
int visit[maxn];
vector<pair<int,int> > vs[maxn];
int ans=0;
void dfs(int x)
{
    visit[x]=1;
    int max1=0;
    int max2=0;
    for(int i=0;i<vs[x].size();i++)
    {

        int p=vs[x][i].first;
        if(visit[p]) continue;//cout<<p<<endl;
        dfs(p);
        int k=dp[p]-vs[x][i].second;
        if(k>=max1) max2=max1,max1=k;
        else if(k>=max2) max2=k;
    }
    //cout<<x<<"  "<<dp[x]<<endl;
    dp[x]=a[x]+max1;
    ans=max(ans,dp[x]);
    ans=max(ans,max1+max2+a[x]);
}
int32_t main()
{
    int n; cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=n;i++) dp[i]=a[i];
    for(int i=1;i<n;i++)
    {
        int x,y,z; cin>>x>>y>>z;
        vs[x].push_back({y,z}); //mp[{x,y}]=z;
        vs[y].push_back({x,z}); //mp[{y,x}]=z;
    }
    dfs(1);
    cout<<ans<<endl;
    return 0;
}
2308ms
技术分享图片
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=3e5+10;
int a[maxn];
int dp[maxn];
int visit[maxn];
vector<pair<int,int> > vs[maxn];
int ans=0;
void dfs(int x)
{
    visit[x]=1;
    int max1=0;
    int max2=0;
    for(int i=0;i<vs[x].size();i++)
    {

        int p=vs[x][i].first;
        if(visit[p]) continue;//cout<<p<<endl;
        dfs(p);
        int k=dp[p]-vs[x][i].second;
        if(k>=max1) max2=max1,max1=k;
        else if(k>=max2) max2=k;
    }
    //cout<<x<<"  "<<dp[x]<<endl;
    dp[x]=a[x]+max1;
    ans=max(ans,dp[x]);
    ans=max(ans,max1+max2+a[x]);
}
int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n; cin>>n;
    for(int i=1;i<=n;i++) cin>>a[i];
    for(int i=1;i<=n;i++) dp[i]=a[i];
    for(int i=1;i<n;i++)
    {
        int x,y,z; cin>>x>>y>>z;
        vs[x].push_back({y,z}); //mp[{x,y}]=z;
        vs[y].push_back({x,z}); //mp[{y,x}]=z;
    }
    dfs(1);
    cout<<ans<<endl;
    return 0;
}
700+ms
技术分享图片
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int maxn=3e5+10;
int a[maxn];
int dp[maxn];
int visit[maxn];
vector<pair<int,int> > vs[maxn];
int ans=0;
void dfs(int x)
{
    visit[x]=1;
    int max1=0;
    int max2=0;
    for(int i=0;i<vs[x].size();i++)
    {

        int p=vs[x][i].first;
        if(visit[p]) continue;//cout<<p<<endl;
        dfs(p);
        int k=dp[p]-vs[x][i].second;
        if(k>=max1) max2=max1,max1=k;
        else if(k>=max2) max2=k;
    }
    //cout<<x<<"  "<<dp[x]<<endl;
    dp[x]=a[x]+max1;
    ans=max(ans,dp[x]);
    ans=max(ans,max1+max2+a[x]);
}
int32_t main()
{
    /*ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);*/
    int n; scanf("%I64d",&n);
    for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);
    for(int i=1;i<=n;i++) dp[i]=a[i];
    for(int i=1;i<n;i++)
    {
        int x,y,z; scanf("%I64d %I64d %I64d",&x,&y,&z);
        vs[x].push_back({y,z}); //mp[{x,y}]=z;
        vs[y].push_back({x,z}); //mp[{y,x}]=z;
    }
    dfs(1);
    printf("%I64d
",ans);
    return 0;
}
405ms

 

以上是关于D. The Fair Nut and the Best Path 树形dp (终于会了)的主要内容,如果未能解决你的问题,请参考以下文章

D. The Fair Nut and the Best Path 树形dp (终于会了)

Codeforces Round #526 (Div. 2) D. The Fair Nut and the Best Path

[CF1083B]The Fair Nut and Strings

Codeforces Round #526 C - The Fair Nut and String /// 组合递推

CodeForces 1084A The Fair Nut and Elevator 题解

Codeforces Round #526 (Div. 2) C. The Fair Nut and String