1 | #include<bits/stdc++.h> |
2 | using namespace std; |
3 | const int N=1e5+10; |
4 | int n,q; |
5 | vector<int>mp[N]; |
6 | int vis[N]; |
7 | set<int> dfs(int x,int fa,int t,set<int>s) |
8 | { |
9 | if(x==t) |
10 | { |
11 | for(int i=1;i<=n;i++) |
12 | { |
13 | if(vis[i]==1) |
14 | { |
15 | s.insert(i); |
16 | } |
17 | } |
18 | return s; |
19 | } |
20 | for(int i=0;i<mp[x].size();i++) |
21 | { |
22 | if(mp[x][i]==fa) |
23 | { |
24 | continue; |
25 | } |
26 | vis[x]=1; |
27 | dfs(mp[x][i],x,t,s); |
28 | vis[x]=0; |
29 | } |
30 | } |
31 | int main() |
32 | { |
33 | cin>>n>>q; |
34 | for(int i=1;i<n;i++) |
35 | { |
36 | int u,v; |
37 | cin>>u>>v; |
38 | mp[u].push_back(v); |
39 | mp[v].push_back(u); |
40 | } |
41 | while(q--) |
42 | { |
43 | int a,b,c,d; |
44 | cin>>a>>b>>c>>d; |
45 | set<int>s1; |
46 | set<int>s2; |
47 | s1=dfs(a,-1,b,s1); |
48 | memset(vis,0,sizeof(vis)); |
49 | s2=dfs(c,-1,d,s2); |
50 | set<int>s3; |
51 | std::set_intersection(s1.begin(),s1.end(),s2.begin(),s2.end(),std::inserter(s3,s3.begin())); |
52 | if(!s3.size()) |
53 | { |
54 | cout<<"N"<<endl; |
55 | } |
56 | else |
57 | { |
58 | cout<<"Y"<<endl; |
59 | } |
60 | } |
61 | return 0; |
62 | } |