题解:P10070 [CCO 2023] Travelling Trader

· · 题解

Solution

首先发现如果 k 过大一定会出现全部访问的情况。

然后发现 k\le 3,情况很少,直接对 k 数据分治。

K=1

这个是容易的因为你每到一个城市就必须交易,故答案就是从 1 开始的最长链。

K=3

首先说明这个情况一定存在一种行走方式使得整个树都被遍历一遍。

我们先来考虑 k=2 为什么没有办法遍历整个树。

以这个东西为例,假设权值全部相同,那么最优的方案是:

1\to 15\to 17\to 16\to 2\to 3\to 14\to 13\to 4\to 10\to 7\to 6\to 8\to 9\to 11\to 12

我们发现 5 是遍历不到的。

那么理论分析一下:

我们发现我们如果要从一棵子树进入另外一棵子树有且仅有两种方式,一种是从这个子树的根,一种是从这个子树的 fa,并且这个 fa 是共有的,也就是说 fa 只能用一次,那么用了一次就不能再用了。

但是如果使用第一种,那么就会出现我们同时使用了两个子树的根,那么另外一棵子树转移是就只能用 fa 了,这就限制了我们可以访问的子树数量。

放一张图,绿色代表第二种跳子树的方式,即跳到 fa,棕色代表第一种方式。

这里建议还是手玩一下,情况较少,也可以更好理解后文提到的转移。

然后我们观察 k=3 我们发现经过我们刚才的分析,也就是说我们在子树内部多了一个落脚点,那么简单的,我们把一个当作入点,一个当作出点就不难实现将整棵树遍历。

所以这种情况我们依据深度奇偶分层就可以实现构造方案。

K=2

所以这个才是这道题的难点。

超级大芬警告

由于我们需要知道一个子树选不选,一个子树怎么选,在这里,数据结构显然是难以维护的,分治之类的算法也不好做,因为你需要知道你是怎么进入这个分治中心的。

所以这道题我们考虑树形 DP。

众所周知,树形 DP 最困难的地方就在于状态设计抽象和转移的细节多,这道题也不例外。

我们开始对我们刚才所说的为啥不能全选的理论进行状态的设计,我们状态的设计应该直接指向这道题的难点,你如何进入这棵树的,也就是说你的落脚点是哪里。

考虑一下 4 种状态:

然后考虑转移:

f_{u,0}=val_u+\max_{v_1\in son_u} f_{v_1,1}

这个是显然的,因为 f_{v_1,1} 代表由 u 进入子树所可以得到的最大利润,直接累计 u 的权值即可。

f_{u,0}=val_u+\max_{v_1,v_2\in son_u,v_1\ne v_2} f_{v_1,0}+f_{v_2,3}+\sum_{v\in son_u,v\ne v_1,v\ne v_2} val_v

这个式子的意思是,一个子树由 fa 开始,回到它的根,那么由此它可以转移到另外一个子树,并且,我们访问其他子树的起始位置不会改变我们后面的贡献。

f_{u,1}=val_u+\max_{v_1,v_2,v_3\in son_u,v_1\ne v_2\ne v_3} f_{v_1,3}+f_{v_2,2}+f_{v_3,0}+\sum_{v\in son_u,v\notin {v_1,v_2,v_3}} val_v

这里的意思是,由 fa 进入,那么我们此时最多进入 3 个子树,一个从根开始回到 u 然后一个从 u 开始回到根,最后一个直接由根开始不用回来了,剩余子树的根要累计一下贡献。

f_{u,1}=f_{u,0}

从 fa 开始咋进入这棵子树都可以。

f_{u,1}=val_u+\max_{v_1,v_2\in son_u,v_1\ne v_2} f_{v_1,2}+f_{v_2,1}+\sum_{v\in son_u,v\ne v_1,v\ne v_2} val_v

先从 fa 开始进入一棵子树回到 u,重新进入另外一棵子树并且不回来了。

f_{u,2}=val_u+\max_{v_1\in son_u} f_{v_1,3}+\sum_{v\in son_u,v\ne v_1} val_v

因为要回到 fa 所以 u 就只能从 u 开始然后子树中从 u 开始回到 v_1 同时遍历其他的子树根不会影响答案,但是不能再向下走,否则就回不到 fa。

f_{u,3}=val_u+\max_{v_1\in son_u} f_{v_1,2}+\sum_{v\in son_u,v\ne v_1} val_v

注意到根据 DP 的无后效性,我们这里一切关于 fa 的转移均在转移 fa 时累计贡献。

就是说,对于上文中我们有一些状态,要求要选 fa,我们在转移 fa 时再累计贡献。

