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
//Author: Kevin
#include<bits/stdc++.h>
//#pragma GCC optimize("O2")
using namespace std;
#define pb emplace_back
#define mp make_pair
#define ALL(x) (x).begin(),(x).end()
#define rALL(x) (x).rbegin(),(x).rend()
#define srt(x) sort(ALL(x))
#define rev(x) reverse(ALL(x))
#define rsrt(x) sort(rALL(x))
#define sz(x) (int)(x.size())
#define inf 0x3f3f3f3f
#define lb(v,x) (int)(lower_bound(ALL(v),x)-v.begin())
#define ub(v,x) (int)(upper_bound(ALL(v),x)-v.begin())
#define uni(v) v.resize(unique(ALL(v))-v.begin())
using ll=long long;
using ull=unsigned long long;
using pii=pair<int,int>;
using i128=__int128_t;
void die(string S){puts(S.c_str());exit(0);}
#define ls (x<<1)
#define rs (ls|1)
int n,q;
vector<pair<int,ll>> G[200200];
ll a[200200],b[200200],c[200200];
int ans[200200];
int ban[200200];
pair<ll,int> find_farthest(int u,int fa,ll d)
{
	pair<ll,int> ans(d,u);
	for(auto [v,w]:G[u])
		if(v!=fa&&!ban[v])
			ans=max(ans,find_farthest(v,u,d+w));
	return ans;
}
ll f[200200],f2[200200];
ll tmp_fe[200200];
void dfs1(int u,int fa)
{
	ll ln=0,ln2=0,an2=0;
	for(auto [v,w]:G[u])
		if(v!=fa&&!ban[v])
		{
			tmp_fe[v]=w;
			dfs1(v,u);
			ll d1=f[v];
			ll d2=f2[v];
			an2=max(an2,d2);
			ll tmp=d1+w;
			if(tmp>ln) swap(tmp,ln);
			if(tmp>ln2) swap(tmp,ln2);
		}
	f[u]=ln;
	f2[u]=max(an2,ln+ln2);
}
vector<array<ll,3>> vect;
vector<pair<ll,ll>> pl2;
void dfs2(int u,int fa,ll d,array<ll,2> mx)
{
	array<ll,2> pr=mx;
	ll tv=f2[u];
	for(int a=0;a<2;a++)
		if(tv>pr[a])
			swap(tv,pr[a]);
	array<ll,3> cand={d,pr[0],pr[1]};
	pl2.pb(min(d,pr[0]),max(d,pr[0]));
	srt(cand);
	vect.pb(cand);
	ll val[3]={0,0,0};
	for(auto [v,w]:G[u])
		if(v!=fa)
		{
			ll tmp=max(f[v]+w,f2[v]);
			for(int a=0;a<3;a++)
				if(tmp>val[a])
					swap(tmp,val[a]);
		}
	for(auto [v,w]:G[u])
		if(v!=fa)
		{
			ll tmp=max(f[v]+w,f2[v]);
			array<ll,2> pr=mx;
			for(int i=0,fd=0;i<3;i++)
				if(tmp==val[i]&&!fd)
					fd=1;
				else
				{
					ll tv=val[i];
					for(int a=0;a<2;a++)
						if(tv>pr[a])
							swap(tv,pr[a]);
				}
			dfs2(v,u,d+w,pr);
		}
}
void dfs3(int u,int fa,ll d,ll mx,ll diam)
{
	array<ll,3> cand={d,diam,max(mx,f2[u])};
	srt(cand);
	vect.pb(cand);
	ll val[2]={0,0};
	for(auto [v,w]:G[u])
		if(v!=fa&&!ban[v])
		{
			ll tmp=max(f[v]+w,f2[v]);
			for(int a=0;a<2;a++)
				if(tmp>val[a])
					swap(tmp,val[a]);
		}
	for(auto [v,w]:G[u])
		if(v!=fa&&!ban[v])
		{
			ll tmp=max(f[v]+w,f2[v]);
			dfs3(v,u,d+w,max(mx,(tmp==val[0])?(val[1]):(val[0])),diam);
		}
}
vector<int> path;
int on_path[200200];
int get_path(int u,int fa,int t)
{
	path.pb(u);
	if(u==t) return 1;
	for(auto [v,w]:G[u])
		if(v!=fa)
			if(get_path(v,u,t))
				return 1;
	path.pop_back();
	return 0;
}
void set_ban(int u,int fa,int vv)
{
	ban[u]=vv;
	for(auto [v,w]:G[u])
		if(v!=fa)
			set_ban(v,u,vv);
}
vector<pair<array<ll,3>,int>> pl;
template<typename T> vector<T> merge(vector<T> va,vector<T> vb)
{
	int sa=sz(va);
	int sb=sz(vb);
	for(auto b:vb) va.pb(b);
	inplace_merge(va.begin(),va.begin()+sa,va.end(),greater<T>());
	return va;
}
pair<vector<pair<ll,ll>>,vector<pair<pair<ll,ll>,int>>> calc(int l,int r)
{
	if(l>=r)
	{
		vector<pair<ll,ll>> pt;
		vector<pair<pair<ll,ll>,int>> vq;
		if(pl[l].second==inf)
			pt.pb(pl[l].first[1],pl[l].first[2]);
		else
		{
			int id=pl[l].second;
			vq.pb(pair<ll,ll>(b[id],c[id]),id);
		}
		return mp(pt,vq);
	}
	int mid=(l+r)/2;
	auto [pt2,vq]=calc(l,mid);
	auto [pt,vq2]=calc(mid+1,r);
	int po=0;
	ll mx=0;
	for(auto [pr,id]:vq)
	{
		auto [vb,vc]=pr;
		while(po<sz(pt)&&pt[po].first>=vb)
			mx=max(mx,pt[po++].second);
		if(mx>=vc)
			ans[id]=1;
	}
	return mp(merge(pt,pt2),merge(vq,vq2));
}
ll g1[200200],g2[200200];
ll gg1[200200],gg2[200200];
ll h1[200200],h2[200200],h3[200200];
ll dst[200200];
void dfs4(int u,int fa)
{
	for(auto [v,w]:G[u])
		if(v!=fa)
		{
			dst[v]=dst[u]+w;
			dfs4(v,u);
		}
}
struct info_t
{
	ll v1,v2,v3,v12;
}seg[200200<<2];
info_t merge(const info_t &a,const info_t &b)
{
	return {max(a.v1,b.v1),max(a.v2,b.v2),max(a.v3,b.v3),max({a.v12,b.v12,a.v1+b.v2})};
}
void modify(int x,int l,int r,int p,const info_t &t)
{
	if(l==r)
	{
		seg[x]=t;
		return ;
	}
	int mid=(l+r)/2;
	if(p<=mid) modify(ls,l,mid,p,t);
	else modify(rs,mid+1,r,p,t);
	seg[x]=merge(seg[ls],seg[rs]);
}
info_t query(int x,int l,int r,int ql,int qr)
{
	if(l==ql&&r==qr) return seg[x];
	int mid=(l+r)/2;
	if(qr<=mid) return query(ls,l,mid,ql,qr);
	if(ql>mid) return query(rs,mid+1,r,ql,qr);
	return merge(query(ls,l,mid,ql,mid),query(rs,mid+1,r,mid+1,qr));
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>q;
	for(int i=1;i<n;i++)
	{
		int u,v;
		ll w;
		cin>>u>>v>>w;
		G[u].pb(v,w);
		G[v].pb(u,w);
	}
	for(int i=1;i<=q;i++)
		cin>>a[i]>>b[i]>>c[i];
	auto [dp,up]=find_farthest(1,0,0);
	auto [dq,uq]=find_farthest(up,0,0);
	for(int i=1;i<=q;i++)
		if(a[i]+b[i]+c[i]<=dq)
			ans[i]=1;
	assert(get_path(up,0,uq));
	for(auto x:path)
		on_path[x]=1;
	vector<int> cand={up,uq};
	for(auto x:cand)
	{
		dfs1(x,0);
		dfs2(x,0,0,{0,0});
		if(x==up)
			for(auto y:path)
			{
				g1[y]=max(tmp_fe[y]+f[y],f2[y]);
				gg1[y]=f2[y];
			}
		else
			for(auto y:path)
			{
				g2[y]=max(tmp_fe[y]+f[y],f2[y]);
				gg2[y]=f2[y];
			}
	}
	for(int i=1;i<=n;i++)
		ban[i]=1;
	for(auto x:path)
		for(auto [y,w]:G[x])
			if(!on_path[y])
			{
				ban[x]=0;
				set_ban(y,x,0);
				ll ln=f[y]+w;
				if(ln>h1[x]) swap(ln,h1[x]);
				if(ln>h2[x]) swap(ln,h2[x]);
				h3[x]=max(h3[x],f2[y]);
				auto [ndp,nup]=find_farthest(x,0,0);
				auto [ndq,nuq]=find_farthest(nup,0,0);
				vector<int> cand={nup,nuq};
				for(auto nx:cand)
				{
					dfs1(nx,0);
					dfs3(nx,0,0,0,dq);
				}
				ban[x]=1;
				set_ban(y,x,1);
			}
	for(int i=1;i<=n;i++)
		ban[i]=0;
	for(auto arr:vect)
		pl.pb(arr,inf);
	for(int i=1;i<=q;i++)
		pl.pb(array<ll,3>{a[i],b[i],c[i]},i);
	srt(pl);
	calc(0,sz(pl)-1);
	vector<pair<pair<ll,ll>,int>> vq2;
	for(int i=1;i<=q;i++)
	{
		vq2.pb(mp(min(a[i]+b[i],c[i]),max(a[i]+b[i],c[i])),i);
		vq2.pb(mp(b[i],a[i]+c[i]),i);
		vq2.pb(mp(a[i],b[i]+c[i]),i);
	}
	rsrt(pl2);
	rsrt(vq2);
	int po=0;
	ll mx=0;
	for(auto [pr,id]:vq2)
	{
		auto [av,bv]=pr;
		while(po<sz(pl2)&&pl2[po].first>=av)
			mx=max(mx,pl2[po++].second);
		if(mx>=bv)
			ans[id]=1;
	}
	dfs4(up,0);
	for(int i=0;i<sz(path);i++)
	{
		int u=path[i];
		modify(1,0,sz(path)-1,i,info_t{h1[u]-dst[u],h1[u]+dst[u],max(h1[u]+h2[u],h3[u]),-1ll*inf*inf});
	}
	for(int rd=0;rd<6;rd++)
	{
		for(int i=1;i<=q;i++)
		{
			int p1,p2;
			{
				int l=0,r=sz(path);
				while(l<r)
				{
					int mid=(l+r)/2;
					if(g2[path[mid]]<a[i])
						l=mid+1;
					else
						r=mid;
				}
				p1=l;
			}
			{
				int l=-1,r=sz(path)-1;
				while(l<r)
				{
					int mid=(l+r+1)/2;
					if(g1[path[mid]]<b[i])
						r=mid-1;
					else
						l=mid;
				}
				p2=l;
			}
			int p3,p4;
			{
				int l=0,r=sz(path);
				while(l<r)
				{
					int mid=(l+r)/2;
					if(gg2[path[mid]]<a[i])
						l=mid+1;
					else
						r=mid;
				}
				p3=l;
			}
			{
				int l=-1,r=sz(path)-1;
				while(l<r)
				{
					int mid=(l+r+1)/2;
					if(gg1[path[mid]]<b[i])
						r=mid-1;
					else
						l=mid;
				}
				p4=l;
			}
			if(p1+1<=p2-1)
			{
				auto ret=query(1,0,sz(path)-1,p1+1,p2-1);
				ll mx=max({ret.v3,ret.v12});
				if(mx>=c[i])
					ans[i]=1;
			}
			if(p3<=p4)
				if(dst[path[p4]]-dst[path[p3]]>=c[i])
					ans[i]=1;
			if(p3+1<=p2-1)
			{
				auto ret=query(1,0,sz(path)-1,p3+1,p2-1);
				if(ret.v2-dst[path[p3]]>=c[i])
					ans[i]=1;
			}
			if(p1+1<=p4-1)
			{
				auto ret=query(1,0,sz(path)-1,p1+1,p4-1);
				if(ret.v1+dst[path[p4]]>=c[i])
					ans[i]=1;
			}
		}
		for(int i=1;i<=q;i++)
		{
			array<ll,3> tmp={a[i],b[i],c[i]};
			next_permutation(ALL(tmp));
			a[i]=tmp[0];
			b[i]=tmp[1];
			c[i]=tmp[2];
		}
	}
	for(int i=1;i<=q;i++)
		if(ans[i])
			cout<<"TAK\n";
		else
			cout<<"NIE\n";
	return 0;
}