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("

----------

")
#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
");
		else
			printf("N
");
	}

	return 0;
}

原文地址:https://www.cnblogs.com/bingoyes/p/11217838.html