Title Description
The small hamster and his base (Mei) friend (zi) sugar live in underground caves, each with a number of 1~n. The underground cavern is a tree-shaped structure. This day the hamster intends to go from his bedroom (a) to the restaurant (b), while his base friend is at the same time from his bedroom (c) to the library (d). They all take the shortest path. Now the little hamster wants to know, is it possible to meet his base friend somewhere?
Small hamsters so weak, but also to be zzq every day, please come and save him!
Input/output format
Input format:
The first line, two positive integers n and q, indicates the number of nodes in the tree and the number of queries.
Next n-1 line, two positive integers per line, u and V, indicates that there is an edge between node U and node v.
The next Q line, four positive integers a, B, C, and D for each line, represents the node number, which is the meaning of a query.
Output format:
For each query, if there is a common point, output the capital letter "Y"; otherwise output "N".
Input and Output Sample input example # #:
5 52 54 21 31 45 1 5 12 2 1 44 1 3 43 1 1 53 5 1 4
Sample # # of output:
Ynyyy
Description
Subject time limit 1s, memory limit 128M, because the new evaluation machine speed is closer to NOIP evaluation machine speed, please pay attention to the effect of constant problem.
20% of Data n<=200,q<=200
40% of Data n<=2000,q<=2000
70% of Data n<=50000,q<=50000
100% of Data n<=100000,q<=100000
For four times LCA, determine if it is possible to encounter depending on the size relationship.
1 /*by Silvern*/2#include <algorithm>3#include <iostream>4#include <cstring>5#include <cstdio>6#include <cmath>7#include <vector>8 using namespacestd;9 Const intmxn=100010;Ten intRead () { One intx=0, f=1;CharCh=GetChar (); A while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} - while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} - returnx*F; the } - structedge{ - intv,nxt; -}e[mxn<<1]; + inthd[mxn],mct=0; - voidAdd_edge (intUintv) { +E[++MCT].V=V;E[MCT].NXT=HD[U];HD[U]=MCT;return; A } at intn,q; - intfa[mxn][ -]; - intDEP[MXN]; - voidDFS (intUintf) { -dep[u]=dep[f]+1; - for(intI=1;i< -; i++) infa[u][i]=fa[fa[u][i-1]][i-1]; - for(intI=hd[u];i;i=e[i].nxt) { to intv=e[i].v; + if(v==f)Continue; -fa[v][0]=u; the DFS (v,u); * } $ return;Panax Notoginseng } - intLCA (intXinty) { the if(dep[x]<Dep[y]) swap (x, y); + for(intI= -; i>=0; i--) A if(Dep[fa[x][i]]>=dep[y]) x=Fa[x][i]; the if(x==y)returnx; + for(intI= -; i>=0; i--) - if(fa[x][i]!=Fa[y][i]) { $x=fa[x][i];y=Fa[y][i]; $ } - returnfa[x][0]; - } the intMain () { - inti,j,u,v;WuyiN=read (); q=read (); the for(i=1; i<n;i++){ -U=read (); v=read (); Wu Add_edge (u,v); - Add_edge (v,u); About } $ intx, y; -DFS (1,0); - for(i=1; i<=q;i++){ -U=read (); V=read (); X=read (); y=read (); A inttmp=Max (Dep[lca (u,v)],dep[lca (x, y)]); + intres=Max (Max (Dep[lca (u,x)],dep[lca (u,y)), the Max (Dep[lca (v,x)],dep[lca (v,y) )); - if(res>=tmp) printf ("y\n"); $ Elseprintf"n\n"); the } the return 0; the}
Rokua P3398 hamster looking for sugar