#include <bits/stdc++.h> using namespace std; #define e1 first #define e2 second #define pb push_back #define mp make_pair #define boost ios_base::sync_with_stdio(false) #define eb emplace_back #define OUT(x) {cout << x; exit(0); } #define scanf(...) scanf(__VA_ARGS__)?:0 #define FOR(i, a, b) for(int i=(a); i<=(b); ++i) typedef long long ll; typedef unsigned int ull; typedef pair <int, int> PII; typedef pair <PII, int> PPI; typedef pair <ll, int> PLI; typedef pair <ll, ll> PLL; typedef pair <PII, PII> PP; typedef unsigned int ui; const int inf = 1e9+9; const int mod = 1e9+7; const ll MOD = 1e9+696969; template<class flow, const int maxn = 5100, const int maxm = 30100, flow INFF = 2e9> struct Max_flow { int pr[2*maxm], head[maxn], to[2*maxm], it[maxn], newid; flow cap[2*maxm]; Max_flow() { newid = 0; memset(head, -1, sizeof(head)); } inline void add_edge(int v, int u, flow vu, flow uv = 0) { if( newid >= 2*maxm ) exit(0); to[newid] = u, cap[newid] = vu, pr[newid] = head[v], head[v] = newid++; to[newid] = v, cap[newid] = uv, pr[newid] = head[u], head[u] = newid++; } int dist[maxn], q[maxn], h, t; inline int bfs(int src, int sink) { h = t = 0; memset(dist,-1,sizeof(dist)); q[h++] = src; dist[src] = 0; while(h != t) { int v = q[t++]; for(int e = head[v]; e != -1; e = pr[e]) { if(cap[e] > 0 && dist[to[e]] == -1) { dist[to[e]] = dist[v] + 1; q[h++] = to[e]; } } } return dist[sink] != -1; } flow dfs(int v, int sink, flow lim = INFF) { flow tmp = lim; if(v == sink) return lim; for(int &e = it[v]; e != -1; e = pr[e]) { int u = to[e]; if(dist[u] == dist[v]+1 && cap[e]) { flow f = dfs(u,sink, min(lim,cap[e])); cap[e] -= f; cap[e^1] += f; lim -= f; if(lim == 0) break; } } return tmp - lim; } flow max_flow(int src, int sink) { flow ans = 0; while(bfs(src,sink)) { memcpy(it,head,sizeof(head)); ans += dfs(src,sink); } return ans; } }; int n, m; vector <PII> ints; vector <PII> nowe; inline void add(int x, int y){ if (x <= y) nowe.pb(mp(x, y)); } #define maxn 420 int A[maxn], B[maxn], C[maxn]; Max_flow<int,420,420 * 420, inf + inf> Dinic; int main() { boost; nowe.clear(); cin >> n >> m; int ile = 0; FOR(i, 1, n) { cin >> A[i] >> B[i] >> C[i]; ile += C[i]; B[i]--; ints.pb({B[i], 1}); ints.pb({A[i], -1}); } sort(ints.begin(), ints.end()); for (int i=1; i<(int)ints.size(); ++i) { if (ints[i-1].e2 == 1 && ints[i].e2 == -1) { add(ints[i-1].e1 + 1, ints[i].e1 - 1); } if (ints[i-1].e2 == 1 && ints[i].e2 == 1) { add(ints[i-1].e1 + 1, ints[i].e1); } if (ints[i-1].e2 == -1 && ints[i].e2 == -1) { add(ints[i-1].e1, ints[i].e1 - 1); } if (ints[i-1].e2 == -1 && ints[i].e2 == 1) { add(ints[i-1].e1, ints[i].e1); } } //for (auto u : nowe) cout << u.e1 << ' ' << u.e2 << endl; int ww = (int)nowe.size(); int sink = ww + n + 1; FOR(i, 1, n) Dinic.add_edge(0, i, C[i]); FOR(i, 1, ww) Dinic.add_edge(n + i, sink, m *( nowe[i-1].e2 - nowe[i-1].e1 + 1)); FOR(i, 1, n) FOR(j, 1, ww) if (A[i] <= nowe[j-1].e1 && B[i] >= nowe[j-1].e2) Dinic.add_edge(i, n + j, nowe[j-1].e2 - nowe[j-1].e1 + 1); else assert(B[i] < nowe[j-1].e1 || A[i] > nowe[j-1].e2); if (Dinic.max_flow(0, sink) == ile) cout << "TAK"; else cout << "NIE"; }
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 | #include <bits/stdc++.h> using namespace std; #define e1 first #define e2 second #define pb push_back #define mp make_pair #define boost ios_base::sync_with_stdio(false) #define eb emplace_back #define OUT(x) {cout << x; exit(0); } #define scanf(...) scanf(__VA_ARGS__)?:0 #define FOR(i, a, b) for(int i=(a); i<=(b); ++i) typedef long long ll; typedef unsigned int ull; typedef pair <int, int> PII; typedef pair <PII, int> PPI; typedef pair <ll, int> PLI; typedef pair <ll, ll> PLL; typedef pair <PII, PII> PP; typedef unsigned int ui; const int inf = 1e9+9; const int mod = 1e9+7; const ll MOD = 1e9+696969; template<class flow, const int maxn = 5100, const int maxm = 30100, flow INFF = 2e9> struct Max_flow { int pr[2*maxm], head[maxn], to[2*maxm], it[maxn], newid; flow cap[2*maxm]; Max_flow() { newid = 0; memset(head, -1, sizeof(head)); } inline void add_edge(int v, int u, flow vu, flow uv = 0) { if( newid >= 2*maxm ) exit(0); to[newid] = u, cap[newid] = vu, pr[newid] = head[v], head[v] = newid++; to[newid] = v, cap[newid] = uv, pr[newid] = head[u], head[u] = newid++; } int dist[maxn], q[maxn], h, t; inline int bfs(int src, int sink) { h = t = 0; memset(dist,-1,sizeof(dist)); q[h++] = src; dist[src] = 0; while(h != t) { int v = q[t++]; for(int e = head[v]; e != -1; e = pr[e]) { if(cap[e] > 0 && dist[to[e]] == -1) { dist[to[e]] = dist[v] + 1; q[h++] = to[e]; } } } return dist[sink] != -1; } flow dfs(int v, int sink, flow lim = INFF) { flow tmp = lim; if(v == sink) return lim; for(int &e = it[v]; e != -1; e = pr[e]) { int u = to[e]; if(dist[u] == dist[v]+1 && cap[e]) { flow f = dfs(u,sink, min(lim,cap[e])); cap[e] -= f; cap[e^1] += f; lim -= f; if(lim == 0) break; } } return tmp - lim; } flow max_flow(int src, int sink) { flow ans = 0; while(bfs(src,sink)) { memcpy(it,head,sizeof(head)); ans += dfs(src,sink); } return ans; } }; int n, m; vector <PII> ints; vector <PII> nowe; inline void add(int x, int y){ if (x <= y) nowe.pb(mp(x, y)); } #define maxn 420 int A[maxn], B[maxn], C[maxn]; Max_flow<int,420,420 * 420, inf + inf> Dinic; int main() { boost; nowe.clear(); cin >> n >> m; int ile = 0; FOR(i, 1, n) { cin >> A[i] >> B[i] >> C[i]; ile += C[i]; B[i]--; ints.pb({B[i], 1}); ints.pb({A[i], -1}); } sort(ints.begin(), ints.end()); for (int i=1; i<(int)ints.size(); ++i) { if (ints[i-1].e2 == 1 && ints[i].e2 == -1) { add(ints[i-1].e1 + 1, ints[i].e1 - 1); } if (ints[i-1].e2 == 1 && ints[i].e2 == 1) { add(ints[i-1].e1 + 1, ints[i].e1); } if (ints[i-1].e2 == -1 && ints[i].e2 == -1) { add(ints[i-1].e1, ints[i].e1 - 1); } if (ints[i-1].e2 == -1 && ints[i].e2 == 1) { add(ints[i-1].e1, ints[i].e1); } } //for (auto u : nowe) cout << u.e1 << ' ' << u.e2 << endl; int ww = (int)nowe.size(); int sink = ww + n + 1; FOR(i, 1, n) Dinic.add_edge(0, i, C[i]); FOR(i, 1, ww) Dinic.add_edge(n + i, sink, m *( nowe[i-1].e2 - nowe[i-1].e1 + 1)); FOR(i, 1, n) FOR(j, 1, ww) if (A[i] <= nowe[j-1].e1 && B[i] >= nowe[j-1].e2) Dinic.add_edge(i, n + j, nowe[j-1].e2 - nowe[j-1].e1 + 1); else assert(B[i] < nowe[j-1].e1 || A[i] > nowe[j-1].e2); if (Dinic.max_flow(0, sink) == ile) cout << "TAK"; else cout << "NIE"; } |