蒟蒻求助!15分 为什么orz

P1600 [NOIP2016 提高组] 天天爱跑步

DeNeRATe @ 2019-10-22 12:10:00

//running
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<map>
#include<string>
#include<iostream>

typedef long long LL;
#define Cl(X) memset(X,0,sizeof(X))
#define INF 0x7fffffff
using namespace std;
const int MAXn=3e5+5;

int Total,Mem,u,v,Jud[MAXn],Res,St,En,Fa[MAXn];
int Next[MAXn<<1],End[MAXn<<1],Head[MAXn],Cur;
int Anc[MAXn][20],Dep[MAXn],Lev[MAXn],Time[MAXn];

inline int read(){
    int Temp=0,Fac=1;
    char c=getchar();
    while(c<'0' || c>'9'){
        if(c=='-') Fac=-1;
        c=getchar();
    }
    while(c>='0' && c<='9'){
        Temp=(Temp<<3) +(Temp<<1)+c-'0';
        c=getchar();
    }
    return Temp*Fac;
}

inline void Add_Edge(int From,int To){
    Next[++Cur]=Head[From];
    Head[From]=Cur;
    End[Cur]=To;
}

inline void DFS_Tree(int Temp,int Pre){
    Fa[Temp]=Pre;
    if(Temp==1) for(int i=0;i<=19;i++) Anc[Temp][i]=Temp;
    else{
        for(int i=1;i<=19;i++)
          Anc[Temp][i]=Anc[Anc[Temp][i-1]][i-1];    
    }
    for(int i=Head[Temp];i;i=Next[i]){
        if(End[i]==Pre) continue;
        Anc[End[i]][0]=Temp;
        Dep[End[i]]=Dep[Temp]+1;
        DFS_Tree(End[i],Temp);
    }
}

inline void Swim(int &Temp,int Dist){
    int T=0;
    while(Dist){
        if(Dist & 1) Temp=Anc[Temp][T];
        T++;Dist>>=1;
    }
}

inline int LCA(int S,int E){
    int W=0,Dis=0;
    while(true){
        for(W=0;;W++)
          if(Anc[S][W]==Anc[E][W]){Dis=W;break;}
        if(!Dis) return Anc[S][Dis];
        else{
            S=Anc[S][Dis-1];E=Anc[E][Dis-1];
        }
    }
    return -1;
}

inline void RunS(int F,int S){
    if(Time[S]==Jud[S]) Lev[S]++;
    if(S==F) return;
    Time[Fa[S]]=Time[S]+1;
    RunS(F,Fa[S]);
}

inline void RunE(int F,int E){
    if(Time[E]==Jud[E] && E!=F) Lev[E]++;
    if(F==E) return;
    Time[Fa[E]]=Time[E]-1;
    RunS(F,Fa[E]);
}

int main(){
//  freopen("running.in","r",stdin);
//  freopen("running.out","w",stdout);
    Total=read();Mem=read();
    for(int i=1;i<Total;i++){
        u=read();v=read();
        Add_Edge(u,v);Add_Edge(v,u);
    }
    Dep[1]=1;
    DFS_Tree(1,-1);
    for(int i=1;i<=Total;i++) Jud[i]=read();
    for(int i=1;i<=Mem;i++){
        u=read();v=read();
        St=u;En=v;
        if(Dep[u]<Dep[v]) swap(u,v);
        Swim(u,Dep[u]-Dep[v]);
        if(u==v) Res=u;
        else Res=LCA(u,v);
        Cl(Time);
        Time[Res]=Dep[St]-Dep[Res];
        RunS(Res,St);
        Time[En]=Time[Res]+Dep[En]-Dep[Res];
        RunE(Res,En);
    }
    for(int i=1;i<=Total;i++) printf("%d ",Lev[i]);
//  fclose(stdin);fclose(stdout);
    return 0;
}

by _QAQ @ 2019-10-22 12:17:25

加油


by PXY_lover @ 2019-10-23 14:35:40

前来膜拜

跪跪跪跪跪

跪     跪跪跪跪跪跪跪跪跪跪跪跪跪

跪    跪   跪      跪

跪    跪   跪     跪

跪    跪   跪    跪

跪跪跪跪跪   跪   跪跪跪跪跪


|