P3629 [APIO2010] 巡逻
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| #include <bits/stdc++.h> |
| using namespace std; |
| using pii=pair<int,int>; |
| const int M=2e5+5; |
| |
| int mx,w[M]; |
| int h[M],ne[M<<1],e[M<<1],tot; |
| void add(int from,int to) { |
| e[++tot]=to; ne[tot]=h[from]; h[from]=tot; |
| } |
| |
| int root1,root2; |
| int f[M],son[M]; |
| void dfs(int now,int fa) { |
| vector<pii>v; |
| for(int i=h[now];i;i=ne[i]) { |
| int to=e[i]; |
| if(to==fa)continue; |
| dfs(to,now); |
| if(f[to]+w[to]>f[now])f[now]=f[to]+w[to],son[now]=to; |
| v.push_back({f[to]+w[to],to}); |
| } |
| sort(v.rbegin(),v.rend()); |
| if(v.size()==0)f[now]=0; |
| else { |
| if(v[0].first>mx)mx=v[0].first,root1=v[0].second; |
| if(v.size()>=2&&v[0].first+v[1].first>mx) { |
| mx=v[0].first+v[1].first; |
| root1=v[0].second,root2=v[1].second; |
| } |
| } |
| } |
| |
| void up() { |
| while(root1)w[root1]=-1,root1=son[root1]; |
| while(root2)w[root2]=-1,root2=son[root2]; |
| } |
| |
| int main() { |
| int n,k; |
| cin>>n>>k; |
| for(int i=2;i<=n;i++)w[i]=1; |
| for(int i=1;i<n;i++) { |
| int x,y; |
| cin>>x>>y; |
| add(x,y); |
| add(y,x); |
| } |
| dfs(1,0); |
| if(k==1) { |
| cout<<2*(n-1)-mx+1<<endl; |
| return 0; |
| } |
| up(); |
| int tmp=mx;mx=0; |
| for(int i=1;i<=n;i++)f[i]=-1e9; |
| dfs(1,0); |
| cout<<2*(n-1)-mx-tmp+2<<endl; |
| return 0; |
| } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!