1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#ifdef LOC
#include "debuglib.hpp"
#else
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define deb(...)
#define DBP(...)
#endif
using namespace std;
using   ll         = long long;
using   vi         = vector<int>;
using   pii        = pair<int, int>;
#define pb           push_back
#define mp           make_pair
#define x            first
#define y            second
#define rep(i, b, e) for (int i = (b); i < (e); i++)
#define each(a, x)   for (auto& a : (x))
#define all(x)       (x).begin(), (x).end()
#define sz(x)        int((x).size())

constexpr ll INF = ll(1e18);

struct StaticPairSet {
	vector<pair<ll, ll>> S;

	void add(ll a, ll b) {
		S.pb({a, b});
		S.pb({b, a});
	}

	void build() {
		auto tmp = move(S);
		sort(all(tmp), [](const auto& l, const auto& r) { return l.x > r.x; });
		S = { {INF, -1} };
		each(p, tmp) {
			if (p.y > S.back().y) {
				if (p.x == S.back().x) {
					S.back().y = p.y;
				} else {
					S.pb(p);
				}
			}
		}
	}

	bool query(ll a, ll b) const {
		auto it = prev(lower_bound(all(S), a-1, [](const auto& l, ll r) { return l.x > r; }));
		return it->y >= b;
	}
};

struct DynamicPairSet {
	set<pair<ll, ll>> S = { {-1, INF}, {INF, -1} };

	void addOrdered(ll a, ll b) {
		auto [it, inserted] = S.insert({a, b});
		if (!inserted) return;

		if (b <= next(it)->y) {
			S.erase(it);
			return;
		}

		auto prv = prev(it);
		while (prv->y <= it->y) {
			S.erase(prv);
			prv = prev(it);
		}
	}

	void add(ll a, ll b) {
		addOrdered(a, b);
		addOrdered(b, a);
	}

	bool query(ll a, ll b) const {
		return S.lower_bound({a, 0})->y >= b;
	}
};

template<size_t D>
void insertMax(array<ll, D>& mx, ll val) {
	if (val <= mx[D-1]) return;
	for (int i = D-2; i >= 0; i--) {
		if (val <= mx[i]) {
			mx[i+1] = val;
			return;
		}
		mx[i+1] = mx[i];
	}
	mx[0] = val;
}

template<size_t D>
ll otherKth(const array<ll, D>& mx, ll val, int k) {
	rep(i, 0, k+1) {
		if (mx[i] == val) {
			return mx[k+1];
		}
	}
	return mx[k];
}

// ------------------------------------------------------------------------------------------------

struct Edge {
	int to;
	ll w;
	ll depth = 0; // Depth into subtree (excluding edge)
	ll single = 0; // Longest path in subtree (not using edge)

	ll depthInclusive() const { return w + depth; }
	ll singleInclusive() const { return max(single, depthInclusive()); }
};

vector<vector<Edge>> G;
vector<array<ll, 3>> queries;
vector<bool> ans;

ll diameterLength;
vi diameterVerts, indexInDiameter;
vector<ll> diameterWeights;

// Preprocessing + case 0: <=2 paths needed
namespace common {
	pair<ll, ll> dfs1(int v, int p) {
		array<ll, 2> maxDepth = {0, 0};
		ll maxSingle = 0;
		each(e, G[v]) {
			if (e.to != p) {
				tie(e.depth, e.single) = dfs1(e.to, v);
				insertMax(maxDepth, e.depthInclusive());
				maxSingle = max(maxSingle, e.singleInclusive());
			}
		}
		return {maxDepth[0], max(maxSingle, maxDepth[0]+maxDepth[1])};
	}

	void dfs2(int v, Edge p, bool assign, const auto& processCut) {
		array<ll, 3> maxDepth = {p.depthInclusive(), 0, 0};
		array<ll, 2> maxSingle = {p.singleInclusive(), 0};

		each(e, G[v]) {
			if (e.to != p.to) {
				insertMax(maxDepth, e.depthInclusive());
				insertMax(maxSingle, e.singleInclusive());
			} else if (assign) {
				e = p;
			}
		}

		each(e, G[v]) {
			if (e.to != p.to) {
				ll d = otherKth(maxDepth, e.depthInclusive(), 0);
				ll s = max(otherKth(maxSingle, e.singleInclusive(), 0), d + otherKth(maxDepth, e.depthInclusive(), 1));
				Edge f = {v, e.w, d, s};
				processCut(e.single, f.singleInclusive());
				processCut(f.single, e.singleInclusive());
				dfs2(e.to, f, assign, processCut);
			}
		}
	}

