版本比较
CF重现赛37
正文
增加 105 行删除 0 行
95 行未改动9697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320495 行未改动 return 0;}```::::
## CF1675F Vlad and Unfinished Business::::info[题目链接]https://www.luogu.com.cn/problem/CF1675F::::::::info[做法]可以从 $x$ 开始 $dfs$,容易算出有多少条边,是一定要走过去的。然后我们算出一共要经过多少条必要的边,数量记为 $E$,由于我们一定是要先走下去在走回去再走回来,那么就是 $2E$ 条边要走。然后我们还要从 $x$ 从走到 $y$,所以最坏情况总和是 $2E+dist(x,y)$,但是我们发现有一些 $x \to y$ 的路径上的边会和 $E$ 条边重合,这些边也可以 $dfs$ 一遍,然后最终的式子是 $2E+dist(x,y)-same*2$,减两遍的原因是,我们的最短路径中和 $2E$ 条边中可以不走这些重合的边,因为你可以在一开始从 $x$ 出发就走掉,这样在 $2E$ 条边中,就不用往回走,而在 $dist(x,y)$ 中你也不用走这些边,因为一开始第一遍已经走到这里了。::::::::info[代码]```cpp#include<bits/stdc++.h>using namespace std;#define ll long long#define ull unsigned long long#define fi first#define se second#define str string#define YES cout<<"YES"<<'\n';#define NO cout<<"NO"<<'\n';inline int read(){ 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-'0',ch=getchar(); return x*f;}void write(int x){ if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+'0'); return;}const ll INF=1e18;const ll MAXN=2e5+5;ll t,n,k,a[MAXN],x,y,dep[MAXN];ll need[MAXN],fa[MAXN],cnt;vector<ll> g[MAXN];void dfs1(ll u,ll f){ fa[u]=f; dep[u]=dep[f]+1; for(ll v:g[u]){ if(v==f){ continue; } dfs1(v,u); if(need[v]){ need[u]=1; cnt++; } }}void solve(){ cin>>n>>k>>x>>y; for(int i=1;i<=n;++i){ g[i].clear(); need[i]=0; dep[i]=0; fa[i]=0; } for(int i=1;i<=k;++i){ cin>>a[i]; need[a[i]]=1; } for(int i=1;i<n;++i){ ll u,v; cin>>u>>v; g[u].push_back(v); g[v].push_back(u); } cnt=0; dfs1(x,0); ll same=0; ll u=y; while(u!=x){ if(need[u]){ same++; } u=fa[u]; } ll dis=dep[y]-dep[x]; ll ans=2*cnt+dis-2*same; cout<<ans<<'\n';}int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>t; while(t--){ solve(); } return 0;}```::::