#include <bits/stdc++.h> const int MAX_N = 1e3; const int base = 1<<12; std::vector<int> list[MAX_N+3]; int n, m, k; int best[MAX_N+3]; bool taken[MAX_N+3]; int deg[MAX_N+3]; int DEG_SUM; std::pair<int, int> t[2*base+5]; void insert(int v, int x) { v += base; t[v] = {x, v-base}; v /= 2; while (v > 0) { t[v] = std::max(t[v*2], t[v*2+1]); v /= 2; } } std::pair<int, int> get_max() { return t[1]; } void disable(int v) { taken[v] = true; for (auto u : list[v]) if (!taken[u]) { deg[u]--; deg[v]--; insert(u, deg[u]); DEG_SUM -= 2; } insert(v, deg[v]); } void enable(int v) { taken[v] = false; for (auto u : list[v]) if (!taken[u]) { deg[u]++; deg[v]++; insert(u, deg[u]); DEG_SUM += 2; } insert(v, deg[v]); } bool visited[MAX_N+3]; int find_next(int v) { for (auto u : list[v]) if (!taken[u] && !visited[u]) return u; return -1; } int go_2() { for (int i = 1; i <= n; i++) visited[i] = false; int res = 0; for (int i = 1; i <= n; i++) { int u = i; if (deg[u] == 1 && !visited[u]) { int l = 1; visited[u] = true; while (1) { int v = find_next(u); if (v != -1) { l++; visited[v] = true; u = v; } else break; } res += l/2; } } for (int i = 1; i <= n; i++) { int u = i; if (deg[u] == 2 && !visited[u]) { int l = 1; visited[u] = true; while (1) { int v = find_next(u); if (v != -1) { l++; visited[v] = true; u = v; } else break; } if (l > 1) res += ((l+1)/2); } } return res; } int MIN_VC_FOUND; int get_min_vc(int cnt_taken) { if (cnt_taken > MIN_VC_FOUND) return 99; if (cnt_taken > k) return 99; if (DEG_SUM == 0) { MIN_VC_FOUND = std::min(MIN_VC_FOUND, cnt_taken); return cnt_taken; } if (cnt_taken >= k && DEG_SUM != 0) return 99; int v = get_max().second; int max_deg = get_max().first; if (max_deg == 1) { if (cnt_taken + (DEG_SUM/2) <= k) { MIN_VC_FOUND = std::min(MIN_VC_FOUND, cnt_taken + (DEG_SUM/2)); return cnt_taken + (DEG_SUM/2); } return 99; } else if (max_deg == 2) { int res = go_2(); if (cnt_taken + res <= k) { MIN_VC_FOUND = std::min(cnt_taken + res, MIN_VC_FOUND); return cnt_taken + res; } return 99; } // exclude v from VC, this means that his whole neighbourhood must be in VC std::vector<int> U; for (auto u : list[v]) if (!taken[u]) { disable(u); U.push_back(u); } int res = get_min_vc(cnt_taken + U.size()); for (auto u : U) enable(u); disable(v); res = std::min(res, get_min_vc(cnt_taken+1)); enable(v); return res; } void solve() { std::cin >> n >> m >> k; for (int i = 1; i <= n; i++) { list[i].clear(); deg[i] = 0; } DEG_SUM = 0; std::set<std::pair<int, int> > edges; for (int i = 1; i <= m; i++) { int u, v; std::cin >> u >> v; if (edges.count({u,v}) || edges.count({v,u})) continue; edges.insert({u,v}); list[u].push_back(v); list[v].push_back(u); deg[u]++; deg[v]++; DEG_SUM += 2; } for (int i = 1; i <= n; i++) insert(i, deg[i]); MIN_VC_FOUND = 100; for (int i = 1; i <= n; i++) { // min vc assuming i is taken disable(i); best[i] = get_min_vc(1); MIN_VC_FOUND = std::min(MIN_VC_FOUND, best[i]); enable(i); } if (MIN_VC_FOUND <= k) { std::cout << MIN_VC_FOUND << " "; int v_cnt = 0; for (int i = 1; i <= n; i++) if (best[i] == MIN_VC_FOUND) v_cnt++; std::cout << v_cnt << "\n"; for (int i = 1; i <= n; i++) if (best[i] == MIN_VC_FOUND) std::cout << i << " "; std::cout << "\n"; } else { std::cout << "-1\n"; } for (int i = 1; i <= n; i++) insert(i, 0); } int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); int t; std::cin >> t; while (t--) solve(); }
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 179 180 181 182 183 184 185 186 187 188 189 | #include <bits/stdc++.h> const int MAX_N = 1e3; const int base = 1<<12; std::vector<int> list[MAX_N+3]; int n, m, k; int best[MAX_N+3]; bool taken[MAX_N+3]; int deg[MAX_N+3]; int DEG_SUM; std::pair<int, int> t[2*base+5]; void insert(int v, int x) { v += base; t[v] = {x, v-base}; v /= 2; while (v > 0) { t[v] = std::max(t[v*2], t[v*2+1]); v /= 2; } } std::pair<int, int> get_max() { return t[1]; } void disable(int v) { taken[v] = true; for (auto u : list[v]) if (!taken[u]) { deg[u]--; deg[v]--; insert(u, deg[u]); DEG_SUM -= 2; } insert(v, deg[v]); } void enable(int v) { taken[v] = false; for (auto u : list[v]) if (!taken[u]) { deg[u]++; deg[v]++; insert(u, deg[u]); DEG_SUM += 2; } insert(v, deg[v]); } bool visited[MAX_N+3]; int find_next(int v) { for (auto u : list[v]) if (!taken[u] && !visited[u]) return u; return -1; } int go_2() { for (int i = 1; i <= n; i++) visited[i] = false; int res = 0; for (int i = 1; i <= n; i++) { int u = i; if (deg[u] == 1 && !visited[u]) { int l = 1; visited[u] = true; while (1) { int v = find_next(u); if (v != -1) { l++; visited[v] = true; u = v; } else break; } res += l/2; } } for (int i = 1; i <= n; i++) { int u = i; if (deg[u] == 2 && !visited[u]) { int l = 1; visited[u] = true; while (1) { int v = find_next(u); if (v != -1) { l++; visited[v] = true; u = v; } else break; } if (l > 1) res += ((l+1)/2); } } return res; } int MIN_VC_FOUND; int get_min_vc(int cnt_taken) { if (cnt_taken > MIN_VC_FOUND) return 99; if (cnt_taken > k) return 99; if (DEG_SUM == 0) { MIN_VC_FOUND = std::min(MIN_VC_FOUND, cnt_taken); return cnt_taken; } if (cnt_taken >= k && DEG_SUM != 0) return 99; int v = get_max().second; int max_deg = get_max().first; if (max_deg == 1) { if (cnt_taken + (DEG_SUM/2) <= k) { MIN_VC_FOUND = std::min(MIN_VC_FOUND, cnt_taken + (DEG_SUM/2)); return cnt_taken + (DEG_SUM/2); } return 99; } else if (max_deg == 2) { int res = go_2(); if (cnt_taken + res <= k) { MIN_VC_FOUND = std::min(cnt_taken + res, MIN_VC_FOUND); return cnt_taken + res; } return 99; } // exclude v from VC, this means that his whole neighbourhood must be in VC std::vector<int> U; for (auto u : list[v]) if (!taken[u]) { disable(u); U.push_back(u); } int res = get_min_vc(cnt_taken + U.size()); for (auto u : U) enable(u); disable(v); res = std::min(res, get_min_vc(cnt_taken+1)); enable(v); return res; } void solve() { std::cin >> n >> m >> k; for (int i = 1; i <= n; i++) { list[i].clear(); deg[i] = 0; } DEG_SUM = 0; std::set<std::pair<int, int> > edges; for (int i = 1; i <= m; i++) { int u, v; std::cin >> u >> v; if (edges.count({u,v}) || edges.count({v,u})) continue; edges.insert({u,v}); list[u].push_back(v); list[v].push_back(u); deg[u]++; deg[v]++; DEG_SUM += 2; } for (int i = 1; i <= n; i++) insert(i, deg[i]); MIN_VC_FOUND = 100; for (int i = 1; i <= n; i++) { // min vc assuming i is taken disable(i); best[i] = get_min_vc(1); MIN_VC_FOUND = std::min(MIN_VC_FOUND, best[i]); enable(i); } if (MIN_VC_FOUND <= k) { std::cout << MIN_VC_FOUND << " "; int v_cnt = 0; for (int i = 1; i <= n; i++) if (best[i] == MIN_VC_FOUND) v_cnt++; std::cout << v_cnt << "\n"; for (int i = 1; i <= n; i++) if (best[i] == MIN_VC_FOUND) std::cout << i << " "; std::cout << "\n"; } else { std::cout << "-1\n"; } for (int i = 1; i <= n; i++) insert(i, 0); } int main() { std::ios_base::sync_with_stdio(0); std::cin.tie(NULL); int t; std::cin >> t; while (t--) solve(); } |