#include <algorithm> #include <vector> #include <climits> #include <iostream> #include <cstring> #include <cstdio> using namespace std; const int MAXN = 1000300; const int INF = 1000000000; struct edge { int a, b, cap, flow; }; int n, s, t, d[MAXN], ptr[MAXN], q[MAXN]; vector<edge> e; vector<int> g[MAXN]; void add_edge (int a, int b, int cap) { edge e1 = { a, b, cap, 0 }; edge e2 = { b, a, 0, 0 }; g[a].push_back ((int) e.size()); e.push_back (e1); g[b].push_back ((int) e.size()); e.push_back (e2); } bool bfs() { int qh=0, qt=0; q[qt++] = s; memset (d, -1, n * sizeof d[0]); d[s] = 0; while (qh < qt && d[t] == -1) { int v = q[qh++]; for (size_t i=0; i<g[v].size(); ++i) { int id = g[v][i], to = e[id].b; if (d[to] == -1 && e[id].flow < e[id].cap) { q[qt++] = to; d[to] = d[v] + 1; } } } return d[t] != -1; } int dfs (int v, int flow) { if (!flow) return 0; if (v == t) return flow; for (; ptr[v]<(int)g[v].size(); ++ptr[v]) { int id = g[v][ptr[v]], to = e[id].b; if (d[to] != d[v] + 1) continue; int pushed = dfs (to, min (flow, e[id].cap - e[id].flow)); if (pushed) { e[id].flow += pushed; e[id^1].flow -= pushed; return pushed; } } return 0; } int dinic() { int flow = 0; for (;;) { if (!bfs()) break; memset (ptr, 0, n * sizeof ptr[0]); while (int pushed = dfs (s, INF)) flow += pushed; } return flow; } int visited[1000310]; struct Ve {int rev, c, f;}; int main() { int n1, m1, f, b, e; scanf("%d%d", &n1, &m1); if (n1 <= m1) { printf("TAK\n"); return 0; } Ve l; /* newpage */ vector<Ve> K; s = 0; f = 1000001; memset(visited, 0, sizeof(visited)); int ilew = 0; int sum = 0; for (int x = 0; x < n1; x++) { scanf("%d%d%d", &b, &e, &l.c); sum += l.c; b++; e++; ilew = max(ilew, e); Ve v; v.rev = b; v.c = e; v.f = l.c; K.push_back(v); } f = ilew + n1 + 2; t = f; n = f+1; //Graph<Vs, Ve> g(ilew + n + 3); for (int k = 0; k < K.size(); k++) { b = K[k].rev; e = K[k].c; l.c = K[k].f; //g.EdgeU(s, k + 1, l); add_edge(s, k + 1, l.c); //cout << "s k + 1 " << s << " " << k + 1 << " " << l.c << endl; for (int i = b; i < e; i++) { Ve l2; l2.c = 1; //g.EdgeU(k + 1, i + n, l2); add_edge(k + 1, i + n1, l2.c); //cout << "k + 1 i + n " << k + 1 << " " << i + n << " "<< l2.c << endl; if (!visited[i]) { Ve l3; l3.c = m1; //g.EdgeU(i + n, f, l3); add_edge(i + n1, f, l3.c); // cout << "i + n f " << i + n << " " << f << " " << l3.c << endl; visited[i] = 1; } } } int sum2 = dinic(); if (sum == sum2) printf("TAK\n"); else printf("NIE\n"); return 0; }
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 | #include <algorithm> #include <vector> #include <climits> #include <iostream> #include <cstring> #include <cstdio> using namespace std; const int MAXN = 1000300; const int INF = 1000000000; struct edge { int a, b, cap, flow; }; int n, s, t, d[MAXN], ptr[MAXN], q[MAXN]; vector<edge> e; vector<int> g[MAXN]; void add_edge (int a, int b, int cap) { edge e1 = { a, b, cap, 0 }; edge e2 = { b, a, 0, 0 }; g[a].push_back ((int) e.size()); e.push_back (e1); g[b].push_back ((int) e.size()); e.push_back (e2); } bool bfs() { int qh=0, qt=0; q[qt++] = s; memset (d, -1, n * sizeof d[0]); d[s] = 0; while (qh < qt && d[t] == -1) { int v = q[qh++]; for (size_t i=0; i<g[v].size(); ++i) { int id = g[v][i], to = e[id].b; if (d[to] == -1 && e[id].flow < e[id].cap) { q[qt++] = to; d[to] = d[v] + 1; } } } return d[t] != -1; } int dfs (int v, int flow) { if (!flow) return 0; if (v == t) return flow; for (; ptr[v]<(int)g[v].size(); ++ptr[v]) { int id = g[v][ptr[v]], to = e[id].b; if (d[to] != d[v] + 1) continue; int pushed = dfs (to, min (flow, e[id].cap - e[id].flow)); if (pushed) { e[id].flow += pushed; e[id^1].flow -= pushed; return pushed; } } return 0; } int dinic() { int flow = 0; for (;;) { if (!bfs()) break; memset (ptr, 0, n * sizeof ptr[0]); while (int pushed = dfs (s, INF)) flow += pushed; } return flow; } int visited[1000310]; struct Ve {int rev, c, f;}; int main() { int n1, m1, f, b, e; scanf("%d%d", &n1, &m1); if (n1 <= m1) { printf("TAK\n"); return 0; } Ve l; /* newpage */ vector<Ve> K; s = 0; f = 1000001; memset(visited, 0, sizeof(visited)); int ilew = 0; int sum = 0; for (int x = 0; x < n1; x++) { scanf("%d%d%d", &b, &e, &l.c); sum += l.c; b++; e++; ilew = max(ilew, e); Ve v; v.rev = b; v.c = e; v.f = l.c; K.push_back(v); } f = ilew + n1 + 2; t = f; n = f+1; //Graph<Vs, Ve> g(ilew + n + 3); for (int k = 0; k < K.size(); k++) { b = K[k].rev; e = K[k].c; l.c = K[k].f; //g.EdgeU(s, k + 1, l); add_edge(s, k + 1, l.c); //cout << "s k + 1 " << s << " " << k + 1 << " " << l.c << endl; for (int i = b; i < e; i++) { Ve l2; l2.c = 1; //g.EdgeU(k + 1, i + n, l2); add_edge(k + 1, i + n1, l2.c); //cout << "k + 1 i + n " << k + 1 << " " << i + n << " "<< l2.c << endl; if (!visited[i]) { Ve l3; l3.c = m1; //g.EdgeU(i + n, f, l3); add_edge(i + n1, f, l3.c); // cout << "i + n f " << i + n << " " << f << " " << l3.c << endl; visited[i] = 1; } } } int sum2 = dinic(); if (sum == sum2) printf("TAK\n"); else printf("NIE\n"); return 0; } |