//~ while (clock()<=69*CLOCKS_PER_SEC) //~ #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("O3") //~ #pragma GCC optimize("Ofast") //~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //~ #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; using ll=long long; using ld=long double; using pii=pair<int, int>; using pll=pair<ll, ll>; using vi=vector<int>; using vll=vector<ll>; template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; } #ifdef LOCAL #define _upgrade ios_base::sync_with_stdio(0); #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__); #else #define _upgrade ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define debug(...) 98; #define cerr if(0) cout #endif #define pb push_back #define mp make_pair #define all(x) x.begin(),x.end() #define sz(x) ((int)(x).size()) #define fi first #define se second #define erase_duplicates(x) sort(all(x)), (x).resize(distance((x).begin(), unique(all(x)))); #define watch(x) cout << (#x) << " is " << (x) << endl; #define pow2(x) ((x)*(x)) #define mod(x, m) ((((x) % (m)) + (m)) % (m)) #define max3(a, b, c) max(a, max(b, c)) #define min3(a, b, c) min(a, min(b, c)) #define REP(i, a, b) for(int i = a; i <= b; ++i) #define REPV(i, b, a) for(int i = b; i >= a; --i) const double m_pi = acos(-1.0); const int int_max = 0x3f3f3f3f; const int M7 = 1000*1000*1000+7; const int M9 = 1000*1000*1000+9; vector<int> tos[1009]; int deg[1009]; priority_queue<pii> promising; int n, m, k; int maxDepth; int degToScore(int x) { if(x >= 4) { return x; } else if(x == 3) { return 5; } else if(x == 2) { return 10; } else if(x == 1) { return 100; } return x; } vector<pii> removeEdges(vector<int> vertices) { vector<pii> edges; vector<int> affected; // cout <<"sz(vertices) "<<sz(vertices)<<endl; REP(i, 0, sz(vertices) - 1) { int v = vertices[i]; affected.pb(v); // cout << "v "<<v<<endl; while(sz(tos[v])){ int u = tos[v][sz(tos[v])-1]; // cout << "u "<<u<<endl; tos[v].pop_back(); vector<int> tosu; REP(j, 0, sz(tos[u])-1) { if(tos[u][j] != v)tosu.pb(tos[u][j]); } tos[u] = tosu; affected.pb(u); --deg[u]; --deg[v]; edges.pb(mp(v, u)); // cout <<"remove edge "<<v<<" "<<u<<endl; } } erase_duplicates(affected); REP(i, 0, sz(affected)-1){ int x = affected[i]; if(deg[x]) { promising.push(mp(degToScore(deg[x]), x)); } } return edges; } void addEdges(vector<pii> edges) { vector<int> affected; REP(i, 0, sz(edges) - 1) { int u = edges[i].fi; int v = edges[i].se; affected.pb(u); affected.pb(v); tos[v].pb(u); tos[u].pb(v); ++deg[u]; ++deg[v]; } erase_duplicates(affected); REP(i, 0, sz(affected)-1){ int x = affected[i]; if(deg[x]) { promising.push(mp(degToScore(deg[x]), x)); } } } vector<int> vCov(int depth, bool isExpress) { vector<int> ret; if(depth > maxDepth + 2) return ret; int pos = -1; int score = 0; while(!promising.empty()) { pii topEl = promising.top(); // cout << "topEl "<<topEl.fi<<" "<<topEl.se<<endl; promising.pop(); if(degToScore(deg[topEl.se]) == topEl.fi) { pos = topEl.se; score = topEl.fi; break; } } // cout << "pos"<<pos<<endl; if(pos == -1) return ret; if(deg[pos] <= 3) { int newPos = pos; int maxDeg = deg[pos]; REP(i, 0, sz(tos[pos])-1) { int x = tos[pos][i]; if(deg[x] >= maxDeg){ maxDeg = deg[x]; newPos = x; } } pos = newPos; } // cout << "pq size "<<sz(promising)<<endl; // cout << "will remove edges "<<pos<<endl; // cout << deg[pos] << " " << sz(tos[pos]) << endl; vector<int> active; active.pb(pos); vector<pii> edges = removeEdges(active); // cout << "rek "<<endl; vector<int> pickPos = vCov(depth+1, isExpress); pickPos.pb(pos); vector<int> pickOther = pickPos; if(score < maxDepth + 3) { if ((depth + sz(pickPos) > maxDepth) || !isExpress) { // cout << "second branch" << endl; active.pop_back(); REP(i, 0, sz(edges)-1) { active.pb(edges[i].se); } vector<pii> edges2 = removeEdges(active); pickOther = vCov(depth + sz(active), isExpress); REP(i, 0, sz(active)-1) { pickOther.pb(active[i]); } edges.insert( edges.end(), edges2.begin(), edges2.end()); // cout << "second branch end"<<endl; } } // cout << "qwe"<<endl; addEdges(edges); if (sz(pickPos) < sz(pickOther)) { return pickPos; } else { return pickOther; } } int main() { _upgrade mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); cout<<fixed<<setprecision(15); int t;cin>>t; REP(tt, 0, t-1) { cin>>n>>m>>k; maxDepth = k; vector<pii> allEdges; REP(i, 0, m-1){ int a, b;cin>>a>>b;--a;--b; allEdges.pb(mp(a, b)); } erase_duplicates(allEdges); REP(i, 0, sz(allEdges)-1){ int a = allEdges[i].fi; int b = allEdges[i].se; tos[a].pb(b); tos[b].pb(a); ++deg[a]; ++deg[b]; } REP(i, 0, n-1){ if(deg[i]){ promising.push(mp(degToScore(deg[i]), i)); } } // cout << "a"<<endl; vector<int> vertexCov = vCov(0, false); maxDepth = sz(vertexCov); int d = sz(vertexCov); if(d > k) { cout << "-1\n"; } else { vector<int> cursed = vertexCov; vector<int> core; REP(i, 0, sz(vertexCov)-1) { // cout << "another"<<endl; while(!promising.empty())promising.pop(); REP(j, 0, n-1){ if(deg[j]){ promising.push(mp(degToScore(deg[j]), j)); } } int u = vertexCov[i]; vector<int> neighbours; REP(j, 0, sz(tos[u])-1) { neighbours.push_back(tos[u][j]); } // if (!allCovered) { vector<pii> removedEdges = removeEdges(neighbours); vector<int> newCover = vCov(sz(neighbours) + sz(core), true); addEdges(removedEdges); // cout << "abc"<<endl; if (sz(vertexCov) == sz(newCover) + sz(neighbours) + sz(core)) { REP(j, 0, sz(neighbours)-1) { cursed.push_back(neighbours[j]); } } else { core.pb(u); vector<int> uu; uu.pb(u); removeEdges(uu); } removedEdges.clear(); // } neighbours.clear(); } erase_duplicates(cursed); sort(cursed.begin(),cursed.end()); cout << d<<" "<<sz(cursed)<<"\n"; REP(i, 0, sz(cursed)-1) { cout << cursed[i]+1<< " "; } cout << "\n"; core.clear(); } REP(i, 0, sz(allEdges)-1){ int a = allEdges[i].fi; int b = allEdges[i].se; tos[a].clear(); tos[b].clear(); deg[a] = 0; deg[b] = 0; } allEdges.clear(); while(!promising.empty())promising.pop(); } return 0; } /* __builtin_popcount count total set bits on a given integer (use __builtin_popcountll for long long). */
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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | //~ while (clock()<=69*CLOCKS_PER_SEC) //~ #pragma comment(linker, "/stack:200000000") #pragma GCC optimize("O3") //~ #pragma GCC optimize("Ofast") //~ #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") //~ #pragma GCC optimize("unroll-loops") #include <bits/stdc++.h> using namespace std; using ll=long long; using ld=long double; using pii=pair<int, int>; using pll=pair<ll, ll>; using vi=vector<int>; using vll=vector<ll>; template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<" = "<<h<<endl; } #ifdef LOCAL #define _upgrade ios_base::sync_with_stdio(0); #define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__); #else #define _upgrade ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0); #define debug(...) 98; #define cerr if(0) cout #endif #define pb push_back #define mp make_pair #define all(x) x.begin(),x.end() #define sz(x) ((int)(x).size()) #define fi first #define se second #define erase_duplicates(x) sort(all(x)), (x).resize(distance((x).begin(), unique(all(x)))); #define watch(x) cout << (#x) << " is " << (x) << endl; #define pow2(x) ((x)*(x)) #define mod(x, m) ((((x) % (m)) + (m)) % (m)) #define max3(a, b, c) max(a, max(b, c)) #define min3(a, b, c) min(a, min(b, c)) #define REP(i, a, b) for(int i = a; i <= b; ++i) #define REPV(i, b, a) for(int i = b; i >= a; --i) const double m_pi = acos(-1.0); const int int_max = 0x3f3f3f3f; const int M7 = 1000*1000*1000+7; const int M9 = 1000*1000*1000+9; vector<int> tos[1009]; int deg[1009]; priority_queue<pii> promising; int n, m, k; int maxDepth; int degToScore(int x) { if(x >= 4) { return x; } else if(x == 3) { return 5; } else if(x == 2) { return 10; } else if(x == 1) { return 100; } return x; } vector<pii> removeEdges(vector<int> vertices) { vector<pii> edges; vector<int> affected; // cout <<"sz(vertices) "<<sz(vertices)<<endl; REP(i, 0, sz(vertices) - 1) { int v = vertices[i]; affected.pb(v); // cout << "v "<<v<<endl; while(sz(tos[v])){ int u = tos[v][sz(tos[v])-1]; // cout << "u "<<u<<endl; tos[v].pop_back(); vector<int> tosu; REP(j, 0, sz(tos[u])-1) { if(tos[u][j] != v)tosu.pb(tos[u][j]); } tos[u] = tosu; affected.pb(u); --deg[u]; --deg[v]; edges.pb(mp(v, u)); // cout <<"remove edge "<<v<<" "<<u<<endl; } } erase_duplicates(affected); REP(i, 0, sz(affected)-1){ int x = affected[i]; if(deg[x]) { promising.push(mp(degToScore(deg[x]), x)); } } return edges; } void addEdges(vector<pii> edges) { vector<int> affected; REP(i, 0, sz(edges) - 1) { int u = edges[i].fi; int v = edges[i].se; affected.pb(u); affected.pb(v); tos[v].pb(u); tos[u].pb(v); ++deg[u]; ++deg[v]; } erase_duplicates(affected); REP(i, 0, sz(affected)-1){ int x = affected[i]; if(deg[x]) { promising.push(mp(degToScore(deg[x]), x)); } } } vector<int> vCov(int depth, bool isExpress) { vector<int> ret; if(depth > maxDepth + 2) return ret; int pos = -1; int score = 0; while(!promising.empty()) { pii topEl = promising.top(); // cout << "topEl "<<topEl.fi<<" "<<topEl.se<<endl; promising.pop(); if(degToScore(deg[topEl.se]) == topEl.fi) { pos = topEl.se; score = topEl.fi; break; } } // cout << "pos"<<pos<<endl; if(pos == -1) return ret; if(deg[pos] <= 3) { int newPos = pos; int maxDeg = deg[pos]; REP(i, 0, sz(tos[pos])-1) { int x = tos[pos][i]; if(deg[x] >= maxDeg){ maxDeg = deg[x]; newPos = x; } } pos = newPos; } // cout << "pq size "<<sz(promising)<<endl; // cout << "will remove edges "<<pos<<endl; // cout << deg[pos] << " " << sz(tos[pos]) << endl; vector<int> active; active.pb(pos); vector<pii> edges = removeEdges(active); // cout << "rek "<<endl; vector<int> pickPos = vCov(depth+1, isExpress); pickPos.pb(pos); vector<int> pickOther = pickPos; if(score < maxDepth + 3) { if ((depth + sz(pickPos) > maxDepth) || !isExpress) { // cout << "second branch" << endl; active.pop_back(); REP(i, 0, sz(edges)-1) { active.pb(edges[i].se); } vector<pii> edges2 = removeEdges(active); pickOther = vCov(depth + sz(active), isExpress); REP(i, 0, sz(active)-1) { pickOther.pb(active[i]); } edges.insert( edges.end(), edges2.begin(), edges2.end()); // cout << "second branch end"<<endl; } } // cout << "qwe"<<endl; addEdges(edges); if (sz(pickPos) < sz(pickOther)) { return pickPos; } else { return pickOther; } } int main() { _upgrade mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); cout<<fixed<<setprecision(15); int t;cin>>t; REP(tt, 0, t-1) { cin>>n>>m>>k; maxDepth = k; vector<pii> allEdges; REP(i, 0, m-1){ int a, b;cin>>a>>b;--a;--b; allEdges.pb(mp(a, b)); } erase_duplicates(allEdges); REP(i, 0, sz(allEdges)-1){ int a = allEdges[i].fi; int b = allEdges[i].se; tos[a].pb(b); tos[b].pb(a); ++deg[a]; ++deg[b]; } REP(i, 0, n-1){ if(deg[i]){ promising.push(mp(degToScore(deg[i]), i)); } } // cout << "a"<<endl; vector<int> vertexCov = vCov(0, false); maxDepth = sz(vertexCov); int d = sz(vertexCov); if(d > k) { cout << "-1\n"; } else { vector<int> cursed = vertexCov; vector<int> core; REP(i, 0, sz(vertexCov)-1) { // cout << "another"<<endl; while(!promising.empty())promising.pop(); REP(j, 0, n-1){ if(deg[j]){ promising.push(mp(degToScore(deg[j]), j)); } } int u = vertexCov[i]; vector<int> neighbours; REP(j, 0, sz(tos[u])-1) { neighbours.push_back(tos[u][j]); } // if (!allCovered) { vector<pii> removedEdges = removeEdges(neighbours); vector<int> newCover = vCov(sz(neighbours) + sz(core), true); addEdges(removedEdges); // cout << "abc"<<endl; if (sz(vertexCov) == sz(newCover) + sz(neighbours) + sz(core)) { REP(j, 0, sz(neighbours)-1) { cursed.push_back(neighbours[j]); } } else { core.pb(u); vector<int> uu; uu.pb(u); removeEdges(uu); } removedEdges.clear(); // } neighbours.clear(); } erase_duplicates(cursed); sort(cursed.begin(),cursed.end()); cout << d<<" "<<sz(cursed)<<"\n"; REP(i, 0, sz(cursed)-1) { cout << cursed[i]+1<< " "; } cout << "\n"; core.clear(); } REP(i, 0, sz(allEdges)-1){ int a = allEdges[i].fi; int b = allEdges[i].se; tos[a].clear(); tos[b].clear(); deg[a] = 0; deg[b] = 0; } allEdges.clear(); while(!promising.empty())promising.pop(); } return 0; } /* __builtin_popcount count total set bits on a given integer (use __builtin_popcountll for long long). */ |