题解:P10070 [CCO 2023] Travelling Trader
Solution
首先发现如果
然后发现
K=1
这个是容易的因为你每到一个城市就必须交易,故答案就是从
K=3
首先说明这个情况一定存在一种行走方式使得整个树都被遍历一遍。
我们先来考虑
以这个东西为例,假设权值全部相同,那么最优的方案是:
我们发现
那么理论分析一下:
我们发现我们如果要从一棵子树进入另外一棵子树有且仅有两种方式,一种是从这个子树的根,一种是从这个子树的
但是如果使用第一种,那么就会出现我们同时使用了两个子树的根,那么另外一棵子树转移是就只能用
放一张图,绿色代表第二种跳子树的方式,即跳到
这里建议还是手玩一下,情况较少,也可以更好理解后文提到的转移。
然后我们观察
所以这种情况我们依据深度奇偶分层就可以实现构造方案。
K=2
所以这个才是这道题的难点。
超级大芬警告
由于我们需要知道一个子树选不选,一个子树怎么选,在这里,数据结构显然是难以维护的,分治之类的算法也不好做,因为你需要知道你是怎么进入这个分治中心的。
所以这道题我们考虑树形 DP。
众所周知,树形 DP 最困难的地方就在于状态设计抽象和转移的细节多,这道题也不例外。
我们开始对我们刚才所说的为啥不能全选的理论进行状态的设计,我们状态的设计应该直接指向这道题的难点,你如何进入这棵树的,也就是说你的落脚点是哪里。
考虑一下
然后考虑转移:
这个是显然的,因为
这个式子的意思是,一个子树由
这里的意思是,由
从
先从
因为要回到
注意到根据 DP 的无后效性,我们这里一切关于
就是说,对于上文中我们有一些状态,要求要选
在这里有一个小细节在上文的转移中没有给出,对于在转移儿子的
人话翻译就是,我们需要把后面的求和变为一个定值
最后显然的是对于一个点
然后就是输出方案了。
直接从根开始向最优决策点转移即可,但是需要深度理解转移的意义,否则就会因为进入子树的顺序导致方案错误。
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