NEERC 17 L Laminar Family (树链剖分)

题意:

     给你一颗树,每次选取一个路径,把路径上的所有点加入到一个集合里面,问最后的集合是否两两之间满足 A B 或者 B A 或者 A B =

思路:

    我们发现,如果最后是符合条件的,那么必定是一个集合被另一个集合套的这样的形式,也就是说一个集合是完全套在另一个集合上面的。这个很明显可以利用线段树维护,那么我们开始把所有集合按集合大小从大到小排序,然后一个一个放进去染色,如果是符合条件的,那么每个我们扔进去的颜色段之前的位置必定只有一种颜色(多种就不符合了)。
    那么现在只剩下一个问题了,如何把树上的一段区间变成数组上的一段区间,这很明显就是树剖的事情了

错误及反思:

    开始的时候可以把线段树当作都被0染了一次,这样以后操作也非常方便

代码:

#include<bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int N =100010;

int segtree[N*4],lazy[N*4];
struct EDGE{
    int to,next;
}e[N*2];
int tot,tid,n,q;

struct ques{
    int len,l,r;
};
vector<ques> v;
bool cmp(ques a,ques b){
    return a.len>b.len;
}

bool judge=true;
int top[N],si[N],fa[N],first[N],son[N],depth[N],id[N],num,bef;

void addedge(int x,int y){
    e[tot].to=y;
    e[tot].next=first[x];
    first[x]=tot++;
    e[tot].to=x;
    e[tot].next=first[y];
    first[y]=tot++;
}

void dfs1(int now,int bef,int dep){
    fa[now]=bef;
    depth[now]=dep;
    si[now]=1;
    for(int i=first[now];i!=-1;i=e[i].next)
        if(e[i].to!=bef){
            dfs1(e[i].to,now,dep+1);
            si[now]+=si[e[i].to];
            if(son[now]==-1) son[now]=e[i].to;
            else son[now]=si[e[i].to]>si[son[now]]?e[i].to:son[now];
        }
}

void dfs2(int now,int tp){
    top[now]=tp;
    id[now]=tid++;
    if(son[now]!=-1) dfs2(son[now],tp);
    for(int i=first[now];i!=-1;i=e[i].next)
        if(e[i].to!=fa[now]&&e[i].to!=son[now])
            dfs2(e[i].to,e[i].to);
}

void init(){
    memset(segtree,0,sizeof(segtree));
    memset(lazy,0,sizeof(lazy));
    tot=0; tid=1;
    memset(first,-1,sizeof(first));
    memset(son,-1,sizeof(son));
}

int getlen(int L,int R){
    int dep=depth[L]+depth[R];
    int f1=top[L],f2=top[R];
    while(f1!=f2){
        if(depth[f1]<depth[f2]){
            swap(f1,f2);
            swap(L,R);
        }
        L=fa[f1];
        f1=top[L];
    }
    if(depth[L]>depth[R]) swap(L,R);
    return dep-depth[L]-depth[L]+1;
}

void pushdown(int l,int r,int rt){
    if(lazy[rt]){
        segtree[rt<<1]=segtree[rt<<1|1]=lazy[rt];
        lazy[rt<<1]=lazy[rt<<1|1]=lazy[rt];
        lazy[rt]=0;
    }
}

void pushup(int l,int r,int rt){
    if(segtree[rt<<1]==segtree[rt<<1|1]) segtree[rt]=segtree[rt<<1];
   // else if(segtree[rt<<1]==0||segtree[rt<<1|1]==0) segtree[rt]=segtree[rt<<1]?segtree[rt<<1]:segtree[rt<<1|1];
    else segtree[rt]=-1;
}

void color(int L,int R,int col,int l,int r,int rt){
    if(L<=l&&R>=r){
        if(segtree[rt]==-1){
            judge=false;
        }
        else{
            if(segtree[rt]!=bef){
                num++;
                bef=segtree[rt];
            }
            lazy[rt]=col;
            segtree[rt]=col;
        }
        return ;
    }
    pushdown(l,r,rt);
    int m=l+r>>1;
    if(L<=m) color(L,R,col,lson);
    if(R>m) color(L,R,col,rson);
    pushup(l,r,rt);
    return ;
}

void modify(int L,int R,int col){
    num=0,bef=-1;
    int f1=top[L],f2=top[R];
    while(f1!=f2){
        if(depth[f1]<depth[f2]){
            swap(f1,f2);
            swap(L,R);
        }
        color(id[f1],id[L],col,1,tid-1,1);
        L=fa[f1];
        f1=top[L];
    }
    if(depth[L]>depth[R]) swap(L,R);
    color(id[L],id[R],col,1,tid-1,1);
    if(num>1) judge=false;
    return ;
}

int main(){
    init();
    scanf("%d%d",&n,&q);
    for(int i=0,u,v;i<n-1;i++){
        scanf("%d%d",&u,&v);
        addedge(u,v);
    }
    dfs1(1,1,1);
    dfs2(1,1);
    for(int i=0,l,r;i<q;i++){
        scanf("%d%d",&l,&r);
        v.push_back({getlen(l,r),l,r});
    }
    sort(v.begin(),v.end(),cmp);
    for(int i=0;i<v.size();i++)
        modify(v[i].l,v[i].r,i+1);
    if(judge) printf("Yes\n");
    else printf("No\n");
}

猜你喜欢

转载自blog.csdn.net/roll_keyboard/article/details/81009483