题意:询问树上从u到v的路径是否经过k
思路:把树dfs转化为有根树后,对于u,v的路径而言,设p为u,v的最近公共祖先,u到v的路径必定是可以看成两条路径的组合,u->p,v->p,这样一来便可以将判断条件转化为(LCA(u,k)=k || LCA(v,k)=k) && LCA(k,p)=p。由于这个LCA询问里面需要用到中间结果p,所以这种方法用tarjan离线不行,只能用dfs+RMQ。
1 #pragma comment(linker, "/STACK:10240000,10240000") 2 3 #include <iostream> 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstdlib> 7 #include <cstring> 8 #include <map> 9 #include <queue> 10 #include <deque> 11 #include <cmath> 12 #include <vector> 13 #include <ctime> 14 #include <cctype> 15 #include <stack> 16 #include <set> 17 #include <bitset> 18 #include <functional> 19 #include <numeric> 20 #include <stdexcept> 21 #include <utility> 22 23 using namespace std; 24 25 #define mem0(a) memset(a, 0, sizeof(a)) 26 #define mem_1(a) memset(a, -1, sizeof(a)) 27 #define lson l, m, rt << 1 28 #define rson m + 1, r, rt << 1 | 1 29 #define define_m int m = (l + r) >> 1 30 #define rep_up0(a, b) for (int a = 0; a < (b); a++) 31 #define rep_up1(a, b) for (int a = 1; a <= (b); a++) 32 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--) 33 #define rep_down1(a, b) for (int a = b; a > 0; a--) 34 #define all(a) (a).begin(), (a).end() 35 #define lowbit(x) ((x) & (-(x))) 36 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {} 37 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {} 38 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {} 39 #define pchr(a) putchar(a) 40 #define pstr(a) printf("%s", a) 41 #define sstr(a) scanf("%s", a) 42 #define sint(a) scanf("%d", &a) 43 #define sint2(a, b) scanf("%d%d", &a, &b) 44 #define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c) 45 #define pint(a) printf("%d ", a) 46 #define test_print1(a) cout << "var1 = " << a << endl 47 #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl 48 #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl 49 #define mp(a, b) make_pair(a, b) 50 #define pb(a) push_back(a) 51 52 typedef long long LL; 53 typedef pair<int, int> pii; 54 typedef vector<int> vi; 55 56 const int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1}; 57 const int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 }; 58 const int maxn = 4e5 + 7; 59 const int md = 1e9 + 7; 60 const int inf = 1e9 + 7; 61 const LL inf_L = 1e18 + 7; 62 const double pi = acos(-1.0); 63 const double eps = 1e-6; 64 65 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);} 66 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;} 67 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;} 68 template<class T>T condition(bool f, T a, T b){return f?a:b;} 69 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];} 70 int make_id(int x, int y, int n) { return x * n + y; } 71 72 struct SparseTable { 73 int d[maxn][22]; 74 int t[maxn]; 75 void Init_min(int a[], int n) { 76 rep_up0(i, n) d[i][0] = a[i]; 77 for (int j = 1; (1 << j) <= n; j++) { 78 for (int i = 0; i + (1 << j) - 1 < n; i++) { 79 d[i][j] = min(d[i][j - 1], d[i + (1 << (j - 1))][j - 1]); 80 } 81 } 82 int p = -1; 83 rep_up1(i, n) { 84 if ((i & (i - 1)) == 0) p++; 85 t[i] = p; 86 } 87 } 88 void Init_max(int a[], int n) { 89 rep_up0(i, n) d[i][0] = a[i]; 90 for (int j = 1; (1 << j) <= n; j++) { 91 for (int i = 0; i + (1 << j) - 1 < n; i++) { 92 d[i][j] = max(d[i][j - 1], d[i + (1 << (j - 1))][j - 1]); 93 } 94 } 95 int p = -1; 96 rep_up1(i, n) { 97 if ((i & (i - 1) == 0)) p++; 98 t[i] = p; 99 } 100 } 101 int RMQ_min(int L, int R) { 102 int p = t[R - L + 1]; 103 return min(d[L][p], d[R - (1 << p) + 1][p]); 104 } 105 int RMQ_max(int L, int R) { 106 int p = t[R - L + 1]; 107 return max(d[L][p], d[R - (1 << p) + 1][p]); 108 } 109 }; 110 111 SparseTable st; 112 113 struct Graph { 114 vector<vector<int> > G; 115 void clear() { G.clear(); } 116 void resize(int n) { G.resize(n + 2); } 117 void add(int u, int v) { G[u].push_back(v); } 118 vector<int> & operator [] (int u) { return G[u]; } 119 }; 120 Graph G; 121 122 int dfs_clock; 123 int a[maxn], b[maxn], r[maxn]; 124 125 void dfs(int u, int fa) { 126 r[u] = dfs_clock; 127 a[dfs_clock ++] = u; 128 int sz = G[u].size(); 129 rep_up0(i, sz) { 130 int v = G[u][i]; 131 if (fa != v) { 132 dfs(v, u); 133 a[dfs_clock ++] = u; 134 } 135 } 136 } 137 138 int LCA(int u, int v) { 139 if (r[u] > r[v]) swap(u, v); 140 return a[st.RMQ_min(r[u], r[v])]; 141 } 142 143 bool chk(int u, int v, int w) { 144 int pos = LCA(u, v); 145 return (LCA(u, w) == w || LCA(v, w) == w) && LCA(pos, w) == pos; 146 } 147 148 int main() { 149 //freopen("in.txt", "r", stdin); 150 int n, q; 151 while (cin >> n >> q) { 152 G.clear(); 153 G.resize(n); 154 rep_up0(i, n - 1) { 155 int u, v; 156 sint2(u, v); 157 G.add(u, v); 158 G.add(v, u); 159 } 160 dfs_clock = 0; 161 dfs(1, 0); 162 rep_up0(i, dfs_clock) b[i] = r[a[i]]; 163 st.Init_min(b, dfs_clock); 164 rep_up0(i, q) { 165 int u, v, w; 166 sint3(u, v, w); 167 puts(chk(u, v, w)? "YES" : "NO"); 168 } 169 cout << endl; 170 } 171 return 0; 172 }