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
#include <cstdio>
#include <cstdlib>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <climits>
#include <queue>
#define NIL 0
#define INF INT_MAX
using namespace std;

struct Task {
  int p, k, c;
};
Task tasks[100 + 2];
vector<int> task_starts[1000000 + 2];

int n, m;
vector<int> U, V;
vector<int> G[1000000 + 2];
int Pair_U[1000000 + 2];
int Pair_V[1000000 + 2];
int Dist[1000000 + 2];

bool bfs() {
  queue<int> Q;

  for (int u : U) {
    if (Pair_U[u] == NIL) {
      Dist[u] = 0;
      Q.push(u);
    } else {
      Dist[u] = INF;
    }
  }

  Dist[NIL] = INF;

  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();

    if (Dist[u] < Dist[NIL]) {
      for (int v : G[u]) {
        if (Dist[Pair_V[v]] == INF) {
          Dist[Pair_V[v]] = Dist[u] + 1;
          Q.push(Pair_V[v]);
        }
      }
    }
  }

  return Dist[NIL] != INF;
}

bool dfs(int u) {
  if (u != NIL) {
    for (int v : G[u]) {
      if (Dist[Pair_V[v]] == Dist[u] + 1) {
        if (dfs(Pair_V[v])) {
          Pair_V[v] = u;
          Pair_U[u] = v;
          return true;
        }
      }
    }
    Dist[u] = INF;
    return false;
  }
  return true;
}

int HopcroftKarp() {
  for (int u : U) {
    Pair_U[u] = NIL;
  }
  for (int v : V) {
    Pair_V[v] = NIL;
  }

  int matching = 0;

  while (bfs()) {
    for (int u : U) {
      if (Pair_U[u] == NIL) {
        if (dfs(u)) {
          matching++;
        }
      }
    }
  }

  return matching;
}

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

  set<int> U_set, V_set;
  for (int i = 0; i < m; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    G[a].push_back(b);
    G[b].push_back(a);

    U_set.insert(a);
    V_set.insert(b);
  }

  for (int u : U_set) {
    U.push_back(u);
  }
  for (int v : V_set) {
    V.push_back(v);
  }

  printf("%d\n", HopcroftKarp());
}

void heuristic() {
  for (int i = 0; i < n; i++) {
    task_starts[tasks[i].p].push_back(i);
  }

  set<int> active_tasks, cpus;
  int tasks_processed = 0;
  int time = 0;

  while (tasks_processed < n) {
    // Recalculate c_i for all currently processed tasks.
    for (int task_idx : cpus) {
      tasks[task_idx].c--;
      if (tasks[task_idx].c == 0) {
        tasks_processed++;
        active_tasks.erase(task_idx);
      }
    }
    cpus.clear();

    // Add new tasks.
    for (int new_task_idx : task_starts[time]) {
      active_tasks.insert(new_task_idx);
    }

    // Choose <= m new tasks to be processed by the cpus.
    set<pair<int, int> > tasks_by_priority;
    for (int task_idx : active_tasks) {
      if (tasks[task_idx].k - time < tasks[task_idx].c) {
        printf("NIE\n");
        return;
      }

      tasks_by_priority.insert(make_pair(tasks[task_idx].k - time - tasks[task_idx].c, task_idx));
    }

    auto it = tasks_by_priority.begin();
    for (int i = 0; i < m && it != tasks_by_priority.end(); i++, it++) {
      //printf("[%d] Assigning task %d.\n", time, it->second);
      cpus.insert(it->second);
    }

    time++;
  }

  printf("TAK\n");
}

int main() {
  // Load input.
  scanf("%d%d", &n, &m);

  if (m >= n) {
    printf("TAK\n");
    return 0;
  }

  int min_p = INT_MAX, max_k = 0;
  int v_base = 1;
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d", &tasks[i].p, &tasks[i].k, &tasks[i].c);
    min_p = min(min_p, tasks[i].p);
    max_k = max(max_k, tasks[i].k);
    v_base += tasks[i].c;
  }

  int total_time = max_k - min_p;
  int cur_u = 1;

  // If too much data, try a heuristic approach.
  if (v_base + m * total_time > 1000000) {
    heuristic();
    return 0;
  }
  
  // Initialize the U and V sets.
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < tasks[i].c; j++) {
      int u = cur_u + j;
      U.push_back(u);
      //printf("Adding %d to U (task %d, offset %d).\n", u, i + 1, j);
    }

    cur_u += tasks[i].c;
  }

  for (int cpu = 0; cpu < m; cpu++) {
    for (int time = 0; time < total_time; time++) {
      int v = v_base + (cpu * total_time) + time;
      V.push_back(v);
      //printf("Adding %d to V (cpu %d, time %d).\n", v, cpu, time + min_p);
    }
  }

  // Insert edges between U and V.
  cur_u = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < tasks[i].c; j++) {
      int u = cur_u + j;

      for (int cpu = 0; cpu < m; cpu++) {
        for (int time = tasks[i].p; time < tasks[i].k; time++) {
          int v = v_base + (cpu * total_time) + (time - min_p);
          G[u].push_back(v);
          G[v].push_back(u);
          //printf("Connecting %d and %d.\n", u, v);
        }
      }
    }

    cur_u += tasks[i].c;
  }

  printf("%s\n", HopcroftKarp() == U.size() ? "TAK" : "NIE");

  return 0;
}