#include <bits/stdc++.h> using namespace std; const int MAXN = 1010; const int MAXM = 3010; int n; set<int> G[MAXN]; int total_edges; bool cover[MAXN]; bool used[MAXN]; bool deg2_can_cover[MAXN]; bool scan_component(int v, vector<int> &comp) { cover[v] = true; comp.push_back(v); if (G[v].size() == 0) { return false; } if (G[v].size() == 1) { int x = *(G[v].begin()); if (!cover[x]) { scan_component(x, comp); } return false; } auto it = G[v].begin(); int x = *it; it++; int y = *it; if (!cover[x]) { bool c = scan_component(x, comp); if (!cover[y]) { reverse(comp.begin(), comp.end()); scan_component(y, comp); return false; } return c; } if (!cover[y]) { bool c = scan_component(y, comp); return c; } return true; } int deg2_min_vc() { for (int i = 1; i <= n; i++) { deg2_can_cover[i] = false; } int k = 0; for (int i = 1; i <= n; i++) { if (!used[i] && !cover[i]) { vector<int> comp; bool is_cycle = scan_component(i, comp); if (is_cycle) { //cycle k += ((int)comp.size() + 1) / 2; for (auto v : comp) { deg2_can_cover[v] = true; } } else { k += (int)comp.size() / 2; for (int i = 0; i < (int)comp.size(); i++) { if ((int)comp.size() % 2 == 0 || i % 2 == 1) { deg2_can_cover[comp[i]] = true; } } } } } for (int i = 1; i <= n; i++) { if (!used[i]) { cover[i] = false; } } return k; } int vc_branch(int k, set<int> *can_cover) { if (k < 0) { return -1; } int maxdeg = 0; int v = -1; for (int i = 1; i <= n; i++) { if (!used[i] && G[i].size() > maxdeg) { maxdeg = G[i].size(); v = i; } } if (maxdeg <= 2) { int k2 = deg2_min_vc(); if (k2 > k) { return -1; } for (int i = 1; i <= n; i++) { if (cover[i] || deg2_can_cover[i]) { if (can_cover != nullptr) { can_cover->insert(i); } } } return k - k2; } // pick v total_edges -= (int)G[v].size(); for (auto x : G[v]) { G[x].erase(v); } cover[v] = true; used[v] = true; int rpick = vc_branch(k - 1, can_cover); // pick N[v] cover[v] = false; for (auto x : G[v]) { total_edges -= (int)G[x].size(); cover[x] = true; used[x] = true; for (auto y : G[x]) { G[y].erase(x); } } int rleave = vc_branch(k - (int)G[v].size(), can_cover); // restore graph for (auto it = G[v].end(); it != G[v].begin(); ) { it--; int x = *it; total_edges += (int)G[x].size(); cover[x] = false; used[x] = false; for (auto y : G[x]) { G[y].insert(x); } } used[v] = false; for (auto x : G[v]) { G[x].insert(v); } total_edges += (int)G[v].size(); return max(rpick, rleave); } void test() { int k; scanf("%d%d%d", &n, &total_edges, &k); for (int i = 1; i <= n; i++) { G[i].clear(); } for (int i = 0; i < total_edges; i++) { int x, y; scanf("%d%d", &x, &y); G[x].insert(y); G[y].insert(x); } int s = vc_branch(k, nullptr); if (s == -1) { puts("-1"); return; } k = k - s; // min vertex cover set<int> verts; vc_branch(k, &verts); printf("%d %d\n", k, verts.size()); for (auto v : verts) printf("%d ", v); puts(""); } int main() { int t; scanf("%d", &t); while (t--) { test(); } }
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 | #include <bits/stdc++.h> using namespace std; const int MAXN = 1010; const int MAXM = 3010; int n; set<int> G[MAXN]; int total_edges; bool cover[MAXN]; bool used[MAXN]; bool deg2_can_cover[MAXN]; bool scan_component(int v, vector<int> &comp) { cover[v] = true; comp.push_back(v); if (G[v].size() == 0) { return false; } if (G[v].size() == 1) { int x = *(G[v].begin()); if (!cover[x]) { scan_component(x, comp); } return false; } auto it = G[v].begin(); int x = *it; it++; int y = *it; if (!cover[x]) { bool c = scan_component(x, comp); if (!cover[y]) { reverse(comp.begin(), comp.end()); scan_component(y, comp); return false; } return c; } if (!cover[y]) { bool c = scan_component(y, comp); return c; } return true; } int deg2_min_vc() { for (int i = 1; i <= n; i++) { deg2_can_cover[i] = false; } int k = 0; for (int i = 1; i <= n; i++) { if (!used[i] && !cover[i]) { vector<int> comp; bool is_cycle = scan_component(i, comp); if (is_cycle) { //cycle k += ((int)comp.size() + 1) / 2; for (auto v : comp) { deg2_can_cover[v] = true; } } else { k += (int)comp.size() / 2; for (int i = 0; i < (int)comp.size(); i++) { if ((int)comp.size() % 2 == 0 || i % 2 == 1) { deg2_can_cover[comp[i]] = true; } } } } } for (int i = 1; i <= n; i++) { if (!used[i]) { cover[i] = false; } } return k; } int vc_branch(int k, set<int> *can_cover) { if (k < 0) { return -1; } int maxdeg = 0; int v = -1; for (int i = 1; i <= n; i++) { if (!used[i] && G[i].size() > maxdeg) { maxdeg = G[i].size(); v = i; } } if (maxdeg <= 2) { int k2 = deg2_min_vc(); if (k2 > k) { return -1; } for (int i = 1; i <= n; i++) { if (cover[i] || deg2_can_cover[i]) { if (can_cover != nullptr) { can_cover->insert(i); } } } return k - k2; } // pick v total_edges -= (int)G[v].size(); for (auto x : G[v]) { G[x].erase(v); } cover[v] = true; used[v] = true; int rpick = vc_branch(k - 1, can_cover); // pick N[v] cover[v] = false; for (auto x : G[v]) { total_edges -= (int)G[x].size(); cover[x] = true; used[x] = true; for (auto y : G[x]) { G[y].erase(x); } } int rleave = vc_branch(k - (int)G[v].size(), can_cover); // restore graph for (auto it = G[v].end(); it != G[v].begin(); ) { it--; int x = *it; total_edges += (int)G[x].size(); cover[x] = false; used[x] = false; for (auto y : G[x]) { G[y].insert(x); } } used[v] = false; for (auto x : G[v]) { G[x].insert(v); } total_edges += (int)G[v].size(); return max(rpick, rleave); } void test() { int k; scanf("%d%d%d", &n, &total_edges, &k); for (int i = 1; i <= n; i++) { G[i].clear(); } for (int i = 0; i < total_edges; i++) { int x, y; scanf("%d%d", &x, &y); G[x].insert(y); G[y].insert(x); } int s = vc_branch(k, nullptr); if (s == -1) { puts("-1"); return; } k = k - s; // min vertex cover set<int> verts; vc_branch(k, &verts); printf("%d %d\n", k, verts.size()); for (auto v : verts) printf("%d ", v); puts(""); } int main() { int t; scanf("%d", &t); while (t--) { test(); } } |