	void iterSubgraphCuts(Edge e, int v, const auto& processCut) {
		Edge f = {v, e.w, 0, 0};
		processCut(e.single, f.singleInclusive());
		processCut(f.single, e.singleInclusive());
		dfs2(e.to, f, false, processCut);
	}

	void findDiameter() {
		rep(v, 0, sz(G)) {
			each(e, G[v]) {
				ll alt = e.depthInclusive();
				if (alt > diameterLength) {
					diameterLength = alt;
					diameterVerts = {v, e.to};
					diameterWeights = {e.w};
				}
			}
		}

		bool ok = 1;
		ll remain = diameterLength - diameterWeights[0];
		while (ok) {
			int p = diameterVerts[sz(diameterVerts)-2];
			int v = diameterVerts.back();
			ok = 0;
			each(e, G[v]) {
				if (e.to != p && e.depthInclusive() == remain) {
					diameterVerts.pb(e.to);
					diameterWeights.pb(e.w);
					remain -= e.w;
					ok = 1;
					break;
				}
			}
		}

		indexInDiameter.assign(sz(G), -1);
		rep(i, 0, sz(diameterVerts)) {
			indexInDiameter[diameterVerts[i]] = i;
		}
	}

	void solve() {
		StaticPairSet pairs;
		dfs1(0, -1);
		dfs2(0, {-1, 0}, true, [&](ll a, ll b) { pairs.add(a, b); });
		pairs.build();
		findDiameter();
		rep(i, 0, sz(queries)) {
			auto [a, b, c] = queries[i];
			ans[i] = a+b+c <= diameterLength || pairs.query(a+b, c) || pairs.query(a+c, b) || pairs.query(b+c, a);
		}
	}
};

// Case 1: One of the paths is diameter
namespace case1 {
	void solve() {
		StaticPairSet pairs;
		array<ll, 2> maxSingle = {0, 0};
		each(v, diameterVerts) {
			each(e, G[v]) {
				if (indexInDiameter[e.to] == -1) {
					insertMax(maxSingle, e.singleInclusive());
					common::iterSubgraphCuts(e, v, [&](ll a, ll b) { pairs.add(a, b); });
				}
			}
		}
		pairs.add(maxSingle[0], maxSingle[1]);
		pairs.build();
		rep(i, 0, sz(queries)) {
			auto [a, b, c] = queries[i];
			ans[i] = ans[i] ||
				(a <= diameterLength && pairs.query(b, c)) ||
				(b <= diameterLength && pairs.query(a, c)) ||
				(c <= diameterLength && pairs.query(a, b));
		}
	}
};

// Case 2: Sandwiched path is not "half-diameter" (or there is no sandwiched path)
namespace case2 {
	struct Node {
		ll single = 0, depthLeft = 0, depthRight = 0, length = 0;
	};

	vector<ll> prefixBest, suffixBest;
	vector<Node> tree;
	int len;

	Node merge(const Node& l, const Node& r) {
		return {
			max(max(l.single, r.single), l.depthRight+r.depthLeft),
			max(l.depthLeft, l.length+r.depthLeft),
			max(r.depthRight, r.length+l.depthRight),
			l.length+r.length,
		};
	}

	Node queryTree(int vb, int ve, int i, int b, int e) {
		if (b >= ve || vb >= e) return {};
		if (b >= vb && e <= ve) return tree[i];
		int m = (b+e) / 2;
		return merge(queryTree(vb, ve, i*2, b, m), queryTree(vb, ve, i*2+1, m, e));
	}

	Node queryTree(int vb, int ve) {
		if (vb >= ve) return {};
		return queryTree(vb, ve, 1, 0, len);
	}

