当前位置:网站首页>Logu p3398 hamster looks for sugar (double LCA on the tree to judge whether the two paths in the tree intersect)
Logu p3398 hamster looks for sugar (double LCA on the tree to judge whether the two paths in the tree intersect)
2022-07-02 10:59:00 【Morgannr】
Hamsters look for sugar
Title Description
Hamster's and his base (mei) friend (zi)sugar Living in underground caves , The number of each node is 1~n. The underground cave is a tree structure . On this day, hamster plans to go from his bedroom (a) To the restaurant (b), And his base friend is going to be in his bedroom at the same time (c) To the library (d). They all take the shortest path . Now little hamster wants to know , Is it possible to be somewhere , You can meet his friends ?
Hamsters are so weak , And every day zzq Uncle abuse , Please come and save him !
Input format
Two positive integers in the first line n and q, It indicates the number of nodes in the tree and the number of queries .
Next n-1 That's ok , Two positive integers per line u and v, Representation node u To the node v There is a side between .
Next q That's ok , Four positive integers per line a、b、c and d, Indicates the node number , That is, an inquiry , Its meaning is as above .
Output format
For each inquiry , If there is a public point , Output capital letters “Y”; Otherwise output “N”.
Examples #1
The sample input #1
5 5
2 5
4 2
1 3
1 4
5 1 5 1
2 2 1 4
4 1 3 4
3 1 1 5
3 5 1 4
Sample output #1
Y
N
Y
Y
Y
Tips
__ Time limit of this question 1s, Memory limit 128M, Because the speed of the new evaluation machine is close to NOIP The speed of the evaluation machine , Note the effect of the constant problem .__
20% The data of n<=200,q<=200
40% The data of n<=2000,q<=2000
70% The data of n<=50000,q<=50000
100% The data of n<=100000,q<=100000
The question :
Given n Nodes , Nodes are connected in total n - 1 side , give most 1e5 A asked , Every inquiry is judged a ~ b and c ~ d Whether the two paths intersect . Intersection output ‘Y’, otherwise ‘N’.
Ideas :
Draw more pictures to simulate , You can find the following magical rule :
If a ~ b and c ~ d Two paths intersect , Then there must be a ~ b The path of LCA stay c ~ d On the path , perhaps c ~ d The path of LCA stay a ~ b On the path .
and Judge a node x, Whether in the path s - t The following conditions need to be met :( please remember , Important conclusions )
depth[x] >= depth[lca(s, t)] // x be located s、t Two o'clock lca Same height or below
lca(s, x) = x || lca(t, x) = x; // s、x Of lca yes x or t、x Of lca yes x
Code :
#define _CRT_SECURE_NO_WARNINGS 1
#include <bits/stdc++.h>
using namespace std;
//#define int long long
//#define map unordered_map
const int N = 1e5 + 10, M = N << 1;
int n, q;
int h[N], e[M], ne[M], idx;
int depth[N];
int fa[N][18];
void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
}
void bfs(int u)
{
queue<int> q; q.push(u);
while (q.size())
{
auto t = q.front(); q.pop();
for (int i = h[t]; ~i; i = ne[i])
{
int j = e[i];
if (depth[j] > depth[t] + 1)
{
depth[j] = depth[t] + 1;
q.push(j);
fa[j][0] = t;
for (int k = 1; k <= 17; ++k)
{
fa[j][k] = fa[fa[j][k - 1]][k - 1];
}
}
}
}
}
int lca(int a, int b)
{
if (depth[a] < depth[b]) swap(a, b);
for (int k = 17; k >= 0; --k)
{
if (depth[fa[a][k]] >= depth[b]) a = fa[a][k];
}
if (a == b) return a;
for (int k = 17; k >= 0; --k)
{
if (fa[a][k] != fa[b][k])
{
a = fa[a][k];
b = fa[b][k];
}
}
return fa[a][0];
}
void init(int root)
{
memset(depth, 0x3f, sizeof depth);
depth[0] = 0, depth[root] = 1;
bfs(root);
}
bool jud(int a, int b, int c, int d)
{
int p = lca(a, b);
if (depth[p] >= depth[lca(c, d)] && (lca(c, p) == p || lca(d, p) == p))
return true;
return false;
}
signed main()
{
int T = 1; //cin >> T;
while (T--)
{
memset(h, -1, sizeof h);
cin >> n >> q;
int t = n - 1;
int root = 0;
for (int i = 0; i < t; ++i)
{
int u, v; scanf("%d%d", &u, &v);
if (!i) root = u;
add(u, v), add(v, u);
}
init(root);
while (q--)
{
int a, b, c, d; scanf("%d%d%d%d", &a, &b, &c, &d);
if (jud(a, b, c, d) || jud(c, d, a, b))
{
puts("Y");
}
else puts("N");
}
}
return 0;
}
边栏推荐
- 软件产品管理系统有哪些?12个最佳产品管理工具盘点
- MySQL keyword
- PCL 投影点云
- JVM garbage collector
- UVM - usage of common TLM port
- Special topic of binary tree -- acwing 19 The next node of the binary tree (find the successor of the node in the tree)
- Pywin32 opens the specified window
- Win11 arm系统配置.net core环境变量
- 4. Random variables
- Sus system availability scale
猜你喜欢

二叉树专题--AcWing 3384. 二叉树遍历(已知先序遍历 边建树 边输出中序遍历)

【TS】1368- 秒懂 TypeScript 泛型工具类型!

HDU1236 排名(结构体排序)

(五)APA场景搭建之挡位控制设置

How to get the password of cpolar?

Special topic of binary tree -- acwing 1589 Building binary search tree
![[SUCTF2018]followme](/img/63/9104f9c8bd24937b0fc65053efec96.png)
[SUCTF2018]followme

Use of vscode tool

JSP webshell免杀——JSP的基础

618 what is the secret of dominating the list again? Nike's latest financial report gives the answer
随机推荐
AttributeError: type object ‘Image‘ has no attribute ‘fromarray‘
HDU1236 排名(结构体排序)
(五)APA场景搭建之挡位控制设置
PCL之滤波
实验电镜距离测量之Matlab处理
【快应用】Win7系统使用华为IDE无法运行和调试项目
Convert yv12 to rgb565 image conversion, with YUV to RGB test
华为联机对战服务玩家掉线重连案例总结
记录 AttributeError: ‘NoneType‘ object has no attribute ‘nextcall‘
HDU1228 A + B(map映射)
Kustomize使用手册
Special topic of binary tree -- acwing 1497 Traversal of the tree (use post and mid order traversal to build a binary tree)
2022-06-17
UVM learning - build a simple UVM verification platform
《实习报告》Skywalking分布式链路追踪?
Shapiro Wilk normal analysis by SPSS
JSP webshell free -- the basis of JSP
Common methods of JS array
PCL point cloud to depth image
华为游戏初始化init失败,返回错误码907135000