ARC179D Portable Gate
题意简述
有一棵树
- 经过一条树边走到相邻点,花费
。 - 将门放在当前点。
- 将自己传送到门所在的点。
求最小花费。
分析
先考虑根(出发点)固定怎么做。
由于放门没有任何代价,我们可以视为在正常行走时随身把门也带上。正常情况下我们肯定是访问完一个子树后进入另一个子树继续访问,而显然门的位置一定在子树的祖先上,所以子树之间的花费相互独立。
再发现一个比较重要的性质:一个子树内若门在子树祖先上,则传送技能仅会使用至多一次。
证明:若使用两次以上,证明该点和门点之间的边要走两遍,那么我们将门放在该点上,该点和门点之间的边也要走两遍,但遍历该子树的代价不会变劣。
由此考虑树形 dp,设
- 令
表示 子树中离 距离最远的点的距离。 - 令
,表示在走 子树时门在 的祖先上时走完 子树且不返回根节点的最小花费, 表示走完 子树且返回根节点的最小花费,若不返回根节点,肯定要尽可能的少走,我们贪心的选离 和离其最远的点这条链少走一遍,则减掉 。 ,表示对于 的每个子树 ,遍历 子树可以选择把门给 ,然后该边(指 和 之间的边,下同)一进一出花费 ,即 ;也可以在该子树内选择使用传送技能,则门留在 ,无需返回 ,且该边一进不出花费 ,即 。 ,由于不回根节点,肯定终点会落在恰好一个子树内,则该子树无需返回根节点,其他子树需要返回根节点,考虑枚举这个子树,则在回根节点的基础上,减掉原来的贡献,加上新的贡献,即,可以选择把门留下(花费仍是 ),也可以把门给 ,该边一进不出花费 ,即 。
再考虑根不固定的情况,有了树形 dp 式子,直接换根即可。
简述换根流程:
- 计算以该点为根时的答案。
- 枚举子树,删掉子树的贡献(若是取最值可以同时保留次值,若最值是该子树贡献的则减掉最值加上次值),递归进子树。
代码
注意代码中的
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<map>
#include<unordered_map>
#include<vector>
#include<queue>
#include<bitset>
#include<set>
#include<ctime>
#include<random>
#include<cassert>
#define x1 xx1
#define y1 yy1
#define IOS ios::sync_with_stdio(false)
#define ITIE cin.tie(0);
#define OTIE cout.tie(0);
#define PY puts("Yes")
#define PN puts("No")
#define PW puts("-1")
#define P0 puts("0")
#define P__ puts("")
#define PU puts("--------------------")
#define popc __builtin_popcount
#define mp make_pair
#define fi first
#define se second
#define gc getchar
#define pc putchar
#define pb emplace_back
#define rep(a,b,c) for(int a=(b);a<=(c);++a)
#define per(a,b,c) for(int a=(b);a>=(c);--a)
#define reprange(a,b,c,d) for(int a=(b);a<=(c);a+=(d))
#define perrange(a,b,c,d) for(int a=(b);a>=(c);a-=(d))
#define graph(i,j,k,l) for(int i=k[j];i;i=l[i].nxt)
#define lowbit(x) (x&-x)
#define lson(x) (x<<1)
#define rson(x) (x<<1|1)
#define mem(x,y) memset(x,y,sizeof x)
//#define double long double
#define int long long
//#define int __int128
using namespace std;
typedef long long i64;
using pii=pair<int,int>;
bool greating(int x,int y){return x>y;}
bool greatingll(long long x,long long y){return x>y;}
inline int rd(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}return x*f;
}
inline void write(int x,char ch='\0'){
if(x<0){x=-x;putchar('-');}
int y=0;char z[40];
while(x||!y){z[y++]=x%10+48;x/=10;}
while(y--)putchar(z[y]);if(ch!='\0')putchar(ch);
}
bool Mbg;
const int maxn=2e5+5,maxm=4e5+5,inf=0x3f3f3f3f;
const long long llinf=0x3f3f3f3f3f3f3f3f;
int n;
vector<int>Graph[maxn];
int f[maxn][2],g[maxn],t[maxn];
int F[maxn][2],G[maxn],T[maxn];
int siz[maxn];
int ans;
void dfs1(int x,int y){
// printf("Visit vertex %lld\n",x);
siz[x]=1;
for(int u:Graph[x])if(u^y){
dfs1(u,x),siz[x]+=siz[u];
g[x]=max(g[x],g[u]+1);
f[x][1]+=min(f[u][1]+2,t[u]);
}
f[x][0]=f[x][1];
for(int u:Graph[x])if(u^y){
f[x][0]=min(f[x][0],f[x][1]-min(f[u][1]+2,t[u])+min(f[u][0]+1,t[u]));
}
t[x]=2*(siz[x]-1)-g[x]+1;
}
void dfs2(int x,int y){
if(y){
int sum=0,res=0;
for(int u:Graph[x])if(u^y)sum+=min(f[u][1]+2,t[u]);
sum+=min(F[y][1]+2,T[y]);
res=sum;
for(int u:Graph[x])if(u^y)res=min(res,sum-min(f[u][1]+2,t[u])+min(f[u][0]+1,t[u]));
res=min(res,sum-min(F[y][1]+2,T[y])+min(F[y][0]+1,T[y]));
ans=min(ans,res);
}
multiset<pii,greater<pii> >sf,sg;
for(int u:Graph[x])if(u^y){
sf.insert(mp(min(f[u][1]+2,t[u])-min(f[u][0]+1,t[u]),u));
sg.insert(mp(g[u]+1,u));
}
if(y){
sf.insert(mp(min(F[y][1]+2,T[y])-min(F[y][0]+1,T[y]),y));
sg.insert(mp(G[y]+1,y));
}
sf.insert(mp(0,0)),sg.insert(mp(0,0));
int rety=y?min(F[y][1]+2,2*(n-siz[x]-1)-G[y]+1):0;
for(int u:Graph[x])if(u^y){
F[x][1]=f[x][1]+rety-min(f[u][1]+2,2*(siz[u]-1)-g[u]+1);
auto fit=sf.begin(),git=sg.begin();
if(fit->se==u)++fit;
F[x][0]=F[x][1]-fit->fi;
if(git->se==u)++git;
G[x]=git->fi,T[x]=2*(n-siz[u]-1)-G[x]+1;
dfs2(u,x);
}
}
void solve_the_problem(){
n=rd();
rep(i,2,n){
int x=rd(),y=rd();
Graph[x].emplace_back(y),Graph[y].emplace_back(x);
}
dfs1(1,0);
// PU;rep(i,1,n)write(f[i][0],32),write(f[i][1],10);PU;
ans=f[1][0];
dfs2(1,0);
write(ans);
}
bool Med;
signed main(){
// freopen(".in","r",stdin);freopen(".out","w",stdout);
// fprintf(stderr,"%.3lfMB\n",(&Mbg-&Med)/1048576.0);
int _=1;while(_--)solve_the_problem();
}
/*
*/
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步