/ Randle /

记录详情

Wrong Answer


  
# 状态 耗时 内存占用
#1 Accepted 3ms 4.312 MiB
#2 Accepted 3ms 4.199 MiB
#3 Accepted 2ms 4.316 MiB
#4 Accepted 56ms 16.703 MiB
#5 Accepted 59ms 16.703 MiB
#6 Accepted 57ms 16.711 MiB
#7 Wrong Answer 194ms 16.617 MiB
#8 Wrong Answer 179ms 16.695 MiB
#9 Wrong Answer 186ms 16.695 MiB
#10 Wrong Answer 177ms 16.688 MiB

代码

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define rep(i,j,k) for (int i=j;i<=k;i++)
#define dep(i,j,k) for (int i=j;i>=k;i--)
#define m(a,b) memset(a,b,sizeof(a))
#define ll long long
using namespace std;
int n,m,cnt,A,B,C,X,Y,ans;
int head[100010],nextt[200010],deep[100010];
int fa[100010][30],sum[100010],f[100010],v[100010];
struct node{
    int f,t,d;
}tree[200010];
void Build(int f,int t,int d){
   tree[++cnt]=(node){f,t,d};  
   nextt[cnt]=head[f];
   head[f]=cnt;
   return;
}
int Dfs(int f,int t){
    deep[t]=deep[f]+1;
    fa[t][0]=f;
    for(int i=head[t];i;i=nextt[i])
       if(tree[i].t!=f) sum[t]=sum[t]+Dfs(t,tree[i].t)+tree[i].d;
    return sum[t];   
}
void Done(){
    for(int i=1;i<=log2(n);++i)
       for(int j=1;j<=n;++j)
          fa[j][i]=fa[fa[j][i-1]][i-1];
    return;
}
int lca(int x,int y){
    if(deep[x]<deep[y]) swap(x,y);
    for(int i=log2(n);i>=0;--i)
        if(deep[fa[x][i]]>=deep[y])
            x=fa[x][i];
    if(x==y) return x;
    for(int i=log2(n);i>=0;--i)
       if(fa[x][i]!=fa[y][i])
          x=fa[x][i],y=fa[y][i];
    return fa[x][0];
}
int ask(int x,int d){
    for(int i=log2(n);i>=0;--i)
        if(deep[fa[x][i]]>=d)
           x=fa[x][i];
    return x;
}
bool cmp(int a,int b){
    return a>b;
}
void ask_ans(int kx,int ky,int Lca,int L){
    if(kx!=ky){   
        if(deep[kx]>=deep[ky]) ans=sum[kx];  
        else ans=sum[1]-sum[Lca];
    }
    else ans=0;
    int n=0;
    for(int i=head[Lca];i;i=nextt[i]){
         if(tree[i].t!=kx&&tree[i].t!=ky){
              ++n;
              if(tree[i].t!=fa[Lca][0]) v[n]=sum[tree[i].t]+tree[i].d;
              else v[n]=sum[1]-sum[Lca];
         }
         if(tree[i].t==kx&&deep[kx]>=deep[ky]) ans+=tree[i].d; 
    }
    int k=1;
    if(L%2!=0) ++k;
    sort(v+1,v+n+1,cmp); 
    for(int i=k;i<=n;i+=2)  ans+=v[i];
    return;
}
int main(){
   // freopen("b.in","r",stdin);
   // freopen("b.out","w",stdout);
    scanf("%d%d",&n,&m);
    int MM=m;
    for(int i=1;i<n;++i){
        scanf("%d%d%d",&A,&B,&C);
        Build(A,B,C);
        Build(B,A,C);
    }
    sum[1]=Dfs(1,1);
    Done();
    while(m--){
        scanf("%d%d",&X,&Y);
        int Lca=lca(X,Y),kx,ky,k;
        int L=deep[X]+deep[Y]-2*deep[Lca];
        int lll=L;
        if(L%2==0) L/=2;
        else L=L/2+1;
        if(deep[X]-deep[Lca]==L) {
            kx=ask(X,deep[Lca]+1);
            ky=ask(Y,deep[Lca]+1);
            ask_ans(kx,ky,Lca,lll);
        }
        else{
            if(X==fa[Y][0]) ask_ans(X,Y,Y,1);
            else if(deep[X]-deep[Lca]>L) {
                k=ask(X,deep[X]-L+1);
                ask_ans(k,fa[k][1],fa[k][0],lll);
            } 
            else if(deep[X]-deep[Lca]<L){
                L=L-deep[X]+deep[Lca];
                int l=deep[Lca]+L+1;
                k=ask(Y,l);
                ask_ans(fa[k][1],k,fa[k][0],lll);
            }
        }
        if ((n<=1000 && MM<=1000) || (MM==1))printf("%d\n",ans);
    }
    return 0;
}

信息

递交者
类型
递交
题目
树上角斗 T2
题目数据
下载
语言
C++
递交时间
2019-11-03 21:40:33
评测时间
2019-11-03 21:40:33
评测机
分数
60
总耗时
921ms
峰值内存
16.711 MiB