#include <iostream> #include <stdio.h> #include <algorithm> using namespace std; const int MAX_NODES = 300; const int MAX_EDGES = 400; const int MAX_DELETIONS = 4; int nNodes; int nEdges; int nDeletions; int graph[MAX_EDGES][2]; int deletions[MAX_DELETIONS]; int chains[MAX_NODES]; void prepareGraph() { cin >> nNodes; cin >> nEdges; cin >> nDeletions; for (int i = 0; i < nEdges; i++) { int from; cin >> from; from--; int to; cin >> to; to--; graph[i][0] = from; graph[i][1] = to; } } void sortGraph() { int coded[nEdges]; for (int i = 0; i < nEdges; i++) { int from = graph[i][0]; int to = graph[i][1]; coded[i] = from * MAX_EDGES + to; } sort(coded, coded + nEdges); for (int i = 0; i < nEdges; i++) { graph[i][0] = coded[i] / MAX_EDGES; graph[i][1] = coded[i] % MAX_EDGES; } } void resetDeletions() { for (int i = 0; i < nDeletions; i++) { deletions[i] = i; } } /* * returns true if key was incremented or false if end of scope */ bool incrementDeletions() { if (nDeletions == 0) { return false; } int keep; for (keep = 0; keep < nDeletions; keep++) { if (deletions[keep] == keep + (nNodes - nDeletions)) { break; } } if (keep == 0) { // we are at last combination return false; } deletions[keep - 1]++; for (int i = keep; i < nDeletions; i++) { deletions[i] = deletions[i - 1] + 1; } return true; } void forwardDeletions(int indexOfNode) { int keep = 0; for (int i = 0; i < nDeletions; i++) { if (deletions[i] < indexOfNode) { keep++; } else { break; } } // push remaining deletions to the right for (int i = keep; i < nDeletions; i++) { deletions[i] = i + (nNodes - nDeletions); } } bool isDeleted(int indexOfEdge) { for (int i = 0; i < nDeletions; i++) { if (deletions[i] == graph[indexOfEdge][0] || deletions[i] == graph[indexOfEdge][1]) { return true; } } return false; } /* * returns length of longest chain if it is shorter than limit * * or (node-nNodes) if (node) hit limit */ int findLongestChain(int limit) { // clean chains for (int i = 0; i < nNodes; i++) { chains[i] = 1; } int longestChain = 1; for (int i = 0; i < nEdges; i++) { if (isDeleted(i)) { continue; } int nodeA = graph[i][0]; int nodeB = graph[i][1]; chains[nodeB] = max(chains[nodeB], chains[nodeA] + 1); longestChain = max(chains[nodeB], longestChain); if (longestChain >= limit) { // encode hitting limit as negative result return nodeB - nNodes; } } return longestChain; } int findResultByIterating(int limit) { int result = limit; resetDeletions(); do { int response = findLongestChain(result); if (response < 0) { forwardDeletions(response + nNodes); } else { result = response; } } while (incrementDeletions()); return result; } int findSolution() { if (nDeletions == 0) { return findLongestChain(1000); } if (nNodes == nDeletions) { return 0; } if (nDeletions <= 2) { return findResultByIterating(1000); } return findResultByIterating(1000); } int main() { prepareGraph(); sortGraph(); printf("%d", findSolution()); 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 154 155 156 157 158 159 160 161 162 163 164 165 166 | #include <iostream> #include <stdio.h> #include <algorithm> using namespace std; const int MAX_NODES = 300; const int MAX_EDGES = 400; const int MAX_DELETIONS = 4; int nNodes; int nEdges; int nDeletions; int graph[MAX_EDGES][2]; int deletions[MAX_DELETIONS]; int chains[MAX_NODES]; void prepareGraph() { cin >> nNodes; cin >> nEdges; cin >> nDeletions; for (int i = 0; i < nEdges; i++) { int from; cin >> from; from--; int to; cin >> to; to--; graph[i][0] = from; graph[i][1] = to; } } void sortGraph() { int coded[nEdges]; for (int i = 0; i < nEdges; i++) { int from = graph[i][0]; int to = graph[i][1]; coded[i] = from * MAX_EDGES + to; } sort(coded, coded + nEdges); for (int i = 0; i < nEdges; i++) { graph[i][0] = coded[i] / MAX_EDGES; graph[i][1] = coded[i] % MAX_EDGES; } } void resetDeletions() { for (int i = 0; i < nDeletions; i++) { deletions[i] = i; } } /* * returns true if key was incremented or false if end of scope */ bool incrementDeletions() { if (nDeletions == 0) { return false; } int keep; for (keep = 0; keep < nDeletions; keep++) { if (deletions[keep] == keep + (nNodes - nDeletions)) { break; } } if (keep == 0) { // we are at last combination return false; } deletions[keep - 1]++; for (int i = keep; i < nDeletions; i++) { deletions[i] = deletions[i - 1] + 1; } return true; } void forwardDeletions(int indexOfNode) { int keep = 0; for (int i = 0; i < nDeletions; i++) { if (deletions[i] < indexOfNode) { keep++; } else { break; } } // push remaining deletions to the right for (int i = keep; i < nDeletions; i++) { deletions[i] = i + (nNodes - nDeletions); } } bool isDeleted(int indexOfEdge) { for (int i = 0; i < nDeletions; i++) { if (deletions[i] == graph[indexOfEdge][0] || deletions[i] == graph[indexOfEdge][1]) { return true; } } return false; } /* * returns length of longest chain if it is shorter than limit * * or (node-nNodes) if (node) hit limit */ int findLongestChain(int limit) { // clean chains for (int i = 0; i < nNodes; i++) { chains[i] = 1; } int longestChain = 1; for (int i = 0; i < nEdges; i++) { if (isDeleted(i)) { continue; } int nodeA = graph[i][0]; int nodeB = graph[i][1]; chains[nodeB] = max(chains[nodeB], chains[nodeA] + 1); longestChain = max(chains[nodeB], longestChain); if (longestChain >= limit) { // encode hitting limit as negative result return nodeB - nNodes; } } return longestChain; } int findResultByIterating(int limit) { int result = limit; resetDeletions(); do { int response = findLongestChain(result); if (response < 0) { forwardDeletions(response + nNodes); } else { result = response; } } while (incrementDeletions()); return result; } int findSolution() { if (nDeletions == 0) { return findLongestChain(1000); } if (nNodes == nDeletions) { return 0; } if (nDeletions <= 2) { return findResultByIterating(1000); } return findResultByIterating(1000); } int main() { prepareGraph(); sortGraph(); printf("%d", findSolution()); return 0; } |