#include <cstdio> #include <algorithm> #include <vector> #include <set> using namespace std; const int inf = 1000000005; const int maxv = 1000005; const int maxn = 1005; const int source = 0; const int target = 1; struct Edge { int v, f, c, rev; Edge(int x, int y, int z, int t) : v(x), f(y), c(z), rev(t) {} }; vector<Edge> g[maxn]; int it[maxn]; int d[maxn]; vector<int> b[maxv]; vector<int> e[maxv]; int p[maxn]; int k[maxn]; int c[maxn]; void edge(int u, int v, int c) { g[u].push_back(Edge(v, 0, c, g[v].size())); g[v].push_back(Edge(u, c, c, g[u].size() - 1)); } int makeNetwork(int n, int m) { set<int> s; int l = 0; for(int i = 2; i <= n; ++i) { edge(source, i, c[i]); } for(int i = 0; i < maxv - 1; ++i) { for(int j = 0; j < b[i].size(); ++j) s.insert(b[i][j]); if(s.size() > 0) l++; else l = 0; if(l > 0 && (e[i].size() > 0 || b[i + 1].size() > 0)) { n++; for(set<int>::iterator it = s.begin(); it != s.end(); ++it) { edge(*it, n, l); } edge(n, target, m * l); l = 0; } for(int j = 0; j < e[i].size(); ++j) s.erase(e[i][j]); } return n; } int flow(int u, int f) { if(u == target || f == 0) return f; int res = 0; while(it[u] < g[u].size() && f) { Edge& e = g[u][it[u]]; if(e.f < e.c && d[u] + 1 == d[e.v]) { int s = flow(e.v, min(f, e.c - e.f)); Edge& r = g[e.v][e.rev]; e.f += s; r.f -= s; f -= s; res += s; } it[u]++; } return res; } void bfs(int u) { vector<int> q; d[u] = 1; q.push_back(u); for(int j = 0; j < q.size(); ++j) { u = q[j]; for(int i = 0; i < g[u].size(); ++i) { Edge e = g[u][i]; if(d[e.v] == 0 && e.f < e.c) { d[e.v] = d[u] + 1; q.push_back(e.v); } } } } int maxFlow(int n) { int res = 0; while(true) { for(int i = 0; i <= n; ++i) { d[i] = 0; it[i] = 0; } bfs(source); if(d[target] == 0) break; res += flow(source, inf); } return res; } int main() { int n, m, sum = 0; scanf("%d%d", &n, &m); n++; for(int i = 2; i <= n; ++i) { scanf("%d%d%d", p + i, k + i, c + i); k[i]--; sum += c[i]; b[p[i]].push_back(i); e[k[i]].push_back(i); } n = makeNetwork(n, m); int f = maxFlow(n); printf("%s\n", (sum == f) ? "TAK" : "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 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 | #include <cstdio> #include <algorithm> #include <vector> #include <set> using namespace std; const int inf = 1000000005; const int maxv = 1000005; const int maxn = 1005; const int source = 0; const int target = 1; struct Edge { int v, f, c, rev; Edge(int x, int y, int z, int t) : v(x), f(y), c(z), rev(t) {} }; vector<Edge> g[maxn]; int it[maxn]; int d[maxn]; vector<int> b[maxv]; vector<int> e[maxv]; int p[maxn]; int k[maxn]; int c[maxn]; void edge(int u, int v, int c) { g[u].push_back(Edge(v, 0, c, g[v].size())); g[v].push_back(Edge(u, c, c, g[u].size() - 1)); } int makeNetwork(int n, int m) { set<int> s; int l = 0; for(int i = 2; i <= n; ++i) { edge(source, i, c[i]); } for(int i = 0; i < maxv - 1; ++i) { for(int j = 0; j < b[i].size(); ++j) s.insert(b[i][j]); if(s.size() > 0) l++; else l = 0; if(l > 0 && (e[i].size() > 0 || b[i + 1].size() > 0)) { n++; for(set<int>::iterator it = s.begin(); it != s.end(); ++it) { edge(*it, n, l); } edge(n, target, m * l); l = 0; } for(int j = 0; j < e[i].size(); ++j) s.erase(e[i][j]); } return n; } int flow(int u, int f) { if(u == target || f == 0) return f; int res = 0; while(it[u] < g[u].size() && f) { Edge& e = g[u][it[u]]; if(e.f < e.c && d[u] + 1 == d[e.v]) { int s = flow(e.v, min(f, e.c - e.f)); Edge& r = g[e.v][e.rev]; e.f += s; r.f -= s; f -= s; res += s; } it[u]++; } return res; } void bfs(int u) { vector<int> q; d[u] = 1; q.push_back(u); for(int j = 0; j < q.size(); ++j) { u = q[j]; for(int i = 0; i < g[u].size(); ++i) { Edge e = g[u][i]; if(d[e.v] == 0 && e.f < e.c) { d[e.v] = d[u] + 1; q.push_back(e.v); } } } } int maxFlow(int n) { int res = 0; while(true) { for(int i = 0; i <= n; ++i) { d[i] = 0; it[i] = 0; } bfs(source); if(d[target] == 0) break; res += flow(source, inf); } return res; } int main() { int n, m, sum = 0; scanf("%d%d", &n, &m); n++; for(int i = 2; i <= n; ++i) { scanf("%d%d%d", p + i, k + i, c + i); k[i]--; sum += c[i]; b[p[i]].push_back(i); e[k[i]].push_back(i); } n = makeNetwork(n, m); int f = maxFlow(n); printf("%s\n", (sum == f) ? "TAK" : "NIE"); } |