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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
//#pragma GCC optimize("Ofast", "unroll-loops")
//#pragma GCC target("sse", "sse2", "sse3", "ssse3", "sse4")

#include <bits/stdc++.h>

#define all(a) a.begin(),a.end()
#define len(a) (int)(a.size())
#define mp make_pair
#define pb push_back
#define fi first
#define se second

using namespace std;

typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
template<class T>
using vec = vector<T>;

template<typename T>
bool umin(T &a, T b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}
template<typename T>
bool umax(T &a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

#ifdef KoRoVa
#define DEBUG for (bool _FLAG = true; _FLAG; _FLAG = false)
#define LOG(...) print(#__VA_ARGS__" ::", __VA_ARGS__) << endl
template <class ...Ts> auto &print(Ts ...ts) { return ((cerr << ts << " "), ...); }
#else
#define DEBUG while (false)
#define LOG(...)
#endif

const int max_n = 2e5 + 11, inf = 1000111222;

const ll linf = inf * 1ll * inf;

namespace seg {
    struct node {

        ll two, a, b, ans;

        /// not existing node
        node () : ans(-linf), a(-linf), b(-linf), two(-linf) {}

    };



    inline node pull (node a, node b) {
        node res;
        res.two = max(a.two, b.two);
        res.a = max(a.a, b.a);
        res.b = max(a.b, b.b);
        res.ans = max({a.ans, b.ans, a.a + b.b});
        return res;
    }

    struct segment_tree {
        vector <node> t;
        vector <node> a;
        int n;

        segment_tree () {}

        inline void build (int v, int tl, int tr) {
            if (tl == tr) {
                t[v] = a[tr]; /// think
                return;
            }
            int tm = (tl + tr) >> 1;
            build(v << 1, tl, tm);
            build(v << 1 | 1, tm + 1, tr);
            t[v] = pull(t[v << 1], t[v << 1 | 1]);
        }

        segment_tree (int n, vector <node> a) : n(n), a(a) {
            t.resize(4 * n);
            build(1, 0, n - 1);
        }

        inline void push (int v, int tl, int tr) {

        }



        inline node query (int v, int tl, int tr, int l, int r) {
            push(v, tl, tr);
            if (l > r) return node();
            if (tl == l && tr == r) {
                return t[v];
            }
            int tm = (tl + tr) >> 1;
            return pull(query(v << 1, tl, tm, l, min(r, tm)), query(v << 1 | 1, tm + 1, tr, max(tm + 1, l), r));
        }



        inline node get (int l, int r) {
            return query(1, 0, n - 1, l, r);
        }
    };

}


vector <pair<int, ll> > edge[max_n];


vector <int> path;
int n;


pair<ll, int> farthest (int v) {
    path.clear();
    vector <ll> d(n, -1);
    vector <int> pr(n, -1);
    d[v] = 0;
    queue <int> q;
    q.push(v);
    while (!q.empty()) {
        v = q.front();
        q.pop();
        for (auto [to, w] : edge[v]) {
            if (d[to] == -1) {
                d[to] = d[v] + w;
                pr[to] = v;
                q.push(to);
            }
        }
    }
    int mx = 0;
    for (int i = 0; i < n; i++) {
        if (d[i] > d[mx]) {
            mx = i;
        }
    }
    v = mx;
    while (d[v] > 0) {
        path.pb(v);
        v = pr[v];
    }
    path.pb(v);
    reverse(all(path));
    return make_pair(d[mx], mx);
}

void TAK () {
    cout << "TAK\n";
}

void NIE () {
    cout << "NIE\n";
}

ll second_mx = 0;

int used[max_n];

ll h[max_n], mx_path[max_n];
ll depth[max_n];

void dfs (int v, int p = -1) {
//    LOG(v);
    ll mx2 = 0;
    h[v] = 0;
    mx_path[v] = 0;
    for (auto &i: edge[v]) {
        int to = i.first;
        ll w = i.second;
        if (to == p) {
            continue;
        }
        depth[to] = depth[v] + w;
        dfs(to, v);
        if (h[v] < h[to] + w) {
            mx2 = h[v];
            h[v] = h[to] + w;
        }
        else if (mx2 < h[to] + w) {
            mx2 = h[to] + w;
        }
        umax(mx_path[v], mx_path[to]);
        if (!used[to]) {
            umax(second_mx, h[to] + w);
        }
    }
    if (!used[v]) {
        umax(second_mx, h[v] + mx2);
    }
    umax(mx_path[v], h[v] + mx2);
}



struct node {
    ll mx, mx2;

    node() : mx(0), mx2(0) {}

    void upd (ll x) {
        if (x > mx) {
            mx2 = mx;
            mx = x;
        }
        else if (x > mx2) {
            mx2 = x;
        }
    }

    void upd (node &a) {
        upd(a.mx);
        upd(a.mx2);
    }
};


ll s[3];

struct triple {
    ll a, b, c;
    bool operator < (const triple &x) const {
        return a < x.a;
    }
};

vector <triple> have;

void add_triple (ll a, ll b, ll c) {
    s[0] = a;
    s[1] = b;
    s[2] = c;
    sort(s, s + 3);
    have.pb(triple{s[0], s[1], s[2]});
}

vector <pair<ll, ll> > pp;

void add_double (ll a, ll b) {
    pp.pb({a, b});
}

void go (int v, ll d, node mx, int p = -1) {


    vector <node> res(len(edge[v]) + 1);

    for (int i = len(edge[v]) - 1; i >= 0; i--) {
        res[i] = res[i + 1];
        int to = edge[v][i].first;
        ll w = edge[v][i].second;
        if (to == p) {
            continue;
        }
        res[i].upd(max(h[to] + w, mx_path[to]));
    }

    {
//        LOG(v, d, mx.mx, mx.mx2, mx_path[v]);
        node now = mx;
        now.upd(mx_path[v]);
        add_triple(d, now.mx, now.mx2);
        add_double(d, now.mx);
    }

    {
        node now = mx;
        now.upd(res[0]);
        add_triple(d, now.mx, now.mx2);
        add_double(d, now.mx);
    }

    {
        vector <pair<ll, int> > heights, ins;
        heights.reserve(len(edge[v]));
        ins.reserve(len(edge[v]));
        for (auto [to, w] : edge[v]) {
            if (to == p) {
                continue;
            }
            heights.pb({h[to] + w, to});
            ins.pb({max(h[to] + w, mx_path[to]), to});
        }
        if (len(ins) > 3) {
            nth_element(ins.begin(), ins.begin() + 2,
                        ins.end(), greater<pair<ll, int> >());
            nth_element(heights.begin(), heights.begin() + 2,
                        heights.end(), greater<pair<ll, int> >());
            ins.resize(3);
            heights.resize(3);
        }
        sort(all(ins), greater<pair<ll, int> >());
        sort(all(heights), greater<pair<ll, int> >());

        for (auto &i : ins) {
            int pos = 0;
            while (pos < len(heights) && heights[pos].second == i.second) {
                ++pos;
            }
            int nxt_pos = pos + 1;
            while (nxt_pos < len(heights) && heights[nxt_pos].second == i.second) {
                ++nxt_pos;
            }

            ll rest = 0;
            if (pos < len(heights)) {
                rest += heights[pos].first;
            }
            if (nxt_pos < len(heights)) {
                rest += heights[nxt_pos].first;
            }
            add_triple(d, i.first, rest);
        }
    }

    node cur_pref = mx;
    for (int i = 0; i < len(edge[v]); i++) {
        int to = edge[v][i].first;
        ll w = edge[v][i].second;
        if (to == p) {
            continue;
        }
        node now = cur_pref;
        now.upd(res[i + 1]);
        go(to, d + w, now, v);

        cur_pref.upd(max(h[to] + w, mx_path[to]));
    }
}


ll D;


ll tmp_mx;
int tmp_ver;
int SP;

void find_furthest_again (int v, ll d, int p = -1) {
    if (umax(tmp_mx, d)) {
        tmp_ver = v;
    }
    for (auto [to, w] : edge[v]) {
        if (used[to] || to == p) {
            continue;
        }
        find_furthest_again(to, d + w, v);
    }
}


ll H[max_n];

ll MX_PATH[max_n];

void dfs_calc (int v, int p = -1) {
    if (v == SP) {
        H[v] = 0;
        MX_PATH[v] = 0;
        return;
    }
    node mx;
    for (auto [to, w] : edge[v]) {
        if (to == p || (used[to] && to != SP)) {
            continue;
        }
        dfs_calc(to, v);
        umax(H[v], H[to] + w);
        umax(MX_PATH[v], MX_PATH[to]);
        mx.upd(H[to] + w);
    }
    umax(MX_PATH[v], mx.mx + mx.mx2);
}

void go_win (int v, ll d, ll mx, int p = -1) {
    if (v == SP) {
        add_triple(D, d, mx);
        return;
    }
    vector <ll> suff(len(edge[v]) + 1);
    for (int i = len(edge[v]) - 1; i >= 0; i--) {
        suff[i] = suff[i + 1];
        int to = edge[v][i].first;
        ll w = edge[v][i].second;
        if (to == p || (used[to] && to != SP)) {
            continue;
        }
        umax(suff[i], max(H[to] + w, MX_PATH[to]));
    }

    {
        ll now = mx;
        umax(now, suff[0]);
        add_triple(D, d, now);
    }

    {
        ll now = mx;
        umax(now, MX_PATH[v]);
        add_triple(D, d, now);
    }

    ll pref = mx;
    for (int i = 0; i < len(edge[v]); i++) {
        int to = edge[v][i].first;
        ll w = edge[v][i].second;
        if (to == p || (used[to] && to != SP)) {
            continue;
        }
        ll now = pref;
        umax(now, suff[i + 1]);

        go_win(to, d + w, now, v);

        umax(pref, max(H[to] + w, MX_PATH[to]));
    }
}

void solve (int sp, int v, ll diam, ll gg) {
    SP = sp;
    D = diam;
    tmp_mx = -1;
    find_furthest_again(v, gg, SP);
    v = tmp_ver;
    dfs_calc(v);
    go_win(v, 0, 0);
}


ll pref[max_n][2];
ll suff[max_n][2];


template <class T>
struct fenwick {
public:
    int n;
    vector <T> t; /// !!!


    fenwick (int n) : n(n) {
        t.assign(n, T(0));
    }

    inline void upd (int i, T x) {
        for (; i < n; i = i | (i + 1)) umax(t[i], x);
    }

    inline T sum (int r) {
        T ans = 0;
        for (; r >= 0; r = (r & (r + 1)) - 1) umax(ans, t[r]);
        return ans;
    }

};

//struct dsu {
//public:
//    int n;
//    vector <int> p, cnt;
//
//    inline void make_set (int v) {
//        p[v] = v;
//    }
//
//    dsu (int n) : n(n) {
//        p.resize(n);
//        cnt.assign(n, 1);
//        for (int i = 0; i < n; i++) {
//            make_set(i);
//        }
//    }
//
//    inline int get (int v) {
//        if (p[v] == v) return v;
//        return p[v] = get(p[v]); /// compressing path
//    }
//
//    inline bool unite (int a, int b) {
//        a = get(a);
//        b = get(b);
//        if (a == b) return false;
//        if (cnt[a] > cnt[b]) {
//            swap(a, b);
//        }
//        p[a] = b;
//        cnt[b] += cnt[a];
//        return true;
//    }
//};




int main() {
//    freopen("132.in", "r", stdin);
//    freopen("output.txt", "w", stdout);

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int q;
    cin >> n >> q;
    ll c;
//    dsu cc(n);
//    LOG(n, q);
    for (int i = 1, a, b; i < n; i++) {
        cin >> a >> b >> c;
//        assert(1 <= a && a <= n);
//        assert(1 <= b && b <= n);
        --a, --b;
        edge[a].pb({b, c});
        edge[b].pb({a, c});
//        if (!cc.unite(a, b)) {
//            exit(4);
//        }
    }
//    LOG("hm");


    int A = farthest(0).second;
    ll diam;
    int B;
    tie(diam, B) = farthest(A);
//    LOG("hm");


    for (int v : path) {
        used[v] = 1;
    }
//    LOG("ok");

    dfs(A);

    int m = len(path);

//    node gg;
//    LOG("wow");

    for (int i = 0; i < m; i++) {
        int v = path[i];
        for (auto [to, w] : edge[v]) {
            if (!used[to]) {
                // solve that tree

                solve(v, to, diam, w);

//                gg.upd(max(h[to] + w, mx_path[to]));
            }


        }
    }
//    LOG("I am here");

//    add_triple(diam, gg.mx, gg.mx2);


    go(A, 0, node());

//    LOG("ok");


    dfs(B);

    go(B, 0, node());

//    LOG("hm");



    depth[A] = 0;
    dfs(A);
//    LOG("here");

    vector <seg::node> e(m);

    for (int i = 0; i < m; i++) {
        int v = path[i];
        umax(pref[i][0], depth[v]);
        umax(pref[i][1], depth[v]);
        node gg;
        for (auto [to, w] : edge[v]) {
            if (!used[to]) {
                umax(pref[i][1], depth[v] + w + h[to]);
                gg.upd(w + h[to]);
            }
        }
        e[i].two = gg.mx + gg.mx2;
        e[i].b = depth[v] + gg.mx;
        e[i].a = -depth[v] + gg.mx;
        if (i) {
            umax(pref[i][0], pref[i - 1][0]);
            umax(pref[i][0], pref[i - 1][1]);

            umax(pref[i][1], pref[i - 1][1]);
            umax(pref[i][1], pref[i - 1][0]);
        }
    }

    for (int i = m - 1; i >= 0; i--) {
        int v = path[i];

        umax(suff[i][0], depth[B] - depth[v]);
        umax(suff[i][1], depth[B] - depth[v]);


        for (auto [to, w] : edge[v]) {
            if (!used[to]) {
                umax(suff[i][1], depth[B] - depth[v] + w + h[to]);
            }
        }

        if (i + 1 < m) {
            umax(suff[i][0], suff[i + 1][0]);
            umax(suff[i][0], suff[i + 1][1]);

            umax(suff[i][1], suff[i + 1][1]);
            umax(suff[i][1], suff[i + 1][0]);
        }
    }
//    LOG("here");

    seg::segment_tree t(m, e);
    sort(all(pp));
    vector <pair<ll, ll> > new_pp;
    ll last = -1;
    reverse(all(pp));
    for (auto &i : pp) {
        if (umax(last, i.second)) {
            new_pp.pb(i);
        }
    }
    pp = new_pp;
    reverse(all(pp));


    auto check_two = [&] (ll x, ll y) -> bool {
        if (x <= diam && y <= second_mx) {
            return true;
        }
//        for (auto &i : pp) {
//            if (x <= i.first && y <= i.second) {
//                return true;
//            }
//        }
        int j = lower_bound(all(pp), make_pair(x, y)) - pp.begin();
        if (j == len(pp)) {
            return false;
        }
        if (pp[j].second >= y) {
            return true;
        }
        return false;
    };
    auto two = [&] (ll x, ll y) -> bool {
        return check_two(x, y) || check_two(y, x);
    };
    auto check_triple = [&] (ll a, ll b, ll c) {
        for (auto &i : have) {
//            LOG(i.a, i.b, i.c);
            if (a <= i.a && b <= i.b && c <= i.c) {
                return true;
            }
        }
        return false;
    };

    ll a, b;
    ll tmp[3];
    vector <int> ans(q);
    vector <vector <ll> > save;
    vector <ll> coords;
    coords.reserve(q + len(have));
    for (int i = 0; i < len(have); i++) {
        coords.pb(-have[i].b);
    }
    for (int i = 0; i < q; i++) {
        cin >> a >> b >> c;
//        if (i + 1 == 1141) {
//            LOG(a, b, c, a <= b, b <= c);
//        }
//        else {
//            continue;
//        }
        if (a + b + c <= diam) {
//            TAK();
            ans[i] = 1;
            continue;
        }
        if (two(a, b + c) || two(b, a + c) || two(c, a + b)) {
//            TAK();
            ans[i] = 1;
            continue;
        }

//        if (check_triple(a, b, c)) {
//            TAK();
//            ans[i] = 1;
//            continue;
//        }
//        LOG("here");

        tmp[0] = a;
        tmp[1] = b;
        tmp[2] = c;

        bool ok = false;
        do {
            if (ok) {
                break;
            }

            for (int x = 0; x < 2; x++) {
                if (pref[m - 1][x] < tmp[0]) {
                    continue;
                }

                int L = 0, R = m - 1;
                while (L < R) {
                    int mid = (L + R) >> 1;
                    if (pref[mid][x] >= tmp[0]) {
                        R = mid;
                    }
                    else {
                        L = mid + 1;
                    }
                }
                int posL = R;
                for (int y = 0; y < 2; y++) {
                    if (suff[0][y] < tmp[1]) {
                        continue;
                    }

                    L = 0, R = m - 1;
                    while (L < R) {
                        int mid = (L + R + 1) >> 1;
                        if (suff[mid][y] >= tmp[1]) {
                            L = mid;
                        }
                        else {
                            R = mid - 1;
                        }
                    }

                    int posR = R;

                    if (posL > posR) {
                        continue;
                    }

                    if (posL == posR && x == 1 && y == 1) {
                        continue;
                    }

                    ll now = depth[path[posR]] - depth[path[posL]];
                    int l = posL + x;
                    int r = posR - y;
                    auto res = t.get(l, r);
                    { // two
                        if (l <= r) {
                            umax(now, res.two);
                            umax(now, res.ans);
                        }
                    }
//                    LOG(now, res.a, l, r, e[l].a);
                    if (x) {
                        umax(now, res.b - depth[path[posL]]);
                    }

                    if (y) {
                        umax(now, res.a + depth[path[posR]]);
                    }



//                    LOG(tmp[0], tmp[1], tmp[2], A, B, path[posL], path[posR], now);
                    if (tmp[2] <= now) {
                        ok = true;
                    }

                }
            }
        } while (next_permutation(tmp, tmp + 3));

        if (ok) {
//            TAK();
            ans[i] = 1;
            continue;
        }

        save.pb({a, b, c, i});
        coords.pb(-b);

//        NIE();
    }
    sort(all(save));
    reverse(all(save));
    int j = 0;
    sort(all(have));
    reverse(all(have));

    sort(all(coords));
    coords.erase(unique(all(coords)), coords.end());

    auto cmp = [&] (ll x) -> int {
        return lower_bound(all(coords), x) - coords.begin();
    };

    fenwick <ll> T(len(coords));
    for (auto &i : save) {
        while (j < len(have) && have[j].a >= i[0]) {
            int pos = cmp(-have[j].b);
            T.upd(pos, have[j].c);
            ++j;
        }
        int pos = cmp(-i[1]);
        ll res = T.sum(pos);
        if (res >= i[2]) {
            ans[i[3]] = 1;
        }
    }
    for (auto &i : ans) {
        if (i) {
            TAK();
        }
        else {
            NIE();
        }
    }

}

/*
KoRoVa!
*/