// 2022-3-med-medrcy-brut.cpp : This file contains the 'main' function. Program execution begins and ends there. // #include <iostream> #include <vector> #include <set> #include <algorithm> constexpr int MAXN = 1e4; std::set<int> edges[MAXN]; std::set<int> not_single; std::set<int> good_vertices; bool visited[MAXN]; void dfs1(int v) { visited[v] = 1; for (auto u : edges[v]) { if (!visited[u]) { dfs1(u); } } } int how_many_components() { int how_many = 0; for (auto u : not_single) { if (!visited[u]) { how_many++; dfs1(u); } } for (auto u : not_single) { visited[u] = 0; } return how_many; } int minimum_val(int max_val); int handle_vertex(int max_val, int v) { std::set<int> ev = edges[v]; not_single.erase(v); // edges[v].clear(); for (auto u : ev) { edges[u].erase(v); if (edges[u].size() == 0) { not_single.erase(u); } } bool was_found = false; // the vertex is not taken if (max_val >= ev.size()) { for (auto u : ev) { for (auto w : edges[u]) { edges[w].erase(u); if (edges[w].size() == 0) { not_single.erase(w); } } not_single.erase(u); } int tmp1 = minimum_val(max_val - ev.size()); if (tmp1 != -1) { was_found = 1; max_val = tmp1 + ev.size(); good_vertices.insert(ev.begin(), ev.end()); } for (auto u : ev) { for (auto w : edges[u]) { edges[w].insert(u); if (edges[w].size() > 0) { not_single.insert(w); } } if (edges[u].size() > 0) { not_single.insert(u); } } } // the vertex is taken int tmp1 = minimum_val(max_val - 1); if (tmp1 != -1) { was_found = true; max_val = tmp1 + 1; good_vertices.insert(v); } for (auto u : ev) { edges[u].insert(v); not_single.insert(u); } not_single.insert(v); if (!was_found) { return -1; } return max_val; } int minimum_val(int max_val) { if (not_single.empty()) { if (max_val > 0) { good_vertices.clear(); } return 0; } if (max_val <= 0) { return -1; } if (how_many_components() > max_val) { return -1; } size_t mx_deg = 0; for (auto u : not_single) { mx_deg = std::max(mx_deg, edges[u].size()); } for (auto u : not_single) { if (mx_deg == edges[u].size()) { return handle_vertex(max_val, u); } } } int main() { int qqq, n, m, k, u, v, res; std::cin >> qqq; while (qqq--) { std::cin >> n >> m >> k; for (int i = 0; i <= n; i++) { edges[i].clear(); } good_vertices.clear(); not_single.clear(); for (int i = 0; i < m; i++) { std::cin >> u >> v; edges[u].insert(v); edges[v].insert(u); not_single.insert(u); not_single.insert(v); } res = minimum_val(k); if (res == -1) { std::cout << "-1\n"; continue; } std::cout << res << ' ' << good_vertices.size() << '\n'; for (auto v : good_vertices) { std::cout << v << ' '; } std::cout << '\n'; } } // Run program: Ctrl + F5 or Debug > Start Without Debugging menu // Debug program: F5 or Debug > Start Debugging menu // Tips for Getting Started: // 1. Use the Solution Explorer window to add/manage files // 2. Use the Team Explorer window to connect to source control // 3. Use the Output window to see build output and other messages // 4. Use the Error List window to view errors // 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
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 | // 2022-3-med-medrcy-brut.cpp : This file contains the 'main' function. Program execution begins and ends there. // #include <iostream> #include <vector> #include <set> #include <algorithm> constexpr int MAXN = 1e4; std::set<int> edges[MAXN]; std::set<int> not_single; std::set<int> good_vertices; bool visited[MAXN]; void dfs1(int v) { visited[v] = 1; for (auto u : edges[v]) { if (!visited[u]) { dfs1(u); } } } int how_many_components() { int how_many = 0; for (auto u : not_single) { if (!visited[u]) { how_many++; dfs1(u); } } for (auto u : not_single) { visited[u] = 0; } return how_many; } int minimum_val(int max_val); int handle_vertex(int max_val, int v) { std::set<int> ev = edges[v]; not_single.erase(v); // edges[v].clear(); for (auto u : ev) { edges[u].erase(v); if (edges[u].size() == 0) { not_single.erase(u); } } bool was_found = false; // the vertex is not taken if (max_val >= ev.size()) { for (auto u : ev) { for (auto w : edges[u]) { edges[w].erase(u); if (edges[w].size() == 0) { not_single.erase(w); } } not_single.erase(u); } int tmp1 = minimum_val(max_val - ev.size()); if (tmp1 != -1) { was_found = 1; max_val = tmp1 + ev.size(); good_vertices.insert(ev.begin(), ev.end()); } for (auto u : ev) { for (auto w : edges[u]) { edges[w].insert(u); if (edges[w].size() > 0) { not_single.insert(w); } } if (edges[u].size() > 0) { not_single.insert(u); } } } // the vertex is taken int tmp1 = minimum_val(max_val - 1); if (tmp1 != -1) { was_found = true; max_val = tmp1 + 1; good_vertices.insert(v); } for (auto u : ev) { edges[u].insert(v); not_single.insert(u); } not_single.insert(v); if (!was_found) { return -1; } return max_val; } int minimum_val(int max_val) { if (not_single.empty()) { if (max_val > 0) { good_vertices.clear(); } return 0; } if (max_val <= 0) { return -1; } if (how_many_components() > max_val) { return -1; } size_t mx_deg = 0; for (auto u : not_single) { mx_deg = std::max(mx_deg, edges[u].size()); } for (auto u : not_single) { if (mx_deg == edges[u].size()) { return handle_vertex(max_val, u); } } } int main() { int qqq, n, m, k, u, v, res; std::cin >> qqq; while (qqq--) { std::cin >> n >> m >> k; for (int i = 0; i <= n; i++) { edges[i].clear(); } good_vertices.clear(); not_single.clear(); for (int i = 0; i < m; i++) { std::cin >> u >> v; edges[u].insert(v); edges[v].insert(u); not_single.insert(u); not_single.insert(v); } res = minimum_val(k); if (res == -1) { std::cout << "-1\n"; continue; } std::cout << res << ' ' << good_vertices.size() << '\n'; for (auto v : good_vertices) { std::cout << v << ' '; } std::cout << '\n'; } } // Run program: Ctrl + F5 or Debug > Start Without Debugging menu // Debug program: F5 or Debug > Start Debugging menu // Tips for Getting Started: // 1. Use the Solution Explorer window to add/manage files // 2. Use the Team Explorer window to connect to source control // 3. Use the Output window to see build output and other messages // 4. Use the Error List window to view errors // 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file |