BZOJ 2282 [Sdoi2011]消防
题解:
路径一定在直径上
取出直径来
在直径上枚举左右端点就可以了(A掉)
但是可能会被卡
所以枚举左端点,右端点单调,更新答案即可
WOC ST表竟然打错了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 | #include<iostream> #include<cstdio> #include<cstring> #include<cmath> using namespace std; const int maxn=300009; int n,s; int ans=0x7fffffff; int cntedge=0; int head[maxn]={0}; int to[maxn<<1]={0},nex[maxn<<1]={0},dist[maxn<<1]={0}; void Addedge( int x, int y, int z){ nex[++cntedge]=head[x]; to[cntedge]=y; dist[cntedge]=z; head[x]=cntedge; } int lu=0,ru=0; int father[maxn]={0}; int d[maxn]={0}; void Dfs( int now, int fa){ father[now]=fa; for ( int i=head[now];i;i=nex[i]){ if (to[i]==fa) continue ; d[to[i]]=d[now]+dist[i]; Dfs(to[i],now); } } int p[maxn],nn=0; int ond[maxn]={0}; int h[maxn]={0}; int GetMaxdep( int x, int fa){ int ret=0; for ( int i=head[x];i;i=nex[i]){ if (to[i]==fa) continue ; if (ond[to[i]]) continue ; ret=max(ret,dist[i]+GetMaxdep(to[i],x)); } return ret; } int f[maxn][20]; void STinit( int n){ for ( int i=1;i<=n;++i)f[i][0]=h[i]; for ( int j=1;j<=19;++j){ for ( int i=1;i+(1<<j)-1<=n;++i){ f[i][j]=max(f[i][j-1],f[i+(1<<(j-1))][j-1]); } } } int Querymax( int l, int r){ int k=log2(r-l+1.5); return max(f[l][k],f[r-(1<<k)+1][k]); } int main(){ scanf( "%d%d" ,&n,&s); for ( int i=1;i<=n-1;++i){ int x,y,z; scanf( "%d%d%d" ,&x,&y,&z); Addedge(x,y,z); Addedge(y,x,z); } d[1]=0; Dfs(1,0); lu=1; for ( int i=1;i<=n;++i){ if (d[i]>d[lu])lu=i; } d[lu]=0; Dfs(lu,0); ru=1; for ( int i=1;i<=n;++i){ if (d[i]>d[ru])ru=i; } int x=ru; while (x){ p[++nn]=x; ond[x]=1; x=father[x]; } for ( int i=1;i<=nn;++i){ int x=p[i]; h[i]=GetMaxdep(x,0); } // for(int i=1;i<=nn;++i){ // for(int j=i;j<=nn;++j){ // if(d[p[i]]-d[p[j]]>s)continue; // int tm=max(d[p[1]]-d[p[i]],d[p[j]]-d[p[nn]]); // for(int k=i;k<=j;++k)tm=max(tm,h[k]); // ans=min(ans,tm); // } // } STinit(nn); int last=1; for ( int i=1;i<=nn;++i){ while (d[p[last]]-d[p[i]]>s)++last; int tm=max(d[p[1]]-d[p[last]],d[p[i]]-d[p[nn]]); tm=max(tm,Querymax(last,i)); ans=min(ans,tm); } printf( "%d\n" ,ans); return 0; } |
致歉:笔者已经意识到这是一篇几乎没有价值的文章,给您的阅读带来不好的体验,并且干扰了您的搜索环境,非常抱歉!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构