标签:需要 getchar int 最小 pac har 最大值 += else
#include<iostream> #include<cstdio> #define lson k<<1,l,mid #define rson k<<1|1,mid+1,r #define ls k<<1 #define rs k<<1|1 #define mid ((l+r)>>1) using namespace std; const int N=200005,inf=2e9; int n,m,cnt,s,mi,tot; int head[N],fa[N][23],dis[N],dep[N]; int son[N],top[N],val[N],siz[N],id[N]; int tr[N<<2]; struct node{ int to,nxt,dis; }e[N<<1]; inline int read(){ int s=0,w=1; char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)w=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){s=s*10+ch-‘0‘;ch=getchar();} return w*s; } inline void add(int from,int to,int dis){ e[++cnt]=(node){to,head[from],dis}; head[from]=cnt; } void dfs(int x,int f,int di){ dep[x]=dep[f]+1; fa[x][0]=f; dis[x]=dis[f]+di;siz[x]=1; for(int i=1;(1<<i)<=dep[x];++i) fa[x][i]=fa[fa[x][i-1]][i-1]; for(int i=head[x];i;i=e[i].nxt) if(e[i].to!=f){ dfs(e[i].to,x,e[i].dis); siz[x]+=siz[e[i].to]; if(siz[e[i].to]>siz[son[x]])son[x]=e[i].to; } } void dfs2(int x,int topf){ top[x]=topf;id[x]=++tot;val[tot]=dis[x]; if(!son[x])return ; dfs2(son[x],topf); int y; for(int i=head[x];i;i=e[i].nxt){ y=e[i].to; if(y==fa[x][0]||y==son[x])continue; dfs2(y,y); } } inline int lca(int x,int y){ if(dep[x]>dep[y])x^=y^=x^=y; for(int i=21;i>=0;i--) if(dep[x]<=dep[fa[y][i]]) y=fa[y][i]; if(x==y)return x; for(int i=21;i>=0;--i){ if(fa[x][i]==fa[y][i])continue; x=fa[x][i]; y=fa[y][i]; } return fa[x][0]; } inline void update(int k){ tr[k]=max(tr[ls],tr[rs]); } void build(int k,int l,int r){ if(l==r){ tr[k]=val[l]; return ; } build(lson);build(rson); update(k); } int ask(int k,int l,int r,int x,int y){ if(l==x&&y==r){ return tr[k]; } if(y<=mid)return ask(lson,x,y); else if(x>mid)return ask(rson,x,y); else return max(ask(lson,x,mid),ask(rson,mid+1,y)); } void query(int x,int y){ mi=-inf; while(top[x]!=top[y]){ if(dep[top[x]]<dep[top[y]])swap(x,y); mi=max(ask(1,1,n,id[top[x]],id[x]),mi); x=fa[top[x]][0]; } if(dep[x]>dep[y])swap(x,y); mi=max(ask(1,1,n,id[x],id[y]),mi); } int main(){ freopen("road.in","r",stdin); freopen("road.out","w",stdout); n=read();m=read();s=read(); int x,y,z; for(int i=1;i<n;++i){ x=read();y=read();z=read(); add(x,y,z);add(y,x,z); } dfs(s,0,0); dfs2(s,s); build(1,1,n); int ans; while(m--){ x=read();y=read(); z=lca(x,y); if(z==s){ query(y,s);mi=dis[y]-mi; ans=dis[x]+(mi>0?0:mi); query(x,s);mi=dis[x]-mi; ans=min(ans,dis[y]+(mi>0?0:mi)); printf("%d\n",ans); } else if(z==x){ query(y,x);mi=dis[y]-mi; printf("%d\n",mi>0?0:mi); } else if(z==y){ query(x,y);mi=dis[x]-mi; printf("%d\n",mi>0?0:mi); } else { query(y,z);mi=dis[y]-mi; ans=dis[x]+(mi>0?0:mi)-dis[z]; query(x,z);mi=dis[x]-mi; ans=min(ans,dis[y]+(mi>0?0:mi)-dis[z]); printf("%d\n",ans); } } return 0; }
标签:需要 getchar int 最小 pac har 最大值 += else
原文地址:https://www.cnblogs.com/sanjinliushi/p/11704800.html