一个小问题
  • 板块P4178 Tree
  • 楼主天梦
  • 当前回复2
  • 已保存回复2
  • 发布时间2021/6/25 19:05
  • 上次更新2023/11/4 21:30:53
查看原帖
一个小问题
194093
天梦楼主2021/6/25 19:05

我的程序:

#include<bits/stdc++.h>
#define dd double
#define ld long double
#define ll long long
#define uint unsigned int
#define ull unsigned long long
#define N 40100
#define M number
using namespace std;

const int INF=0x3f3f3f3f;

inline int Max(int a,int b){
    return a>b?a:b;
}

template<typename T>  inline void read(T &x) {
    x=0; int f=1;
    char c=getchar();
    for(;!isdigit(c);c=getchar()) if(c == '-') f=-f;
    for(;isdigit(c);c=getchar()) x=x*10+c-'0';
    x*=f;
}

bool vis[N];
int root,size[N],maxsize[N],treesum,ans;
int dist[N],ftail,cnt[N],K;
struct node{
    int belong,val;
    inline node(){}
    inline node(int belong,int val) : belong(belong),val(val) {}
    inline bool operator < (const node &b) const {
        return val<b.val;
    }
};
node f[N];

struct edge{
    int to,next,w;
    inline void intt(int to_,int ne_,int w_){
        to=to_;next=ne_;w=w_;
    }
};
edge li[N<<1];
int head[N],tail;

inline void add(int from,int to,int w){
    li[++tail].intt(to,head[from],w);
    head[from]=tail;
}

inline void getzhongxin(int k,int fa){
    size[k]=1;maxsize[k]=-INF;
    for(int x=head[k];x;x=li[x].next){
        int to=li[x].to;
        if(to==fa||vis[to]) continue;
        getzhongxin(to,k);
        size[k]+=size[to];maxsize[k]=Max(maxsize[k],size[to]);
    }
    maxsize[k]=Max(maxsize[k],treesum-size[k]);
    if(maxsize[k]<maxsize[root]) root=k;
}

inline void solvezhongxin(int k){
    root=0;maxsize[0]=INF;
    getzhongxin(k,-1);getzhongxin(root,-1);
//    printf("nowroot:%d\n",root);
}

inline void getdist(int k,int fa,int belong){
    f[++ftail]=node(belong,dist[k]);cnt[belong]++;
    for(int x=head[k];x;x=li[x].next){
        int to=li[x].to;
        if(to==fa||vis[to]) continue;
        dist[to]=dist[k]+li[x].w;
        getdist(to,k,belong);
    }
}

inline void compeat(int l,int r){
    while(l<r){
        while(f[r].val+f[l].val>K) cnt[f[r].belong]--,r--;
        ans+=r-l-cnt[f[l].belong];
        l++;cnt[f[l].belong]--;
    }
//    printf("nowans:%d\n",ans);
}

inline void dfz(int k,int fa){
    vis[k]=1;ftail=0;f[++ftail]=node(k,0);dist[k]=0;
    for(int x=head[k];x;x=li[x].next){
        int to=li[x].to;
        if(to==fa||vis[to]) continue;
        dist[to]=li[x].w;getdist(to,-1,to);
    }
    sort(f+1,f+ftail+1);int l=1,r=ftail;compeat(l,r);
//    for(int i=1;i<=ftail;i++){
//        printf("f:val:%d\n",f[i].val);
//        printf("f:belong%d\n",f[i].belong);
//    }
    for(int x=head[k];x;x=li[x].next){
        int to=li[x].to;
        if(to==fa||vis[to]) continue;
        cnt[to]=0;
    }
    for(int x=head[k];x;x=li[x].next){
        int to=li[x].to;
        if(to==fa||vis[to]) continue;
        treesum=size[to];solvezhongxin(to);
        dfz(root,k);
    }
}

int n,k;

int main(){
//    freopen("my.out","w",stdout);
    read(n);
    for(int i=1;i<=n-1;i++){
        int from,to,w;read(from);read(to);read(w);
        add(from,to,w);add(to,from,w);
    }
    read(K);
    treesum=n;solvezhongxin(1);dfz(root,-1);
    printf("%d",ans);
    return 0;
}

这个程序它过了,但是我这里加了对 cnt 数组的清零,否则它不能 AC 。可是我想知道哪种情况会导致 cnt 数组不是 0? 目前已知的有:

  • compeat 函数结束时 l,r 可能不是0。
  • 除了 l,r 还有其他地方可能不是 0。

可能上面的两个结论不对哈

2021/6/25 19:05
加载中...