在这里有一个小细节在上文的转移中没有给出,对于在转移儿子的 f 时,我们认为它们的权值为 f-val,即需要减去其本身的价值,否则他就会影响后面的求和取值。

人话翻译就是,我们需要把后面的求和变为一个定值 tot,即点 u 及其距离不超过 1 的儿子的权值和,这样以后我们才可以直接找到最优决策点。

最后显然的是对于一个点 u 我们需要对于其距离不超过 1 的儿子每一个状态,至少需要前 3 优的决策点,供我们转移,当然没有 3 个就算了。

然后就是输出方案了。

直接从根开始向最优决策点转移即可,但是需要深度理解转移的意义,否则就会因为进入子树的顺序导致方案错误。

Code

#include<bits/stdc++.h>
//吹き込んだそよ風が
//窓辺の花を揺らして
//仰ぐ今日の空は
//あの時描いた青だった
namespace fast_IO {
#define IOSIZE 1000000
    char ibuf[IOSIZE], obuf[IOSIZE], *p1 = ibuf, *p2 = ibuf, *p3 = obuf;
#define getchar() ((p1==p2)and(p2=(p1=ibuf)+fread(ibuf,1,IOSIZE,stdin),p1==p2)?(EOF):(*p1++))
#define putchar(x) ((p3==obuf+IOSIZE)&&(fwrite(obuf,p3-obuf,1,stdout),p3=obuf),*p3++=x)
#define isdigit(ch) (ch>47&&ch<58)
#define isspace(ch) (ch<33)
    template<typename T> inline T read() { T s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s * w; }
    template<typename T> inline bool read(T &s) { s = 0; int w = 1; char ch; while (ch = getchar(), !isdigit(ch) and (ch != EOF)) if (ch == '-') w = -1; if (ch == EOF) return false; while (isdigit(ch)) s = s * 10 + ch - 48, ch = getchar(); return s *= w, true; }
    template<typename T> inline void print(T x) { if (x < 0) putchar('-'), x = -x; if (x > 9) print(x / 10); putchar(x % 10 + 48); }
    inline bool read(char &s) { while (s = getchar(), isspace(s)); return true; }
    inline bool read(char *s) { char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) *s++ = ch, ch = getchar(); *s = '\000'; return true; }
    inline void print(char x) { putchar(x); }
    inline void print(char *x) { while (*x) putchar(*x++); }
    inline void print(const char *x) { for (int i = 0; x[i]; i++) putchar(x[i]); }
    inline bool read(std::string& s) { s = ""; char ch; while (ch = getchar(), isspace(ch)); if (ch == EOF) return false; while (!isspace(ch)) s += ch, ch = getchar(); return true; }
    inline void print(std::string x) { for (int i = 0, n = x.size(); i < n; i++) putchar(x[i]); }
    inline bool read(bool &b) { char ch; while(ch=getchar(), isspace(ch)); b=ch^48; return true; }
    inline void print(bool b) { putchar(b+48); }
    template<typename T, typename... T1> inline int read(T& a, T1&... other) { return read(a) + read(other...); }
    template<typename T, typename... T1> inline void print(T a, T1... other) { print(a), print(other...); }
    struct Fast_IO { ~Fast_IO() { fwrite(obuf, p3 - obuf, 1, stdout); } } io;
    template<typename T> Fast_IO& operator >> (Fast_IO &io, T &b) { return read(b), io; }
    template<typename T> Fast_IO& operator << (Fast_IO &io, T b) { return print(b), io; }
#define cout io
#define cin io
#define endl '\n'
} using namespace fast_IO;
using namespace std;
#define int long long 
//NO all in 
//NEVER get mad
//pay attention
//Clear all when start testing a new data

const int N=1e6+15;

int h[N],to[N],ne[N],idx=0;
int val[N];
int n,k;

namespace solve1
{
    int f[N];
    int path[N];

    void dfs(int u,int fa)
    {
        f[u]=val[u];
        int maxx=0,id=0;
        for(int i=h[u];i!=-1;i=ne[i])
        {
            int v=to[i];
            if(v==fa) continue;
            dfs(v,u);
            if(f[v]>maxx) maxx=f[v],id=v;
        }
        f[u]+=maxx;
        path[u]=id;
    }

    void solve()
    {
        dfs(1,0);
        cout<<f[1]<<endl;
        int now=1,step=0;
        while(now)
        {
            step++;
            now=path[now];
        }
        cout<<step<<endl;
        now=1;
        while(now)
        {
            cout<<now<<" ";
            now=path[now];
        }
        cout<<endl;
        return;
    }
};

