#ifdef LOCAL class SS { int x[0]; }; extern "C" SS __sanitizer_print_stack_trace(); #define __last_state \ } \ ; \ SS x { \ __sanitizer_print_stack_trace() #endif #include <bits/stdc++.h> using namespace std; #define endl "\n" #define mp make_pair #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #define pb push_back #define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10), cin.tie(0), cout.tie(0); #define rep(i, n) for (int i = 0; i < (n); ++i) #define fwd(i, a, b) for (int i = (a); i < (b); ++i) #define trav(a, x) for (auto &a : x) #define all(c) (c).begin(), (c).end() #define sz(X) (int)((X).size()) typedef long double ld; typedef unsigned long long ull; typedef long long ll; #ifdef LOCAL ostream &operator<<(ostream &out, string str) { for (char c : str) out << c; return out; } template <class L, class R> ostream &operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.st << ", " << p.nd << ")"; } template <class L, class R, class S> ostream &operator<<(ostream &out, tuple<L, R, S> p) { auto &[a, b, c] = p; return out << "(" << a << ", " << b << ", " << c << ")"; } template <class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) { out << '{'; for (auto it = a.begin(); it != a.end(); it = next(it)) out << (it != a.begin() ? ", " : "") << *it; return out << '}'; } void dump() { cerr << "\n"; } template <class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) #else #define debug(...) 42 #endif typedef vector<bool> vb; struct SOLVE { vector<pii> e; int n, limit; vb mask; int cnt; vector<vector<int>> g; vector<int> deg; vector<int> id; typedef pair<int, vb> piv; SOLVE(vector<pii> e, int n, int limit) : e(e), n(n), limit(limit), mask(n, false), cnt(0), g(n), deg(n), id(n, -1) { for (auto [a, b] : e) { g[a].push_back(b); g[b].push_back(a); deg[a]++; deg[b]++; } for (auto &N : g) sort(all(N)); } void add(int x, int p) { if (!mask[x]) { id[x] = p; mask[x] = true; for (auto y : g[x]) deg[y]--; cnt++; } } void remove(int x, int p) { if (mask[x] && id[x] == p) { mask[x] = false; for (auto y : g[x]) deg[y]++; cnt--; } } vb combine(vb &a, vb &b) { rep(i, sz(a)) a[i] = a[i] || b[i]; return a; } piv best(piv &a, piv &b) { if (a.st == b.st) return {a.st, combine(a.nd, b.nd)}; return min(a, b); } piv sum(piv &a, piv &b) { return {a.st + b.st, combine(a.nd, b.nd)}; } piv minVC_deg2() { auto vis = mask; vb result(n); vector<int> dist(n); int cnt = 0; fwd(k, 1, 3) rep(i, n) if (!vis[i] && deg[i] == k) { static vector<int> X; int j = 0; X.push_back(i); while (j != sz(X)) { auto x = X[j++]; vis[x] = true; for (int y : g[x]) if (!vis[y]) { vis[y] = true; X.push_back(y); dist[y] = dist[x] + 1; } } if (k == 1) { cnt += sz(X) / 2; for (int x : X) if (dist[x] % 2 || (sz(X) % 2 == 0)) result[x] = true; } if (k == 2) { cnt += (sz(X) + 1) / 2; for (auto x : X) result[x] = true; } X.clear(); } return {cnt, result}; } piv minVC() { if (cnt > limit) return {cnt, mask}; pii high_deg = {0, 0}; rep(i, n) if (!mask[i]) high_deg = max(high_deg, make_pair(deg[i], i)); auto [h, v] = high_deg; if (h == 0) return {cnt, mask}; if (cnt == limit) return {cnt + 1, mask}; if (h <= 2) { piv cur = {cnt, mask}; piv two = minVC_deg2(); return sum(cur, two); } piv res1, res2; add(v, v); res1 = minVC(); remove(v, v); for (auto t : g[v]) add(t, v); res2 = minVC(); for (auto t : g[v]) remove(t, v); return best(res1, res2); } piv solve() { auto res = minVC(); return res; } }; vi trans(vb mask) { vi res; rep(i, sz(mask)) if (mask[i]) res.push_back(i); return res; } int32_t main() { _upgrade; // TEST::solve(); int Z; cin >> Z; while (Z--) { int n, m, k; cin >> n >> m >> k; set<pii> TMP; rep(i, m) { int a, b; cin >> a >> b; TMP.insert(minmax({--a, --b})); } vector<pii> U(all(TMP)); auto [day, mask] = SOLVE(U, n, k).solve(); if (day > k) { cout << -1 << endl; } else { auto res = trans(mask); cout << day << " " << sz(res) << endl; for (auto x : res) cout << x + 1 << " "; cout << endl; } } }
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 | #ifdef LOCAL class SS { int x[0]; }; extern "C" SS __sanitizer_print_stack_trace(); #define __last_state \ } \ ; \ SS x { \ __sanitizer_print_stack_trace() #endif #include <bits/stdc++.h> using namespace std; #define endl "\n" #define mp make_pair #define st first #define nd second #define pii pair<int, int> #define vi vector<int> #define pb push_back #define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10), cin.tie(0), cout.tie(0); #define rep(i, n) for (int i = 0; i < (n); ++i) #define fwd(i, a, b) for (int i = (a); i < (b); ++i) #define trav(a, x) for (auto &a : x) #define all(c) (c).begin(), (c).end() #define sz(X) (int)((X).size()) typedef long double ld; typedef unsigned long long ull; typedef long long ll; #ifdef LOCAL ostream &operator<<(ostream &out, string str) { for (char c : str) out << c; return out; } template <class L, class R> ostream &operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.st << ", " << p.nd << ")"; } template <class L, class R, class S> ostream &operator<<(ostream &out, tuple<L, R, S> p) { auto &[a, b, c] = p; return out << "(" << a << ", " << b << ", " << c << ")"; } template <class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) { out << '{'; for (auto it = a.begin(); it != a.end(); it = next(it)) out << (it != a.begin() ? ", " : "") << *it; return out << '}'; } void dump() { cerr << "\n"; } template <class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) #else #define debug(...) 42 #endif typedef vector<bool> vb; struct SOLVE { vector<pii> e; int n, limit; vb mask; int cnt; vector<vector<int>> g; vector<int> deg; vector<int> id; typedef pair<int, vb> piv; SOLVE(vector<pii> e, int n, int limit) : e(e), n(n), limit(limit), mask(n, false), cnt(0), g(n), deg(n), id(n, -1) { for (auto [a, b] : e) { g[a].push_back(b); g[b].push_back(a); deg[a]++; deg[b]++; } for (auto &N : g) sort(all(N)); } void add(int x, int p) { if (!mask[x]) { id[x] = p; mask[x] = true; for (auto y : g[x]) deg[y]--; cnt++; } } void remove(int x, int p) { if (mask[x] && id[x] == p) { mask[x] = false; for (auto y : g[x]) deg[y]++; cnt--; } } vb combine(vb &a, vb &b) { rep(i, sz(a)) a[i] = a[i] || b[i]; return a; } piv best(piv &a, piv &b) { if (a.st == b.st) return {a.st, combine(a.nd, b.nd)}; return min(a, b); } piv sum(piv &a, piv &b) { return {a.st + b.st, combine(a.nd, b.nd)}; } piv minVC_deg2() { auto vis = mask; vb result(n); vector<int> dist(n); int cnt = 0; fwd(k, 1, 3) rep(i, n) if (!vis[i] && deg[i] == k) { static vector<int> X; int j = 0; X.push_back(i); while (j != sz(X)) { auto x = X[j++]; vis[x] = true; for (int y : g[x]) if (!vis[y]) { vis[y] = true; X.push_back(y); dist[y] = dist[x] + 1; } } if (k == 1) { cnt += sz(X) / 2; for (int x : X) if (dist[x] % 2 || (sz(X) % 2 == 0)) result[x] = true; } if (k == 2) { cnt += (sz(X) + 1) / 2; for (auto x : X) result[x] = true; } X.clear(); } return {cnt, result}; } piv minVC() { if (cnt > limit) return {cnt, mask}; pii high_deg = {0, 0}; rep(i, n) if (!mask[i]) high_deg = max(high_deg, make_pair(deg[i], i)); auto [h, v] = high_deg; if (h == 0) return {cnt, mask}; if (cnt == limit) return {cnt + 1, mask}; if (h <= 2) { piv cur = {cnt, mask}; piv two = minVC_deg2(); return sum(cur, two); } piv res1, res2; add(v, v); res1 = minVC(); remove(v, v); for (auto t : g[v]) add(t, v); res2 = minVC(); for (auto t : g[v]) remove(t, v); return best(res1, res2); } piv solve() { auto res = minVC(); return res; } }; vi trans(vb mask) { vi res; rep(i, sz(mask)) if (mask[i]) res.push_back(i); return res; } int32_t main() { _upgrade; // TEST::solve(); int Z; cin >> Z; while (Z--) { int n, m, k; cin >> n >> m >> k; set<pii> TMP; rep(i, m) { int a, b; cin >> a >> b; TMP.insert(minmax({--a, --b})); } vector<pii> U(all(TMP)); auto [day, mask] = SOLVE(U, n, k).solve(); if (day > k) { cout << -1 << endl; } else { auto res = trans(mask); cout << day << " " << sz(res) << endl; for (auto x : res) cout << x + 1 << " "; cout << endl; } } } |