| 比赛 |
2025.12.13 |
评测结果 |
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
| 题目名称 |
巡逻 |
最终得分 |
100 |
| 用户昵称 |
梦那边的没好TM |
运行时间 |
0.608 s |
| 代码语言 |
C++ |
内存使用 |
7.59 MiB |
| 提交时间 |
2025-12-13 11:50:51 |
显示代码纯文本
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define cpy(a,b) copy(begin(a),end(a),begin(b))
#define ld long double
#define dot(x) fixed<<setprecision(x)
#define foru(a,b,c) for(ll a=b;a<=c;a++)
ll n,k,tot,dep[100005],fa[100005],l=1;
ll ans1,ans2,val[100005],t[100005];
vector<vector<pair<ll,ll>>>a(100005);
void dfs1(ll u,ll f,ll x){
dep[u]=dep[f]+x;
for(auto v:a[u]){
ll y=v.first,x=v.second;
if(y==f)continue;
dfs1(y,u,x);
}
if(dep[u]>dep[l])l=u;
}
void dfs2(ll u,ll f,ll x){
dep[u]=dep[f]+x;
fa[u]=f;
for(auto v:a[u]){
ll y=v.first,x=v.second;
if(y==f)continue;
dfs2(y,u,x);
}
if(dep[u]>dep[l])l=u;
}
void calc(ll u,ll f){
for(auto &v:a[u]){
ll y=v.first;
if(y==f)continue;
if(val[u]&&val[y])v.second=-1;
calc(y,u);
ans2=max(ans2,t[u]+t[y]+v.second);
t[u]=max(t[u],t[y]+v.second);
}
}
int main(){
freopen("xunluo.in" ,"r",stdin );
freopen("xunluo.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
cin>>n>>k;
foru(i,1,n-1){
ll x,y;
cin>>x>>y;
a[x].push_back({y,1});
a[y].push_back({x,1});
}
dfs1(1,0,0);
dfs2(l,0,0);
ans1=dep[l];
if(k==1){
cout<<2*(n-1)-ans1+1;
return 0;
}
for(ll i=l;i;i=fa[i]){
val[i]=1;
}
calc(1,0);
cout<<n*2-ans1-ans2;
return 0;
}