namespace solve2
{
    struct node
    {
        int loc[3];
        int maxx[3];
        void cmp(int x,int pos)
        {
            if(x>=maxx[0])
            {
                maxx[2]=maxx[1],loc[2]=loc[1];
                maxx[1]=maxx[0],loc[1]=loc[0];
                maxx[0]=x,loc[0]=pos;
            }
            else if(x>=maxx[1])
            {
                maxx[2]=maxx[1],loc[2]=loc[1];
                maxx[1]=x,loc[1]=pos;
            }
            else if(x>=maxx[2])
            {
                maxx[2]=x,loc[2]=pos;
            }
        }
    }g[N][5];
    int tot[N];
    int f[N][4];
    vector<int> path;

    void dfs(int u,int fa)
    {
        tot[u]=val[u];
        for(int i=h[u];i!=-1;i=ne[i])
        {
            int v=to[i];
            if(v==fa) continue;
            dfs(v,u);
            tot[u]+=val[v];
            for(int i=0;i<=3;i++)
                g[u][i].cmp(f[v][i]-val[v],v);
            g[u][4].cmp(f[v][1],v);
        }
        f[u][0]=val[u]+g[u][4].maxx[0];
        for(int i=0;i<=2;i++)
            for(int j=0;j<=2;j++)
                if((!g[u][0].loc[i])||(!g[u][3].loc[j])||g[u][0].loc[i]!=g[u][3].loc[j])
                {
                    int res1=g[u][0].maxx[i]+g[u][3].maxx[j];
                    int res2=tot[u];
                    f[u][0]=max(f[u][0],res1+res2);
                }
        f[u][1]=f[u][0];
        for(int i=0;i<=2;i++)
            for(int j=0;j<=2;j++)
                if((!g[u][1].loc[i])||(!g[u][2].loc[j])||(g[u][1].loc[i]!=g[u][2].loc[j]))
                {
                    int res1=g[u][1].maxx[i]+g[u][2].maxx[j];
                    int res2=tot[u];
                    f[u][1]=max(f[u][1],res1+res2);
                }
        for(int i=0;i<=2;i++)
            for(int j=0;j<=2;j++)
                for(int k=0;k<=2;k++)
                {
                    if((!g[u][3].loc[i]&&!g[u][0].loc[k])||(!g[u][0].loc[k]&&!g[u][2].loc[j])||(!g[u][3].loc[i]&&!g[u][2].loc[j])||(g[u][3].loc[i]!=g[u][2].loc[j]&&g[u][2].loc[j]!=g[u][0].loc[k]&&g[u][3].loc[i]!=g[u][0].loc[k]))
                    {
                        // cout<<g[u][3].loc[i]<<" "<<g[u][2].loc[j]<<" "<<g[u][0].loc[k]<<endl;
                        int res1=g[u][3].maxx[i]+g[u][2].maxx[j]+g[u][0].maxx[k];
                        int res2=tot[u];
                        f[u][1]=max(f[u][1],res1+res2);
                    }
                }
        f[u][2]=g[u][3].maxx[0]+tot[u];
        f[u][3]=g[u][2].maxx[0]+tot[u];
    }

