NOIP2015D2T3 Difference in transportation plan tree

Topic
Two points + LCA + LCA+ L C A but the size of the code is a bit big...type it
slowly...Look at the code
PS: PS:PS: This question card doublesLCA LCAL C A needs to usetarjan tarjant a r j a n but you can pass on Luogu

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define reg register
#define N (int)(3e5+1)
#define M 20
inline void read(int &x){
    
    
    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<<3)+(s<<1)+(ch&15);ch=getchar();}
    x=s*w;
}
int n,m,u,v,w,cnt,num,mxn,now,flag,head[N],dep[N],dis[N],sum[N],pre[N],f[N][25];
ll l,r=3e9,mid;
struct node{
    
    
    int to,val,nxt;
}edge[N<<1];
struct stu{
    
    
    int s,t,lcaa,len;
}q[N];
inline void addedge(int u, int v, int w){
    
    
    edge[++cnt].to=v,edge[cnt].val=w,edge[cnt].nxt=head[u],head[u]=cnt;
}
inline void superadd(int u, int v, int w){
    
    
    addedge(u,v,w),addedge(v,u,w);
}
void pretreat(int u, int fa){
    
    
    dep[u]=dep[fa]+1;
    f[u][0]=fa;
    for(reg int i=1;i<=M;i++)f[u][i]=f[f[u][i-1]][i-1];
    for(reg int i=head[u];i;i=edge[i].nxt){
    
    
        int vv=edge[i].to;
        if(vv==fa)continue;
        pre[vv]=edge[i].val;
        dis[vv]=dis[u]+edge[i].val;
        pretreat(vv,u);
    }
}
inline int LCA(int x, int y){
    
    
    if(dep[x]<dep[y])swap(x,y);
    for(reg int i=M;~i;i--)if(dep[f[x][i]]>=dep[y])x=f[x][i];
    if(x==y)return x;  
    for(reg int i=M;~i;i--)if(f[x][i]!=f[y][i])x=f[x][i],y=f[y][i];
    return f[x][0];
}
int dfs(int u, int fa, int num, int leng){
    
    
    int AG=sum[u];
    for(reg int i=head[u];i;i=edge[i].nxt){
    
    
        int vv=edge[i].to;
        if(vv==fa)continue;
        AG+=dfs(vv,u,num,leng);
    }
    if(AG>=num&&pre[u]>=leng)flag=1;
    return AG;
}
inline int check(ll lim){
    
    
    memset(sum,0,sizeof(sum));
    num=mxn=flag=0;
    for(reg int i=1;i<=m;i++){
    
    
        if(q[i].len>lim){
    
    
            num++;
            sum[q[i].s]++,sum[q[i].t]++,sum[q[i].lcaa]-=2;
            if(q[i].len>mxn)mxn=q[i].len;
        }
    }
    if(!num)return true;
    now=dfs(1,0,num,mxn-lim);
    return flag;
}
int main(){
    
    
    read(n),read(m);
    for(reg int i=1;i<n;i++){
    
    
        read(u),read(v),read(w);
        superadd(u,v,w);
    }
    pretreat(1,0);
    for(reg int i=1;i<=m;i++){
    
    
        read(q[i].s),read(q[i].t);
        q[i].lcaa=LCA(q[i].s,q[i].t);
        q[i].len=dis[q[i].s]-dis[q[i].lcaa]*2+dis[q[i].t];
    }
    while(l<r){
    
    
        mid=l+r>>1;
        if(check(mid))r=mid;
        else l=mid+1;
    }
    printf("%lld\n",l);
}

Guess you like

Origin blog.csdn.net/dhdhdhx/article/details/102509818