#include <cstdio> #include <vector> #include <algorithm> const int max_n = 1010; const int max_m = 3030; const int max_k = 31; int n, m, k; int E[max_m][2]; int D[max_n]; std::vector<int> G[max_n]; int best_cover; int p[max_n]; int epoch; int elected[max_n]; int candidates[max_n]; std::vector<int> non_zero[max_k]; std::vector<int> recover[max_k]; std::vector<int> recoverN[max_k]; int CP[max_n][max_n]; bool lessThan(const int& i, const int& j) { return D[i] < D[j]; } void check_cover(int found, int sum_d) { if (found > best_cover) { return; // no } if (non_zero[found].size() == 0) { if (found < best_cover) { best_cover = found; // Reset Candidates for (int i = 0; i < n; ++i) p[i] = 0; for (int i = 0; i < found; ++i) p[candidates[i]] = 1; } else { // found == best_cover // Add candidates for (int i = 0; i < found; ++i) p[candidates[i]] = 1; } return; // yes } int nn = non_zero[found].size(); for (int need = 0, sum = 0; need < nn && sum < sum_d / 2; ++need) { sum += D[non_zero[found][nn - need - 1]]; if (need + 1 + found > k) return; // no } if (found == best_cover) { return; // no } // Pick first int v = non_zero[found][nn - 1]; candidates[found] = v; recover[found].clear(); for (int i = 0; i < G[v].size(); ++i) { if (D[G[v][i]] > 0) { --D[G[v][i]]; recover[found].push_back(G[v][i]); } } D[v] = 0; non_zero[found + 1].clear(); for (int i = 0; i < non_zero[found].size(); ++i) { if (D[non_zero[found][i]] > 0) { non_zero[found + 1].push_back(non_zero[found][i]); } } std::sort(non_zero[found + 1].begin(), non_zero[found + 1].end(), lessThan); check_cover(found + 1, sum_d - 2 * recover[found].size()); D[v] = recover[found].size(); for (int i = 0; i < recover[found].size(); ++i) ++D[recover[found][i]]; recover[found].clear(); // Pick neightbours of first if (found + D[v] <= best_cover) { int nfound = found; int nsum_d = sum_d; ++epoch; for (int i = 0; i < G[v].size(); ++i) { if (D[G[v][i]] > 0) { elected[G[v][i]] = epoch; candidates[nfound++] = G[v][i]; recover[found].push_back(G[v][i]); } } recoverN[found].clear(); for (int i = 0; i < recover[found].size(); ++i) { const int u = recover[found][i]; for (int j = 0; j < G[u].size(); ++j) { const int w = G[u][j]; if (D[w] > 0 || elected[w] == epoch) { recoverN[found].push_back(u); --D[u]; --nsum_d; if (elected[w] != epoch) { recoverN[found].push_back(w); --nsum_d; --D[w]; } } } } non_zero[nfound].clear(); for (int i = 0; i < non_zero[found].size(); ++i) { if (D[non_zero[found][i]] > 0) { non_zero[nfound].push_back(non_zero[found][i]); } } std::sort(non_zero[nfound].begin(), non_zero[nfound].end(), lessThan); check_cover(nfound, nsum_d); for (int i = 0; i < recoverN[found].size(); ++i) ++D[recoverN[found][i]]; recoverN[found].clear(); } return; // maybe } void compute() { scanf("%d %d %d", &n, &m, &k); epoch = 0; for (int i = 0; i < n; ++i) D[i] = elected[i] = p[i] = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { CP[i][j] = 0; } } for (int i = 0; i < n; ++i) G[i].clear(); int sum_d = 0; for (int i = 0; i < m; ++i) { scanf("%d %d", &E[i][0], &E[i][1]); --E[i][0]; --E[i][1]; if (CP[E[i][0]][E[i][1]] == 0) { G[E[i][0]].push_back(E[i][1]); G[E[i][1]].push_back(E[i][0]); ++D[E[i][0]]; ++D[E[i][1]]; CP[E[i][0]][E[i][1]] = CP[E[i][1]][E[i][0]] = 1; sum_d += 2; } } non_zero[0].clear(); for (int i = 0; i < n; ++i) { if (D[i] > 0) non_zero[0].push_back(i); } std::sort(non_zero[0].begin(), non_zero[0].end(), lessThan); best_cover = k; check_cover(0, sum_d); int cnt = 0; for (int i = 0; i < n; ++i) cnt += p[i]; if (best_cover <= k && cnt > 0) { printf("%d %d\n", best_cover, cnt); for (int i = 0; i < n; ++i) { if (p[i]) printf("%d ", i + 1); } printf("\n"); return; } printf("-1\n"); } int main () { int tests; scanf("%d", &tests); while (tests--) { compute(); } }
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 178 | #include <cstdio> #include <vector> #include <algorithm> const int max_n = 1010; const int max_m = 3030; const int max_k = 31; int n, m, k; int E[max_m][2]; int D[max_n]; std::vector<int> G[max_n]; int best_cover; int p[max_n]; int epoch; int elected[max_n]; int candidates[max_n]; std::vector<int> non_zero[max_k]; std::vector<int> recover[max_k]; std::vector<int> recoverN[max_k]; int CP[max_n][max_n]; bool lessThan(const int& i, const int& j) { return D[i] < D[j]; } void check_cover(int found, int sum_d) { if (found > best_cover) { return; // no } if (non_zero[found].size() == 0) { if (found < best_cover) { best_cover = found; // Reset Candidates for (int i = 0; i < n; ++i) p[i] = 0; for (int i = 0; i < found; ++i) p[candidates[i]] = 1; } else { // found == best_cover // Add candidates for (int i = 0; i < found; ++i) p[candidates[i]] = 1; } return; // yes } int nn = non_zero[found].size(); for (int need = 0, sum = 0; need < nn && sum < sum_d / 2; ++need) { sum += D[non_zero[found][nn - need - 1]]; if (need + 1 + found > k) return; // no } if (found == best_cover) { return; // no } // Pick first int v = non_zero[found][nn - 1]; candidates[found] = v; recover[found].clear(); for (int i = 0; i < G[v].size(); ++i) { if (D[G[v][i]] > 0) { --D[G[v][i]]; recover[found].push_back(G[v][i]); } } D[v] = 0; non_zero[found + 1].clear(); for (int i = 0; i < non_zero[found].size(); ++i) { if (D[non_zero[found][i]] > 0) { non_zero[found + 1].push_back(non_zero[found][i]); } } std::sort(non_zero[found + 1].begin(), non_zero[found + 1].end(), lessThan); check_cover(found + 1, sum_d - 2 * recover[found].size()); D[v] = recover[found].size(); for (int i = 0; i < recover[found].size(); ++i) ++D[recover[found][i]]; recover[found].clear(); // Pick neightbours of first if (found + D[v] <= best_cover) { int nfound = found; int nsum_d = sum_d; ++epoch; for (int i = 0; i < G[v].size(); ++i) { if (D[G[v][i]] > 0) { elected[G[v][i]] = epoch; candidates[nfound++] = G[v][i]; recover[found].push_back(G[v][i]); } } recoverN[found].clear(); for (int i = 0; i < recover[found].size(); ++i) { const int u = recover[found][i]; for (int j = 0; j < G[u].size(); ++j) { const int w = G[u][j]; if (D[w] > 0 || elected[w] == epoch) { recoverN[found].push_back(u); --D[u]; --nsum_d; if (elected[w] != epoch) { recoverN[found].push_back(w); --nsum_d; --D[w]; } } } } non_zero[nfound].clear(); for (int i = 0; i < non_zero[found].size(); ++i) { if (D[non_zero[found][i]] > 0) { non_zero[nfound].push_back(non_zero[found][i]); } } std::sort(non_zero[nfound].begin(), non_zero[nfound].end(), lessThan); check_cover(nfound, nsum_d); for (int i = 0; i < recoverN[found].size(); ++i) ++D[recoverN[found][i]]; recoverN[found].clear(); } return; // maybe } void compute() { scanf("%d %d %d", &n, &m, &k); epoch = 0; for (int i = 0; i < n; ++i) D[i] = elected[i] = p[i] = 0; for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { CP[i][j] = 0; } } for (int i = 0; i < n; ++i) G[i].clear(); int sum_d = 0; for (int i = 0; i < m; ++i) { scanf("%d %d", &E[i][0], &E[i][1]); --E[i][0]; --E[i][1]; if (CP[E[i][0]][E[i][1]] == 0) { G[E[i][0]].push_back(E[i][1]); G[E[i][1]].push_back(E[i][0]); ++D[E[i][0]]; ++D[E[i][1]]; CP[E[i][0]][E[i][1]] = CP[E[i][1]][E[i][0]] = 1; sum_d += 2; } } non_zero[0].clear(); for (int i = 0; i < n; ++i) { if (D[i] > 0) non_zero[0].push_back(i); } std::sort(non_zero[0].begin(), non_zero[0].end(), lessThan); best_cover = k; check_cover(0, sum_d); int cnt = 0; for (int i = 0; i < n; ++i) cnt += p[i]; if (best_cover <= k && cnt > 0) { printf("%d %d\n", best_cover, cnt); for (int i = 0; i < n; ++i) { if (p[i]) printf("%d ", i + 1); } printf("\n"); return; } printf("-1\n"); } int main () { int tests; scanf("%d", &tests); while (tests--) { compute(); } } |