// kod Hopcrofta-Karpa pochodzi z biblioteczki uzywanej na UWr #include <cstdio> #include <queue> #include <vector> using namespace std; #define MAXV 1500007 #define INF 1000000007 int n, m, max_t; vector<int> adj[MAXV]; int pairr[2*MAXV], dist[2*MAXV]; bool bfs() { deque<int> q; for(int i = 1; i <= n; i++) { if (!pairr[i]) { dist[i] = 0; q.push_back(i); } else dist[i] = INF; } dist[0] = INF; while (!q.empty()) { int v = q.front(); q.pop_front(); for(int i = 0; i < adj[v].size(); i++) { int u = adj[v][i]; if (dist[pairr[u]] == INF) { dist[pairr[u]] = dist[v] + 1; q.push_back(pairr[u]); } } } return dist[0] != INF; } bool dfs(int v) { if (!v) return 1; for(int i = 0; i < adj[v].size(); i++) { int u = adj[v][i]; if (dist[pairr[u]] == dist[v] + 1) { if (dfs(pairr[u])) { pairr[u] = v; pairr[v] = u; return 1; } } } dist[v] = INF; return 0; } int matching() { for(int i = 0; i < n + m + 1; i++) { pairr[i] = 0; } int w = 0; while(bfs()) { // printf("running bfs\n"); for(int v = 1; v <= n; v++) { if (!pairr[v]) { if (dfs(v)) w++; } } } return w; } vector<pair<int, pair<int, int>>> tasks; int new_n, p, k, c, new_m; int main() { scanf("%d%d", &n, &m); while(n--) { scanf("%d%d%d", &p, &k, &c); new_n += c; max_t = std::max(max_t, k); tasks.push_back(make_pair(c, make_pair(p, k-1))); } new_m = m * max_t; int current = 1; for(int i = 0; i < tasks.size(); i++) { for(int j = 0; j < tasks[i].first; j++) { for(int l = tasks[i].second.first; l <= tasks[i].second.second; l++) { for(int k = 0; k < m; k++) { adj[current].push_back(new_n + m * l + k + 1); } } current++; } } m = new_m; n = new_n; if (new_n == matching()) printf("TAK\n"); else printf("NIE\n"); }
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 | // kod Hopcrofta-Karpa pochodzi z biblioteczki uzywanej na UWr #include <cstdio> #include <queue> #include <vector> using namespace std; #define MAXV 1500007 #define INF 1000000007 int n, m, max_t; vector<int> adj[MAXV]; int pairr[2*MAXV], dist[2*MAXV]; bool bfs() { deque<int> q; for(int i = 1; i <= n; i++) { if (!pairr[i]) { dist[i] = 0; q.push_back(i); } else dist[i] = INF; } dist[0] = INF; while (!q.empty()) { int v = q.front(); q.pop_front(); for(int i = 0; i < adj[v].size(); i++) { int u = adj[v][i]; if (dist[pairr[u]] == INF) { dist[pairr[u]] = dist[v] + 1; q.push_back(pairr[u]); } } } return dist[0] != INF; } bool dfs(int v) { if (!v) return 1; for(int i = 0; i < adj[v].size(); i++) { int u = adj[v][i]; if (dist[pairr[u]] == dist[v] + 1) { if (dfs(pairr[u])) { pairr[u] = v; pairr[v] = u; return 1; } } } dist[v] = INF; return 0; } int matching() { for(int i = 0; i < n + m + 1; i++) { pairr[i] = 0; } int w = 0; while(bfs()) { // printf("running bfs\n"); for(int v = 1; v <= n; v++) { if (!pairr[v]) { if (dfs(v)) w++; } } } return w; } vector<pair<int, pair<int, int>>> tasks; int new_n, p, k, c, new_m; int main() { scanf("%d%d", &n, &m); while(n--) { scanf("%d%d%d", &p, &k, &c); new_n += c; max_t = std::max(max_t, k); tasks.push_back(make_pair(c, make_pair(p, k-1))); } new_m = m * max_t; int current = 1; for(int i = 0; i < tasks.size(); i++) { for(int j = 0; j < tasks[i].first; j++) { for(int l = tasks[i].second.first; l <= tasks[i].second.second; l++) { for(int k = 0; k < m; k++) { adj[current].push_back(new_n + m * l + k + 1); } } current++; } } m = new_m; n = new_n; if (new_n == matching()) printf("TAK\n"); else printf("NIE\n"); } |