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
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

/////////////////////////////////////////////////////////////////// begin copied code

#define FOREACH(i,x) for (__typeof((x).begin()) i=(x).begin(); i!=(x).end(); ++i)
#define REP(i,n) for(int i=0;i<(n);++i)

namespace PushRelabel {

// MAKSYMALNY PRZEPLYW O(V^3) (algorytm Push-Relabel)
// Adam Polak

const int N = 100*1000;

struct Edge {
    int v,cap,flow;
    int back_ind;
    Edge *back;
    Edge(int vi, int ci):v(vi),cap(ci){}
};

/* Usage:
   1) n=...; s=...; t=...;
   2) REP(i,n) g[i].clear();
   3) add_edge(...);
   4) compute_flow();
*/

int n,s,t;
int e[N],h[N];
vector<Edge> g[N];
vector<Edge>::iterator cur[N];

void bfs(int start, int start_h) {
    queue<int> q;
    h[start] = start_h;
    for(q.push(start);!q.empty();q.pop()) {
        int u = q.front();
        FOREACH(i,g[u])
            if (i->back->flow < i->back->cap && h[i->v]>h[u]+1) {
                    h[i->v] = h[u] + 1;
                    q.push(i->v);
            }
    }
}

int compute_flow() {
    queue<int> q;
    REP(i,n) {
        FOREACH(j,g[i]) {
            j->flow = 0;
            j->back = &g[j->v][j->back_ind];
        }
        cur[i] = g[i].begin();
        h[i] = e[i] = 0;
    }
    FOREACH(i,g[s]) {
        i->flow = i->cap;
        i->back->flow = -i->flow;
        if (e[i->v]==0 && i->v!=t) q.push(i->v);
        e[i->v] += i->flow;
    }
    h[s] = n;
    int relabel_counter = 0;
    for(;!q.empty();q.pop()) {
        int u = q.front();
        while (e[u]>0) {
            if (cur[u]==g[u].end()) { // relabel
                relabel_counter++;
                h[u] = 2*n+1;
                FOREACH(i,g[u]) if(i->flow < i->cap) h[u]=min(h[u],1+h[i->v]);
                cur[u] = g[u].begin(); 
                continue; 
            }
            if (cur[u]->flow < cur[u]->cap && h[u]==h[cur[u]->v]+1) { // push
                int d = min(e[u], cur[u]->cap - cur[u]->flow);
                cur[u]->flow += d;
                cur[u]->back->flow -= d;
                e[u] -= d;
                e[cur[u]->v] += d;
                if (e[cur[u]->v]==d && cur[u]->v!=t && cur[u]->v!=s) q.push(cur[u]->v);
            } else cur[u]++; 
        }
        if (relabel_counter >= n) { 
            REP(i,n) h[i]=2*n+1;
            bfs(t,0);
            bfs(s,n);
            relabel_counter = 0;
        }
    }
    return e[t];
}

void add_edge(int a, int b, int c, int c_back=0) {
    g[a].push_back(Edge(b,c));
    g[b].push_back(Edge(a,c_back));
    g[a].back().back_ind = g[b].size()-1;
    g[b].back().back_ind = g[a].size()-1;
}

};
/////////////////////////////////////////////////////////////////// end copied code

int main() {
  int n;
  int m;
  scanf("%d%d", &n, &m);

  vector<int> p(n), k(n), c(n);
  vector<int> times;
  for (int i = 0; i < n; ++i) {
    scanf("%d%d%d", &p[i], &k[i], &c[i]);
    times.push_back(p[i]);
    times.push_back(k[i]);
  }
  sort(times.begin(), times.end());
  times.resize(unique(times.begin(), times.end()) - times.begin());

  // Vertices:
  // 0                   - source
  // 1..n                - tasks
  // n+1                 - target
  // n+2..n+times.size() - time slices
  PushRelabel::n = n + times.size() + 1;
  PushRelabel::s = 0;
  PushRelabel::t = n + 1;

  // Edges:
  // source -> time_slices - computing capacity
  // time_slices -> tasks  - assignable capacity
  // tasks -> target       - required computing
  for (int i = 1; i < times.size(); ++i) PushRelabel::add_edge(0, n + 1 + i, m * (times[i] - times[i - 1]));
  for (int i = 1; i < times.size(); ++i) for (int j = 0; j < n; ++j) if (p[j] <= times[i - 1] && times[i] <= k[j]) PushRelabel::add_edge(n + 1 + i, j + 1, times[i] - times[i - 1]);
  for (int i = 0; i < n; ++i) PushRelabel::add_edge(i + 1, n + 1, c[i]);

  int flow = PushRelabel::compute_flow();
  for (int i = 0; i < n; ++i) flow -= c[i];

  /*
  if (flow == 0) {
    for (int i = 1; i < times.size(); ++i) for (int j = 0; j < PushRelabel::g[n + 1 + i].size(); ++j) {
      const int to = PushRelabel::g[n + 1 + i][j].v;
      const int flow = PushRelabel::g[n + 1 + i][j].flow;
      if (to == 0) continue;
      if (flow == 0) continue;
      cout << to - 1 << ' ' << times[i - 1] << ' ' << times[i] << ' ' << flow << endl;;
      Nie
    }
  }
  */

  printf(flow == 0 ? "TAK\n" : "NIE\n");

  return 0;
}