/ Randle /

记录详情

Compile Error

/in/foo.cc: In function 'void Build(int, int, int)':
/in/foo.cc:19:4: error: reference to 'next' is ambiguous
    next[cnt]=first[f];
    ^~~~
/in/foo.cc:12:19: note: candidates are: int next [200010]
 int first[100010],next[200010],deep[100010];
                   ^~~~
In file included from /usr/include/c++/6/bits/stl_algobase.h:66:0,
                 from /usr/include/c++/6/bits/char_traits.h:39,
                 from /usr/include/c++/6/ios:40,
                 from /usr/include/c++/6/ostream:38,
                 from /usr/include/c++/6/iostream:39,
                 from /in/foo.cc:4:
/usr/include/c++/6/bits/stl_iterator_base_funcs.h:205:5: note:                 template<class _ForwardIterator> _ForwardIterator std::next(_ForwardIterator, typename std::iterator_traits<_Iter>::difference_type)
     next(_ForwardIterator __x, typename
     ^~~~
/in/foo.cc: In function 'int Dfs(int, int)':
/in/foo.cc:26:28: error: reference to 'next' is ambiguous
     for(int i=first[t];i;i=next[i])
                            ^~~~
/in/foo.cc:12:19: note: candidates are: int next [200010]
 int first[100010],next[200010],deep[100010];
                   ^~~~
In file included from /usr/include/c++/6/bits/stl_algobase.h:66:0,
                 from /usr/include/c++/6/bits/char_traits.h:39,
                 from /usr/include/c++/6/ios:40,
                 from /usr/include/c++/6/ostream:38,
                 from /usr/include/c++/6/iostream:39,
                 from /in/foo.cc:4:
/usr/include/c++/6/bits/stl_iterator_base_funcs.h:205:5: note:                 template<class _ForwardIterator> _ForwardIterator std::next(_ForwardIterator, typename std::iterator_traits<_Iter>::difference_type)
     next(_ForwardIterator __x, typename
     ^~~~
/in/foo.cc: In function 'void ask_ans(int, int, int, int)':
/in/foo.cc:63:30: error: reference to 'next' is ambiguous
     for(int i=first[Lca];i;i=next[i]){
                              ^~~~
/in/foo.cc:12:19: note: candidates are: int next [200010]
 int first[100010],next[200010],deep[100010];
                   ^~~~
In file included from /usr/include/c++/6/bits/stl_algobase.h:66:0,
                 from /usr/include/c++/6/bits/char_traits.h:39,
                 from /usr/include/c++/6/ios:40,
                 from /usr/include/c++/6/ostream:38,
                 from /usr/include/c++/6/iostream:39,
                 from /in/foo.cc:4:
/usr/include/c++/6/bits/stl_iterator_base_funcs.h:205:5: note:                 template<class _ForwardIterator> _ForwardIterator std::next(_ForwardIterator, typename std::iterator_traits<_Iter>::difference_type)
     next(_ForwardIterator __x, typename
     ^~~~

代码

#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 first[100010],next[200010],deep[100010];
int fa[100010][30],sum[100010],f[100010],v[100010];
struct maple{
    int f,t,d;
}Rode[200010];
void Build(int f,int t,int d){
   Rode[++cnt]=(maple){f,t,d};  
   next[cnt]=first[f];
   first[f]=cnt;
   return;
}
int Dfs(int f,int t){
    deep[t]=deep[f]+1;
    fa[t][0]=f;
    for(int i=first[t];i;i=next[i])
       if(Rode[i].t!=f) sum[t]=sum[t]+Dfs(t,Rode[i].t)+Rode[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=first[Lca];i;i=next[i]){
         if(Rode[i].t!=kx&&Rode[i].t!=ky){
              ++n;
              if(Rode[i].t!=fa[Lca][0]) v[n]=sum[Rode[i].t]+Rode[i].d;
              else v[n]=sum[1]-sum[Lca];
         }
         if(Rode[i].t==kx&&deep[kx]>=deep[ky]) ans+=Rode[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(){
    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:37:05
评测时间
2019-11-03 21:37:05
评测机
分数
0
总耗时
0ms
峰值内存
0 Bytes