# [Luogu 3398] 仓鼠找sugar

<题目链接>

x = LCA(a, b), y = LCA(c, d)，

#include <algorithm>
#include <cstdio>

const int MAXN = 100010;

int n, q;

struct Graph
{
struct Edge
{
int to;
Edge *next;
Edge(int to, Edge* next): to(to), next(next) {}
~Edge(void)
{
if(next != NULL)
delete next;
}
Graph(int n)
{
}
~Graph(void)
{
for(int i = 1; i <= n; ++i)
}
{
}
}*G;

namespace HLD
{
int num;
struct Node
{
int depth, father, son, top, size, DFN;
}s[MAXN];
void DFS1(int u, int k)
{
s[u].depth = k;
s[u].size = 1;
int v;
for(Graph :: Edge *i = G -> head[u]; i != NULL; i = i -> next)
if(!s[v = i -> to].size)
{
DFS1(v, k + 1);
s[u].size += s[v].size;
s[v].father = u;
if(s[v].size > s[s[u].son].size)
s[u].son = v;
}
}
void DFS2(int u, int top)
{
s[u].top = top;
s[u].DFN = ++num;
if(s[u].son)
DFS2(s[u].son, top);
int v;
for(Graph :: Edge *i = G -> head[u]; i != NULL; i = i -> next)
if(!s[v = i -> to].DFN)
DFS2(v, v);
}
void Init(void)
{
DFS1(1, 1);
DFS2(1, 1);
}
int LCA(int x, int y)
{
int a, b;
while((a = s[x].top) ^ (b = s[y].top))
if(s[a].depth > s[b].depth)
x = s[a].father;
else
y = s[b].father;
return s[x].depth < s[y].depth ? x : y;
}
bool Range(int x, int y)
{
return s[x].DFN <= s[y].DFN && s[y].DFN < s[x].DFN + s[x].size;
}
bool Query(int a, int b, int c, int d)
{
int x = LCA(a, b), y = LCA(c, d);
return (Range(x, c) || Range(x, d)) && (Range(y, a) || Range(y, b));
}
}

int main(void)
{
scanf("%d %d", &n, &q);
G = new Graph(n);
for(int i = 1, u, v; i < n; ++i)
{
scanf("%d %d", &u, &v);
}
HLD :: Init();
for(int i = 1, a, b, c, d; i <= q; ++i)
{
scanf("%d %d %d %d", &a, &b, &c, &d);
puts(HLD :: Query(a, b, c, d) ? "Y" : "N");
}
return 0;
}


posted @ 2018-11-06 02:22  Capella  阅读(162)  评论(0编辑  收藏  举报