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
#include <bits/stdc++.h>

using namespace std;

#define PB push_back
#define FORE(i, t) for(__typeof(t.begin())i=t.begin();i!=t.end();++i)
#define SZ(x) int((x).size())
#define REP(i, n) for(int i=0,_=(n);i<_;++i)
#define FOR(i, a, b) for(int i=(a),_=(b);i<=_;++i)
#define FORD(i, a, b) for(int i=(a),_=(b);i>=_;--i)

#ifdef DEBUG
#define deb(x) (cerr << x)
#else
#define deb(x)
#endif

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;

const int INF = 1e9 + 9;
const int MX = 103;

// Przeplyw push-relabel

struct Edge {
	int to, capacity, flow;
	Edge *rev;
	Edge(int to, int capacity, int flow = 0):
		to(to), capacity(capacity), flow(flow) {}
};

struct Vertex {
	int excess, height;
	list<Edge> adj;
	list<Edge>::iterator cur;

	Vertex():
	    excess(0), height(0), cur(adj.begin()) {}
};

struct PushRelabelMaxFlow {
    int number_of_vertices, source, sink;
    vector<Vertex> V;

    PushRelabelMaxFlow(int number_of_vertices, int source, int sink):
        number_of_vertices(number_of_vertices), source(source), sink(sink), V(number_of_vertices) {
        V[source].height = number_of_vertices;
    }

    void add_edge(int from, int to, int forward_capacity = 0, int backward_capacity = 0)
    {
        V[from].adj.push_front(Edge(to, forward_capacity));
        V[to].adj.push_front(Edge(from, backward_capacity));
        V[from].adj.front().rev = &V[to].adj.front();
        V[to].adj.front().rev = &V[from].adj.front();
    }

    inline bool inside(int x) {
        return x != source && x != sink;
    }

    void push(int x, Edge &e) {
        int w = min(V[x].excess, e.capacity - e.flow);
        V[x].excess -= w;
        V[e.to].excess += w;
        e.flow += w;
        e.rev->flow = -e.flow;
    }

    void lift(int x) {
        int mn = INF;
        FORE(it, V[x].adj) {
            if(it->capacity > it->flow) {
                mn = min(mn, V[it->to].height);
            }
        }
        V[x].height = 1 + mn;
    }

    void discharge(int x) {
        while (V[x].excess > 0) {
            list<Edge>::iterator &y = V[x].cur;
            if (y == V[x].adj.end()) {
                lift(x);
                y = V[x].adj.begin();
            } else if((y->capacity > y->flow) && (V[x].height == V[y->to].height + 1)) {
                push(x, *y);
            } else {
                ++y;
            }
        }
    }

    int compute_max_flow()
    {
        FORE(it, V[source].adj) {
            it->flow = it->capacity;
            it->rev->flow = -it->capacity;
            V[it->to].excess += it->capacity;
            V[source].excess -= it->capacity;
        }
        list<int> l;
        REP (i, number_of_vertices) {
            if (inside(i)) {
                l.PB(i);
            }
        }
        FORE(it, l) {
            int old_height = V[*it].height;
            discharge(*it);
            if(V[*it].height > old_height)
            {
                int x = *it;
                l.erase(it);
                l.push_front(x);
                it = l.begin();
            }
        }
        return V[sink].excess;
    }
};

int n, m;
int a[MX], b[MX], duration[MX];

bool is_scheduling_possible() {
    cin >> n >> m;
    set<int> endpoints_set;
    int sum_of_durations = 0;
    REP (i, n) {
        cin >> a[i] >> b[i] >> duration[i];
        sum_of_durations += duration[i];
        endpoints_set.insert(a[i]);
        endpoints_set.insert(b[i]);
    }
    vi endpoints(endpoints_set.begin(), endpoints_set.end());
    int number_of_vertices = 1 + n + (SZ(endpoints) - 1) + 1;
    int source = 0;
    int sink = number_of_vertices - 1;
    PushRelabelMaxFlow graph(number_of_vertices, source, sink);
    int ranges_first_vertex = 1 + n;
    REP (i, n) {
        int vertex = i + 1;
        graph.add_edge(source, vertex, duration[i]);
        REP (j, SZ(endpoints) - 1) {
            int start = endpoints[j];
            int end = endpoints[j + 1];
            if (start >= a[i] && end <= b[i]) {
                int range_vertex = ranges_first_vertex + j;
                graph.add_edge(vertex, range_vertex, end - start);
            }
        }
    }
    REP (j, SZ(endpoints) - 1) {
        int start = endpoints[j];
        int end = endpoints[j + 1];
        int range_vertex = ranges_first_vertex + j;
        graph.add_edge(range_vertex, sink, (end - start) * m);
    }

    int max_flow = graph.compute_max_flow();

    return max_flow >= sum_of_durations;
}

void inline one() {
    cout << (is_scheduling_possible() ? "TAK" : "NIE") << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    //int z; cin >> z; while(z--)
    one();
}