#include <bits/stdc++.h> using namespace std; template<class c> struct rge { c b, e; }; template<class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template<class c> auto dud(c* x) -> decltype(cerr << *x, 0); template<class c> char dud(...); struct debug { ~debug() { cerr << endl; } template<class c> typename \ enable_if<sizeof dud<c>(0) != 1, debug&>::type operator<<(c i) { cerr << boolalpha << i; return *this; } template<class c> typename \ enable_if<sizeof dud<c>(0) == 1, debug&>::type operator<<(c i) { return *this << range(begin(i), end(i)); } template<class c, class b> debug & operator <<(pair<b, c> d) { return *this << "(" << d.first << ", " << d.second << ")"; } template<class c> debug & operator <<(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) { *this << ", " + 2 * (it == d.b) << *it; } return *this << "]"; } }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " mt19937 gen(2137); void solve() { int n, m, k; cin >> n >> m >> k; if (n <= 25 && (1ll << n) * m <= 20'000'000) { vector<pair<int, int>> edges(m); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--; v--; edges[i] = make_pair(u, v); } int result = k + 1; long long who = 0; for (long long bitmask = 0; bitmask < (1ll << n); bitmask++) { if (__builtin_popcount(bitmask) > result) { continue; } bool ok = true; for (auto [u, v] : edges) { if (!(bitmask >> u & 1) && !(bitmask >> v & 1)) { ok = false; break; } } if (ok) { if (__builtin_popcount(bitmask) < result) { result = __builtin_popcount(bitmask); who = bitmask; } else if (__builtin_popcount(bitmask) == result) { who |= bitmask; } } } if (result == k + 1) { cout << -1 << '\n'; } else { cout << result << ' ' << __builtin_popcount(who) << '\n'; for (int bit = 0; bit < n; bit++) { if (who >> bit & 1) { cout << bit + 1 << ' '; } } cout << '\n'; } } else { vector<vector<int>> graph(n); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--; v--; graph[u].emplace_back(v); graph[v].emplace_back(u); } int result = k + 1; vector<bool> vertices(n); for (int rep = 0; rep < (n + m) * 20; rep++) { vector<int> order(n); iota(order.begin(), order.end(), 0); shuffle(order.begin(), order.end(), gen); vector<bool> active(n, true); int cnt = n; for (auto u : order) { bool ok = true; for (auto v : graph[u]) { if (!active[v]) { ok = false; break; } } if (ok) { active[u] = false; cnt--; } } if (cnt < result) { result = cnt; fill(vertices.begin(), vertices.end(), false); for (int u = 0; u < n; u++) { if (active[u]) { vertices[u] = true; } } } else { if (cnt == result) { for (int u = 0; u < n; u++) { if (active[u]) { vertices[u] = true; } } } } } if (result == k + 1) { cout << -1 << '\n'; } else { int cnt = 0; for (int u = 0; u < n; u++) { if (vertices[u]) { cnt++; } } cout << result << ' ' << cnt << '\n'; for (int u = 0; u < n; u++) { if (vertices[u]) { cout << u + 1 << ' '; } } cout << '\n'; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int test_cases; cin >> test_cases; while (test_cases--) { solve(); } 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 171 172 173 174 175 176 177 178 179 180 | #include <bits/stdc++.h> using namespace std; template<class c> struct rge { c b, e; }; template<class c> rge<c> range(c i, c j) { return rge<c>{i, j}; } template<class c> auto dud(c* x) -> decltype(cerr << *x, 0); template<class c> char dud(...); struct debug { ~debug() { cerr << endl; } template<class c> typename \ enable_if<sizeof dud<c>(0) != 1, debug&>::type operator<<(c i) { cerr << boolalpha << i; return *this; } template<class c> typename \ enable_if<sizeof dud<c>(0) == 1, debug&>::type operator<<(c i) { return *this << range(begin(i), end(i)); } template<class c, class b> debug & operator <<(pair<b, c> d) { return *this << "(" << d.first << ", " << d.second << ")"; } template<class c> debug & operator <<(rge<c> d) { *this << "["; for (auto it = d.b; it != d.e; ++it) { *this << ", " + 2 * (it == d.b) << *it; } return *this << "]"; } }; #define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " mt19937 gen(2137); void solve() { int n, m, k; cin >> n >> m >> k; if (n <= 25 && (1ll << n) * m <= 20'000'000) { vector<pair<int, int>> edges(m); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--; v--; edges[i] = make_pair(u, v); } int result = k + 1; long long who = 0; for (long long bitmask = 0; bitmask < (1ll << n); bitmask++) { if (__builtin_popcount(bitmask) > result) { continue; } bool ok = true; for (auto [u, v] : edges) { if (!(bitmask >> u & 1) && !(bitmask >> v & 1)) { ok = false; break; } } if (ok) { if (__builtin_popcount(bitmask) < result) { result = __builtin_popcount(bitmask); who = bitmask; } else if (__builtin_popcount(bitmask) == result) { who |= bitmask; } } } if (result == k + 1) { cout << -1 << '\n'; } else { cout << result << ' ' << __builtin_popcount(who) << '\n'; for (int bit = 0; bit < n; bit++) { if (who >> bit & 1) { cout << bit + 1 << ' '; } } cout << '\n'; } } else { vector<vector<int>> graph(n); for (int i = 0; i < m; i++) { int u, v; cin >> u >> v; u--; v--; graph[u].emplace_back(v); graph[v].emplace_back(u); } int result = k + 1; vector<bool> vertices(n); for (int rep = 0; rep < (n + m) * 20; rep++) { vector<int> order(n); iota(order.begin(), order.end(), 0); shuffle(order.begin(), order.end(), gen); vector<bool> active(n, true); int cnt = n; for (auto u : order) { bool ok = true; for (auto v : graph[u]) { if (!active[v]) { ok = false; break; } } if (ok) { active[u] = false; cnt--; } } if (cnt < result) { result = cnt; fill(vertices.begin(), vertices.end(), false); for (int u = 0; u < n; u++) { if (active[u]) { vertices[u] = true; } } } else { if (cnt == result) { for (int u = 0; u < n; u++) { if (active[u]) { vertices[u] = true; } } } } } if (result == k + 1) { cout << -1 << '\n'; } else { int cnt = 0; for (int u = 0; u < n; u++) { if (vertices[u]) { cnt++; } } cout << result << ' ' << cnt << '\n'; for (int u = 0; u < n; u++) { if (vertices[u]) { cout << u + 1 << ' '; } } cout << '\n'; } } } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); int test_cases; cin >> test_cases; while (test_cases--) { solve(); } return 0; } |