#include<bits/stdc++.h> #define EDGE pair<int, int> #define f first #define s second using namespace std; constexpr int MAXN = 1e3 + 7; constexpr int MAXM = 3e3 + 7; bitset <MAXN> canbe; set <EDGE> edges; // <a, b> in edges <=> a < b int ok; int ook; bitset <MAXN> used; vector <int> graf[MAXN]; void rmv(int w) { used[w] = true; for (int i : graf[w]){ if (used[i]) continue; edges.erase({min(i, w), max(i, w)}); } } void add(int w) { used[w] = false; for (int i : graf[w]){ if (used[i]) continue; edges.insert({min(i, w), max(i, w)}); } } bool exists(int ck) // set ok and edges before using { if (edges.size() == 0) return true; if (edges.size() >= ck * ook) return false; EDGE e = *edges.begin(); rmv(e.f); bool res = exists(ck-1); add(e.f); if (res) return true; rmv(e.s); res = exists(ck-1); add(e.s); return res; } bool ex_mark(int ck) { if (edges.size() == 0) return true; if (edges.size() >= ck * ook) return false; EDGE e = *edges.begin(); if (canbe[e.f]) swap(e.f, e.s); rmv(e.f); bool res = exists(ck-1); add(e.f); if (res) canbe[e.f] = true; bool res2 = false; if (!res || !canbe[e.s]){ rmv(e.s); res2 = exists(ck-1); add(e.s); if (res2) canbe[e.s] = true; } return res || res2; } void cleanv(vector<int>& v) { vector <int> empt; swap(v, empt); } void clean() { used = 0; canbe = 0; for (int i = 0; i < MAXN; i++) cleanv(graf[i]); set <EDGE> emptset; swap(edges, emptset); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int tcase; cin >> tcase; while (tcase--) { clean(); int n, m, k; cin >> n >> m >> k; vector <int> deg(n + 1); for (int i = 0; i <= n; i++) deg[i] = 0; for (int i = 0; i < m; i++){ int a, b; cin >> a >> b; graf[a].push_back(b); graf[b].push_back(a); deg[a]++; deg[b]++; edges.insert({min(a, b), max(a, b)}); } ok = k; ook = k; for (int i = 1; i <= n; i++){ if (deg[i] > ook){ ok--; rmv(i); canbe[i] = true; } } if (ok < 0 || !exists(ok)) { cout << "-1\n"; continue; } while (exists(ok - 1)) ok--; cout << ok << " "; // odzysk wynik vector <int> v; for (int i = 1; i <= n; i++){ if (canbe[i]){ v.push_back(i); continue; } rmv(i); if (ex_mark(ok - 1)) v.push_back(i); add(i); } cout << v.size() << "\n"; for (int i : v) cout << i << " "; cout << "\n"; } 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 167 168 169 170 | #include<bits/stdc++.h> #define EDGE pair<int, int> #define f first #define s second using namespace std; constexpr int MAXN = 1e3 + 7; constexpr int MAXM = 3e3 + 7; bitset <MAXN> canbe; set <EDGE> edges; // <a, b> in edges <=> a < b int ok; int ook; bitset <MAXN> used; vector <int> graf[MAXN]; void rmv(int w) { used[w] = true; for (int i : graf[w]){ if (used[i]) continue; edges.erase({min(i, w), max(i, w)}); } } void add(int w) { used[w] = false; for (int i : graf[w]){ if (used[i]) continue; edges.insert({min(i, w), max(i, w)}); } } bool exists(int ck) // set ok and edges before using { if (edges.size() == 0) return true; if (edges.size() >= ck * ook) return false; EDGE e = *edges.begin(); rmv(e.f); bool res = exists(ck-1); add(e.f); if (res) return true; rmv(e.s); res = exists(ck-1); add(e.s); return res; } bool ex_mark(int ck) { if (edges.size() == 0) return true; if (edges.size() >= ck * ook) return false; EDGE e = *edges.begin(); if (canbe[e.f]) swap(e.f, e.s); rmv(e.f); bool res = exists(ck-1); add(e.f); if (res) canbe[e.f] = true; bool res2 = false; if (!res || !canbe[e.s]){ rmv(e.s); res2 = exists(ck-1); add(e.s); if (res2) canbe[e.s] = true; } return res || res2; } void cleanv(vector<int>& v) { vector <int> empt; swap(v, empt); } void clean() { used = 0; canbe = 0; for (int i = 0; i < MAXN; i++) cleanv(graf[i]); set <EDGE> emptset; swap(edges, emptset); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int tcase; cin >> tcase; while (tcase--) { clean(); int n, m, k; cin >> n >> m >> k; vector <int> deg(n + 1); for (int i = 0; i <= n; i++) deg[i] = 0; for (int i = 0; i < m; i++){ int a, b; cin >> a >> b; graf[a].push_back(b); graf[b].push_back(a); deg[a]++; deg[b]++; edges.insert({min(a, b), max(a, b)}); } ok = k; ook = k; for (int i = 1; i <= n; i++){ if (deg[i] > ook){ ok--; rmv(i); canbe[i] = true; } } if (ok < 0 || !exists(ok)) { cout << "-1\n"; continue; } while (exists(ok - 1)) ok--; cout << ok << " "; // odzysk wynik vector <int> v; for (int i = 1; i <= n; i++){ if (canbe[i]){ v.push_back(i); continue; } rmv(i); if (ex_mark(ok - 1)) v.push_back(i); add(i); } cout << v.size() << "\n"; for (int i : v) cout << i << " "; cout << "\n"; } return 0; } |