	void build() {
		int k = sz(diameterVerts);
		len = 1;
		while (len < k*2) len *= 2;
		prefixBest.resize(k*2-1);
		suffixBest.resize(k*2-1);
		tree.resize(len*2);

		ll prefix = 0;

		rep(i, 0, k) {
			int v = diameterVerts[i];
			array<ll, 2> maxDepth = {0, 0};
			ll maxSingle = 0;

			each(e, G[v]) {
				if (indexInDiameter[e.to] == -1) {
					insertMax(maxDepth, e.depthInclusive());
					maxSingle = max(maxSingle, e.singleInclusive());
				}
			}

			maxSingle = max(maxSingle, maxDepth[0]+maxDepth[1]);
			prefixBest[i*2] = prefix + maxDepth[0];
			suffixBest.rbegin()[i*2] = diameterLength - prefix + maxDepth[0];
			tree[len+i*2] = {maxSingle, maxDepth[0], maxDepth[0], 0};

			if (i < sz(diameterWeights)) {
				ll w = diameterWeights[i];
				prefixBest[i*2+1] = prefix + w;
				suffixBest.rbegin()[i*2+1] = diameterLength - prefix;
				tree[len+i*2+1] = {w, w, w, w};
				prefix += w;
			}
		}

		rep(i, 1, sz(prefixBest)) {
			prefixBest[i] = max(prefixBest[i], prefixBest[i-1]);
			suffixBest[i] = max(suffixBest[i], suffixBest[i-1]);
		}
		for (int i = len-1; i > 0; i--) {
			tree[i] = merge(tree[i*2], tree[i*2+1]);
		}
	}

	bool query(ll a, ll b, ll c) {
		int prefix = int(lower_bound(all(prefixBest), a) - prefixBest.begin());
		int suffix = int(lower_bound(all(suffixBest), b) - suffixBest.begin());
		bool ret = queryTree(prefix+1, sz(suffixBest)-suffix-1).single >= c;
		return ret;
	}

	void solve() {
		build();
		rep(i, 0, sz(queries)) {
			auto [a, b, c] = queries[i];
			ans[i] = ans[i] ||
				query(a, b, c) || query(a, c, b) ||
				query(b, a, c) || query(b, c, a) ||
				query(c, a, b) || query(c, b, a);
		}
	}
};

// Case 3: Sandwiched path is "half-diameter"
namespace case3 {
	struct Event {
		int idx; // -1 = add, otherwise query index
		ll a; // half-diameter length
		ll b, c;
		bool operator<(const Event& r) const {
			return mp(-a, idx) < mp(-r.a, r.idx);
		}
	};

	vector<Event> events;

	void dfs(Edge v, int p, ll len, array<ll, 2> upSingle) {
		auto tmp = upSingle;
		insertMax(tmp, v.single);
		events.pb({-1, len, tmp[0], tmp[1]});

		array<ll, 3> maxSingle = {upSingle[0], upSingle[1], 0};
		each(e, G[v.to]) {
			if (e.to != p) {
				insertMax(maxSingle, e.singleInclusive());
			}
		}

		each(e, G[v.to]) {
			if (e.to != p) {
				ll a = otherKth(maxSingle, e.singleInclusive(), 0);
				ll b = otherKth(maxSingle, e.singleInclusive(), 1);
				dfs(e, v.to, len+e.w, {a, b});
			}
		}
	}

	void solve(int v) {
		events.clear();
		dfs(G[v][0], v, G[v][0].w, {0, 0});
		rep(i, 0, sz(queries)) {
			auto [a, b, c] = queries[i];
			events.pb({i, a, b, c});
			events.pb({i, b, a, c});
			events.pb({i, c, a, b});
		}
		DynamicPairSet pairs;
		sort(all(events));
		each(e, events) {
			if (e.idx == -1) {
				pairs.add(e.b, e.c);
			} else {
				ans[e.idx] = ans[e.idx] || pairs.query(e.b, e.c);
			}
		}
	}

	void solve() {
		solve(diameterVerts[0]);
		solve(diameterVerts.back());
	}
};

int main() {
	cin.sync_with_stdio(0); cin.tie(0);

	int n, q;
	cin >> n >> q;

	G.resize(n);
	queries.resize(q);
	ans.resize(q);

	rep(i, 0, n-1) {
		int u, v, w;
		cin >> u >> v >> w;
		u--; v--;
		G[u].pb({v, w});
		G[v].pb({u, w});
	}

	each(e, queries) {
		cin >> e[0] >> e[1] >> e[2];
	}

	common::solve();
	case1::solve();
	case2::solve();
	case3::solve();

	for (bool a : ans) {
		cout << (a ? "TAK\n" : "NIE\n");
	}
}