#include "bits/stdc++.h" // Tomasz Nowak using namespace std; // University of Warsaw using LL = long long; #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) #define ssize(x) int(x.size()) template<class A, class B> auto& operator<<(ostream &o, pair<A, B> p) { return o << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &o, T x) -> decltype(x.end(), o) { o << '{'; int i = 0; for(auto e : x) o << (", ")+2*!i++ << e; return o << '}'; } #ifdef DEBUG #define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n' #else #define debug(...) {} #endif vector<int> global_deg; vector<vector<int>> global_graph; vector<bool> global_vis; vector<int> global_ans; void backtrack( int remaining_k, vector<bool> &deleted_v, const vector<pair<int, int>> &edges) { int n = ssize(deleted_v); if(remaining_k < 0) return; debug(remaining_k, deleted_v, edges); fill(global_deg.begin(), global_deg.end(), 0); for(auto [v0, v1] : edges) if(not deleted_v[v0] and not deleted_v[v1]) for(int v : {v0, v1}) ++global_deg[v]; int max_deg_v = int(max_element(global_deg.begin(), global_deg.end()) - global_deg.begin()); int deg1_v = -1; REP(v, n) if(global_deg[v] == 1) deg1_v = v; debug(global_deg[max_deg_v], deg1_v); auto save_current_state = [&] { int in_ans = accumulate(deleted_v.begin(), deleted_v.end(), 0); if(in_ans < ssize(global_ans)) { global_ans.clear(); REP(v, n) if(deleted_v[v]) global_ans.emplace_back(v); } }; if(global_deg[max_deg_v] == 0) { save_current_state(); return; } if(remaining_k == 0) return; REP(v, n) global_graph[v].clear(); for(auto [v, u] : edges) if(not deleted_v[v] and not deleted_v[u]) { global_graph[v].emplace_back(u); global_graph[u].emplace_back(v); } if(deg1_v != -1) { int u = global_graph[deg1_v][0]; deleted_v[u] = true; backtrack(remaining_k - 1, deleted_v, edges); deleted_v[u] = false; return; } if(global_deg[max_deg_v] <= 2) { fill(global_vis.begin(), global_vis.end(), false); static vector<vector<int>> paths_or_cycles; paths_or_cycles.clear(); function<void (int)> dfs = [&](int s) { paths_or_cycles.back().emplace_back(s); global_vis[s] = true; for(int u : global_graph[s]) if(not global_vis[u]) dfs(u); }; REP(v, n) if(not global_vis[v] and global_deg[v] == 1) { paths_or_cycles.emplace_back(); dfs(v); } REP(v, n) if(not global_vis[v] and global_deg[v] == 2) { paths_or_cycles.emplace_back(); dfs(v); } static vector<int> to_del; to_del.clear(); for(auto &path_or_cycle : paths_or_cycles) for(int i = bool(global_deg[path_or_cycle[0]] == 1); i < ssize(path_or_cycle); i += 2) to_del.emplace_back(path_or_cycle[i]); if(ssize(to_del) <= remaining_k) { for(int v : to_del) deleted_v[v] = true; save_current_state(); for(int v : to_del) deleted_v[v] = false; } return; } vector<int> neighbors = global_graph[max_deg_v]; deleted_v[max_deg_v] = true; backtrack(remaining_k - 1, deleted_v, edges); deleted_v[max_deg_v] = false; for(int u : neighbors) deleted_v[u] = true; backtrack(remaining_k - ssize(neighbors), deleted_v, edges); for(int u : neighbors) deleted_v[u] = false; } int main() { cin.tie(0)->sync_with_stdio(0); int t; cin >> t; while(t --> 0) { int n, m, k; cin >> n >> m >> k; vector<pair<int, int>> edges(m); for(auto &[v, u] : edges) { cin >> v >> u; --v, --u; if(v > u) swap(v, u); } sort(edges.begin(), edges.end()); edges.erase(unique(edges.begin(), edges.end()), edges.end()); vector<int> deg(n); for(auto [v, u] : edges) { ++deg[v]; ++deg[u]; } debug(deg); vector<int> output_must_take; vector<int> new_id(n, -1); int smaller_n = 0; int smaller_k = k; REP(v, n) if(deg[v] > k) { output_must_take.emplace_back(v); --smaller_k; } else if(deg[v] > 0) new_id[v] = smaller_n++; debug(k, output_must_take); if(ssize(output_must_take) > k) { cout << "-1\n"; continue; } vector<pair<int, int>> smaller_edges; for(auto [v, u] : edges) { v = new_id[v]; u = new_id[u]; if(v != -1 and u != -1) smaller_edges.emplace_back(v, u); } debug(smaller_n, smaller_k, smaller_edges); global_deg.assign(smaller_n, 0); global_graph.assign(smaller_n, vector<int>(smaller_n)); global_vis.assign(smaller_n, false); int best_smaller_size = smaller_n + 1; vector<bool> in_some_best(smaller_n); if(smaller_edges.empty()) best_smaller_size = 0; else if(smaller_k > 0) { vector<bool> deleted_v(smaller_n); global_ans.assign(smaller_n + 1, -1); backtrack(smaller_k, deleted_v, smaller_edges); debug("root", global_ans, deleted_v); best_smaller_size = ssize(global_ans); if(best_smaller_size < smaller_n + 1) { for(int v : global_ans) in_some_best[v] = true; REP(which_taking_v, smaller_n) if(not in_some_best[which_taking_v]) { deleted_v[which_taking_v] = true; global_ans.assign(smaller_n + 1, -1); backtrack(best_smaller_size - 1, deleted_v, smaller_edges); int sz = ssize(global_ans); deleted_v[which_taking_v] = false; debug(which_taking_v, sz); if(sz == best_smaller_size) { in_some_best[which_taking_v] = true; for(int u : global_ans) in_some_best[u] = true; } } } } if(best_smaller_size >= smaller_n + 1) { cout << "-1\n"; continue; } vector<int> old_id(smaller_n, -1); REP(v, n) if(new_id[v] != -1) old_id[new_id[v]] = v; vector<int> output = output_must_take; REP(v, smaller_n) if(in_some_best[v]) output.emplace_back(old_id[v]); sort(output.begin(), output.end()); cout << best_smaller_size + ssize(output_must_take) << ' ' << ssize(output) << '\n'; for(int v : output) cout << v + 1 << ' '; cout << '\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 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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | #include "bits/stdc++.h" // Tomasz Nowak using namespace std; // University of Warsaw using LL = long long; #define FOR(i, l, r) for(int i = (l); i <= (r); ++i) #define REP(i, n) FOR(i, 0, (n) - 1) #define ssize(x) int(x.size()) template<class A, class B> auto& operator<<(ostream &o, pair<A, B> p) { return o << '(' << p.first << ", " << p.second << ')'; } template<class T> auto operator<<(ostream &o, T x) -> decltype(x.end(), o) { o << '{'; int i = 0; for(auto e : x) o << (", ")+2*!i++ << e; return o << '}'; } #ifdef DEBUG #define debug(x...) cerr << "[" #x "]: ", [](auto... $) {((cerr << $ << "; "), ...); }(x), cerr << '\n' #else #define debug(...) {} #endif vector<int> global_deg; vector<vector<int>> global_graph; vector<bool> global_vis; vector<int> global_ans; void backtrack( int remaining_k, vector<bool> &deleted_v, const vector<pair<int, int>> &edges) { int n = ssize(deleted_v); if(remaining_k < 0) return; debug(remaining_k, deleted_v, edges); fill(global_deg.begin(), global_deg.end(), 0); for(auto [v0, v1] : edges) if(not deleted_v[v0] and not deleted_v[v1]) for(int v : {v0, v1}) ++global_deg[v]; int max_deg_v = int(max_element(global_deg.begin(), global_deg.end()) - global_deg.begin()); int deg1_v = -1; REP(v, n) if(global_deg[v] == 1) deg1_v = v; debug(global_deg[max_deg_v], deg1_v); auto save_current_state = [&] { int in_ans = accumulate(deleted_v.begin(), deleted_v.end(), 0); if(in_ans < ssize(global_ans)) { global_ans.clear(); REP(v, n) if(deleted_v[v]) global_ans.emplace_back(v); } }; if(global_deg[max_deg_v] == 0) { save_current_state(); return; } if(remaining_k == 0) return; REP(v, n) global_graph[v].clear(); for(auto [v, u] : edges) if(not deleted_v[v] and not deleted_v[u]) { global_graph[v].emplace_back(u); global_graph[u].emplace_back(v); } if(deg1_v != -1) { int u = global_graph[deg1_v][0]; deleted_v[u] = true; backtrack(remaining_k - 1, deleted_v, edges); deleted_v[u] = false; return; } if(global_deg[max_deg_v] <= 2) { fill(global_vis.begin(), global_vis.end(), false); static vector<vector<int>> paths_or_cycles; paths_or_cycles.clear(); function<void (int)> dfs = [&](int s) { paths_or_cycles.back().emplace_back(s); global_vis[s] = true; for(int u : global_graph[s]) if(not global_vis[u]) dfs(u); }; REP(v, n) if(not global_vis[v] and global_deg[v] == 1) { paths_or_cycles.emplace_back(); dfs(v); } REP(v, n) if(not global_vis[v] and global_deg[v] == 2) { paths_or_cycles.emplace_back(); dfs(v); } static vector<int> to_del; to_del.clear(); for(auto &path_or_cycle : paths_or_cycles) for(int i = bool(global_deg[path_or_cycle[0]] == 1); i < ssize(path_or_cycle); i += 2) to_del.emplace_back(path_or_cycle[i]); if(ssize(to_del) <= remaining_k) { for(int v : to_del) deleted_v[v] = true; save_current_state(); for(int v : to_del) deleted_v[v] = false; } return; } vector<int> neighbors = global_graph[max_deg_v]; deleted_v[max_deg_v] = true; backtrack(remaining_k - 1, deleted_v, edges); deleted_v[max_deg_v] = false; for(int u : neighbors) deleted_v[u] = true; backtrack(remaining_k - ssize(neighbors), deleted_v, edges); for(int u : neighbors) deleted_v[u] = false; } int main() { cin.tie(0)->sync_with_stdio(0); int t; cin >> t; while(t --> 0) { int n, m, k; cin >> n >> m >> k; vector<pair<int, int>> edges(m); for(auto &[v, u] : edges) { cin >> v >> u; --v, --u; if(v > u) swap(v, u); } sort(edges.begin(), edges.end()); edges.erase(unique(edges.begin(), edges.end()), edges.end()); vector<int> deg(n); for(auto [v, u] : edges) { ++deg[v]; ++deg[u]; } debug(deg); vector<int> output_must_take; vector<int> new_id(n, -1); int smaller_n = 0; int smaller_k = k; REP(v, n) if(deg[v] > k) { output_must_take.emplace_back(v); --smaller_k; } else if(deg[v] > 0) new_id[v] = smaller_n++; debug(k, output_must_take); if(ssize(output_must_take) > k) { cout << "-1\n"; continue; } vector<pair<int, int>> smaller_edges; for(auto [v, u] : edges) { v = new_id[v]; u = new_id[u]; if(v != -1 and u != -1) smaller_edges.emplace_back(v, u); } debug(smaller_n, smaller_k, smaller_edges); global_deg.assign(smaller_n, 0); global_graph.assign(smaller_n, vector<int>(smaller_n)); global_vis.assign(smaller_n, false); int best_smaller_size = smaller_n + 1; vector<bool> in_some_best(smaller_n); if(smaller_edges.empty()) best_smaller_size = 0; else if(smaller_k > 0) { vector<bool> deleted_v(smaller_n); global_ans.assign(smaller_n + 1, -1); backtrack(smaller_k, deleted_v, smaller_edges); debug("root", global_ans, deleted_v); best_smaller_size = ssize(global_ans); if(best_smaller_size < smaller_n + 1) { for(int v : global_ans) in_some_best[v] = true; REP(which_taking_v, smaller_n) if(not in_some_best[which_taking_v]) { deleted_v[which_taking_v] = true; global_ans.assign(smaller_n + 1, -1); backtrack(best_smaller_size - 1, deleted_v, smaller_edges); int sz = ssize(global_ans); deleted_v[which_taking_v] = false; debug(which_taking_v, sz); if(sz == best_smaller_size) { in_some_best[which_taking_v] = true; for(int u : global_ans) in_some_best[u] = true; } } } } if(best_smaller_size >= smaller_n + 1) { cout << "-1\n"; continue; } vector<int> old_id(smaller_n, -1); REP(v, n) if(new_id[v] != -1) old_id[new_id[v]] = v; vector<int> output = output_must_take; REP(v, smaller_n) if(in_some_best[v]) output.emplace_back(old_id[v]); sort(output.begin(), output.end()); cout << best_smaller_size + ssize(output_must_take) << ' ' << ssize(output) << '\n'; for(int v : output) cout << v + 1 << ' '; cout << '\n'; } } |