    void get_path(int u,int fa,int opt)
    {
        if(!u) return;
        if(opt==0)
        {
            if(f[u][0]==val[u]+g[u][4].maxx[0])
            {
                path.push_back(u);
                get_path(g[u][4].loc[0],u,1);
            }
            else 
            {
                for(int i=0;i<=2;i++)
                    for(int j=0;j<=2;j++)
                        if((!g[u][0].loc[i])||(!g[u][3].loc[j])||g[u][0].loc[i]!=g[u][3].loc[j])
                        {
                            int res1=g[u][0].maxx[i]+g[u][3].maxx[j];
                            int res2=tot[u];
                            if(f[u][0]==res1+res2)
                            {
                                path.push_back(u);
                                get_path(g[u][3].loc[j],u,3);
                                for(int k=h[u];k!=-1;k=ne[k])
                                {
                                    int v=to[k];
                                    if(v==fa||v==g[u][3].loc[j]||v==g[u][0].loc[i])
                                        continue;
                                    path.push_back(v);
                                }
                                get_path(g[u][0].loc[i],u,0);
                                return;
                            }
                        }
            }
        }
        if(opt==1)
        {
            if(f[u][0]==f[u][1])
                get_path(u,fa,0);
            else
            {
                for(int i=0;i<=2;i++)
                    for(int j=0;j<=2;j++)
                        if((!g[u][1].loc[i])||(!g[u][2].loc[j])||(g[u][1].loc[i]!=g[u][2].loc[j]))
                        {
                            int res1=g[u][1].maxx[i]+g[u][2].maxx[j];
                            int res2=tot[u];
                            if(f[u][1]==res1+res2)
                            {
                                for(int k=h[u];k!=-1;k=ne[k])
                                {
                                    int v=to[k];
                                    if(v==fa||v==g[u][2].loc[j]||v==g[u][1].loc[i])
                                        continue;
                                    path.push_back(v);
                                }
                                get_path(g[u][2].loc[j],u,2);
                                path.push_back(u);
                                get_path(g[u][1].loc[i],u,1);
                                return;
                            }
                        }
                for(int i=0;i<=2;i++)
                    for(int j=0;j<=2;j++)
                        for(int k=0;k<=2;k++)
                        {
                            if((!g[u][3].loc[i]&&!g[u][0].loc[k])||(!g[u][0].loc[k]&&!g[u][2].loc[j])||(!g[u][3].loc[i]&&!g[u][2].loc[j])||(g[u][3].loc[i]!=g[u][2].loc[j]&&g[u][2].loc[j]!=g[u][0].loc[k]&&g[u][3].loc[i]!=g[u][0].loc[k]))
                            {
                                int res1=g[u][3].maxx[i]+g[u][2].maxx[j]+g[u][0].maxx[k];
                                int res2=tot[u];
                                if(f[u][1]==res1+res2)
                                {
                                    for(int l=h[u];l!=-1;l=ne[l])
                                    {
                                        int v=to[l];
                                        if(v==fa||v==g[u][0].loc[k]||v==g[u][2].loc[j]||v==g[u][3].loc[i])
                                            continue;
                                        path.push_back(v);
                                    }
                                    get_path(g[u][2].loc[j],u,2);
                                    path.push_back(u);
                                    get_path(g[u][3].loc[i],u,3);
                                    get_path(g[u][0].loc[k],u,0);
                                    return;
                                }
                            }
                        }
            }
        }
        if(opt==2)
        {
            path.push_back(u);
            get_path(g[u][3].loc[0],u,3);
            for(int i=h[u];i!=-1;i=ne[i])
            {
                int v=to[i];
                if(v==g[u][3].loc[0]||v==fa) 
                    continue;
                path.push_back(v);
            }
        }
        if(opt==3)
        {
            for(int i=h[u];i!=-1;i=ne[i])
            {
                int v=to[i];
                if(v==g[u][2].loc[0]||v==fa) 
                    continue;
                path.push_back(v);
            }
            get_path(g[u][2].loc[0],u,2);
            path.push_back(u);
        }
    }

    void solve()
    {
        dfs(1,0);
        cout<<f[1][0]<<endl;
        get_path(1,0,0);
        cout<<path.size()<<endl;
        for(auto v:path) cout<<v<<" ";
        cout<<endl;
        return;
    }
};

namespace solve3
{
    int fa[N];
    vector<int> path;

    void dfs1(int u,int f)
    {
        fa[u]=f;
        for(int i=h[u];i!=-1;i=ne[i])
        {
            int v=to[i];
            if(v==f) continue;
            dfs1(v,u);
        }
    }

    void dfs2(int u)
    {
        path.push_back(u);
        for(int i=h[u];i!=-1;i=ne[i])
        {
            int v=to[i];
            if(v==fa[u]) continue;
            for(int j=h[v];j!=-1;j=ne[j])
            {
                int k=to[j];
                if(k==fa[v]) continue;
                dfs2(k);
            }
            path.push_back(v);
        }
    }

    void solve()
    {
        dfs1(1,0);
        dfs2(1);
        int ans=0;
        for(int i=1;i<=n;i++) ans+=val[i];
        cout<<ans<<endl;
        cout<<n<<endl;
        for(auto v:path) cout<<v<<" ";
        cout<<endl;
        return;
    }
};

void add(int u,int v)
{
    to[++idx]=v;
    ne[idx]=h[u];
    h[u]=idx;
}

signed main()
{
    memset(h,-1,sizeof h);
    cin>>n>>k;
    for(int i=1;i<n;i++)
    {
        int u,v;
        cin>>u>>v;
        add(u,v),add(v,u);
    }
    for(int i=1;i<=n;i++)
        cin>>val[i];
    if(k==1) solve1::solve();
    else if(k==2) solve2::solve();
    else if(k==3) solve3::solve();
    return 0;
}
//NO all in 
//NEVER get mad
//pay attention
//Clear all when start testing a new data