Luogu3398 仓鼠找sugar (LCA)

第一发lg[]没开够RE了,下了数据本地一直停止运行,还以为是dfs死了,绝望一交,A了。。。
判断\(x\)是否在路径\(s-t\)上,只需满足
\(dep_{x} >= dep_{LCA(s,t)}\)
\(LCA(s, x) = x\) \(or\) \(LCA(t, x) = x\)

//#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long
#define ON_DEBUG

#ifdef ON_DEBUG

#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x)  cout << #x << " = " << x << endl
#define Pause() system("pause")

#else

#define D_e_Line ;

#endif

struct ios{
    template<typename ATP>ios& operator >> (ATP &x){
        x = 0; int f = 1; char c;
        for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
        while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
        x*= f;
        return *this;
    }
}io;
using namespace std;

const int N = 100007;

struct Edge{
    int nxt, pre;
}e[N << 1];
int head[N], cntEdge;
inline void add(int u, int v){
    e[++cntEdge] = (Edge){head[u], v}, head[u] = cntEdge;
}

int dep[N], f[N][19];
int lg[N];
inline void DFS(int u, int fa){
    dep[u] = dep[fa] + 1, f[u][0] = fa;
    for(register int i = 1; (1 << i) <= dep[u]; ++i){
        f[u][i] = f[f[u][i - 1]][i - 1];
    }
    for(register int i = head[u]; i; i = e[i].nxt){
        int v = e[i].pre;
        if(v == fa) continue;
        
        DFS(v, u);
    }
}
inline int LCA(int x, int y){
    if(dep[x] < dep[y]) Swap(x, y);
    while(dep[x] > dep[y]){
        x = f[x][lg[dep[x] - dep[y]] - 1];
    }
    if(x == y) return x;
    nR(i,lg[dep[x]] - 1,0){
        if(f[x][i] != f[y][i]){
            x = f[x][i];
            y = f[x][i];
        }
    }
    return f[x][0];
}

//int fa[N], siz[N], son[N], dep[N];
//inline void DFS_First(int u, int father){
//  fa[u] = father, siz[u] = 1, dep[u] = dep[father] + 1;
//  for(register int i = head[u]; i; i = e[i].nxt){
//      int v = e[i].pre;
//      if(v == father) continue;
//      DFS_First(v, u);
//      siz[u] += siz[v];
//      if(!son[u] || siz[v] > siz[son[u]]){
//          son[u] = v;
//      }
//  }
//}
//int top[N], dfn[N], dfnIdx;
//inline void DFS_Second(int u, int ancester){
//  top[u] = ancester, dfn[u] = ++dfnIdx;
//  if(!son[u]) return;
//  DFS_Second(son[u], ancester);
//  for(register int i = head[u]; i; i = e[i].nxt){
//      int v = e[i].pre;
//      if(v != fa[u] && v != son[u]){
//          DFS_Second(v, v);
//      }
//  }
//}
//inline int LCA(int x, int y){
//  while(top[x] != top[y]){
//      if(dep[top[x]] < dep[top[y]]) Swap(x, y);
//      x = fa[top[x]];
//  }
//  return dep[x] < dep[y] ? x : y;
//}

int main(){
//  freopen("in.txt","r",stdin);
//  freopen("my.txt","w",stdout);
    int n, Ques;
    io >> n >> Ques;
    R(i,2,n){
        int u, v;
        io >> u >> v;
        add(u, v);
        add(v, u);
    }
    R(i,1,n) lg[i] = lg[i - 1] + ((1 << lg[i - 1]) == i);
    
//  DFS_First(1, 0);
//  DFS_Second(1, 1);

    DFS(1, 0);
    
    while(Ques--){
        int a, b, c, d;
        io >> a >> b >> c >> d;
        
        int S1 = LCA(a, b), S2 = LCA(c, d);
        if(dep[S1] < dep[S2]){
            Swap(S1, S2);
            c = a;
            d = b;
        }
        if(LCA(S1, c) == S1 || LCA(S1, d) == S1)
            printf("Y\n");
        else
            printf("N\n");
    }

    return 0;
}

1570282-20190720151700982-879633814.png

转载于:https://www.cnblogs.com/bingoyes/p/11217838.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值