Codeforces Round #520 (Div. 2)(E. Company)

链接:http://codeforces.com/contest/1062/problem/E
思路:求一个区间内删去一个点后深度最深的lca,首先我们要了解与一个性质,如果按照dfs序下来,那么一个区间内的lca就等于dfs区间中dfs序最小和最大的两个点的lca(其他的点一定都属于这两个点lca的子树里面,可自行证明),这个性质是我们做这个题的前提题,然后区间最值的维护方法有很多,这里我用了线段树不过显得麻烦了,简单一点可以用st表,首先选出最大点u和最小点v,去除最大掉就在(l,u-1)U (u+1,r)中寻找最大值与v求lca,同理对于去除v,最终比较一下两种情况下谁的lca的深度最深就是答案了。顺便用这个题练习了一下倍增法求lca以及lca转RMQ问题,tarjan离线在这个题上我还不太清楚怎么使用,反正通过这个题把三个求lca的板子都顺带写了一下,至于树剖的版本等以后学了再补上来吧。

倍增法版本:

#include
using namespace std;
const int maxn = 1e5+100;
int n,q,t;
int maxv[maxn<<2],minv[maxn<<2],mini[maxn<<2],maxi[maxn<<2];
int in[maxn];

void pushup(int o){
   if(minv[o<<1]maxv[o<<1|1]){
       maxv[o] = maxv[o<<1];
       maxi[o] = maxi[o<<1];
   }
   else{
       maxv[o] = maxv[o<<1|1];
       maxi[o] = maxi[o<<1|1];
   }
}

void build(int o,int l,int r){
  if(l>1;
    build(o<<1,l,mid);
    build(o<<1|1,mid+1,r);
    pushup(o);
  }
  else{
    minv[o] = maxv[o] = in[l];
    mini[o] = l;
    maxi[o] = l;
  }
}

int querymin(int o,int tl,int tr,int l,int r){
  if(tr>1;
  int ret1 = querymin(o<<1,tl,mid,l,r);
  int ret2 = querymin(o<<1|1,mid+1,tr,l,r);
    if(in[ret1]>1;
    int ret1 = querymax(o<<1,tl,mid,l,r);
    int ret2 = querymax(o<<1|1,mid+1,tr,l,r);
   if(in[ret1]>in[ret2])return ret1;
    return ret2;
}


struct edge{
    int from,to,dist;
};

struct LCA{
    int f[maxn][25];
    int d[maxn];
    int dep[maxn];
    vector G[maxn];
    vector edges;
    int n,m,t;

    void init(int n,int t){
        this->n = n;
        this->t = t;
        for(int i=0;i<=n;i++)G[i].clear(),dep[i] = d[i] = 0;
        edges.clear();
    }

    void addedge(int from,int to,int dist = 1){
        edges.push_back(edge{from,to,dist});
        m = edges.size();
        G[from].push_back(m-1);
    }

    void bfs(){
        queue q;
        q.push(1);
        dep[1] = 1;
        while(!q.empty()){
            int u = q.front();
            q.pop();
            for(int i=0;idep[y])swap(x,y);
        for(int i=t;i>=0;i--){
            if(dep[f[y][i]]>=dep[x])y = f[y][i];
        }
            if(x==y)return x;
        for(int i=t;i>=0;i--){
            if(f[x][i]!=f[y][i])x = f[x][i],y = f[y][i];
        }
        return f[x][0];
    }
}solver;

void dfs(int u,int f){
    in[u] = t++;
    for(int i=0;iin[u2])uu = u1;
        else uu = u2;
        int v1 = querymin(1,1,n,l,v0-1);
        int v2 = querymin(1,1,n,v0+1,r);
        if(in[v1]solver.dep[x2]){
            resm = v0;
            res = solver.dep[x1];
        }
        else{
            resm = u0;
            res = solver.dep[x2];
        }
        printf("%d %d\n",resm,res-1);
    }
    return 0;
}

RMQ版本

#include
using namespace std;
const int maxn = 1e5+100;
int n,q,t;
int maxv[maxn<<2],minv[maxn<<2],mini[maxn<<2],maxi[maxn<<2];
int in[maxn];

void pushup(int o){
   if(minv[o<<1]maxv[o<<1|1]){
       maxv[o] = maxv[o<<1];
       maxi[o] = maxi[o<<1];
   }
   else{
       maxv[o] = maxv[o<<1|1];
       maxi[o] = maxi[o<<1|1];
   }
}

void build(int o,int l,int r){
  if(l>1;
    build(o<<1,l,mid);
    build(o<<1|1,mid+1,r);
    pushup(o);
  }
  else{
    minv[o] = maxv[o] = in[l];
    mini[o] = l;
    maxi[o] = l;
  }
}

int querymin(int o,int tl,int tr,int l,int r){
  if(tr>1;
  int ret1 = querymin(o<<1,tl,mid,l,r);
  int ret2 = querymin(o<<1|1,mid+1,tr,l,r);
    if(in[ret1]>1;
    int ret1 = querymax(o<<1,tl,mid,l,r);
    int ret2 = querymax(o<<1|1,mid+1,tr,l,r);
   if(in[ret1]>in[ret2])return ret1;
    return ret2;
}


struct edge{
    int from,to,dist;
};

struct LCA{
    int n,m;
    vector G[maxn];
    vector edges;
    int ntime;
    int dep[maxn<<1];
    int d[maxn<<1][25];
    int first[maxn];
    int vis[maxn<<1];

    void init(int n){
        this->n = n;
        for(int i=0;i<=n;i++)G[i].clear();
        edges.clear();
        ntime = 1;
        memset(vis,0,sizeof(vis));
        memset(first,0,sizeof(first));
        memset(dep,0,sizeof(dep));
    }

    void addedge(int from,int to,int dist = 1){
        edges.push_back(edge{from,to,dist});
        m = edges.size();
        G[from].push_back(m-1);
    }

    void dfs(int u,int f,int d){
        first[u] = ntime;
        vis[ntime] = u;
        dep[ntime++] = d;
        for(int i=0;iy)swap(x,y);
        return vis[query(x,y)];
    }

    void solve(){
        dfs(1,-1,0);
        RMQ_init(ntime-1);
    }
}solver;

int dd[maxn];

void dfs(int u,int f){
    in[u] = t++;
    for(int i=0;iin[u2])uu = u1;
        else uu = u2;
        int v1 = querymin(1,1,n,l,v0-1);
        int v2 = querymin(1,1,n,v0+1,r);
        if(in[v1]dd[x2]){  
            resm = v0;
            res = dd[x1];
        }
        else{
            resm = u0;
            res = dd[x2];
        }
        printf("%d %d\n",resm,res-1);
    }
    return 0;
}

你可能感兴趣的:(Codeforces Round #520 (Div. 2)(E. Company))