#include <bits/stdc++.h> using namespace std; typedef pair<int,int> PII; vector<PII> edges; bool taken[1005]; int deg[1005]; int n; vector<int> G[1005]; bool vis[1005]; bool ans[1005]; void dfs(int v, int & cnt, bool start){ vis[v] = true, cnt++; int ind=0; for(int u:G[v]){ if(start && ind == 1 && vis[u]) cnt++; if(!vis[u])dfs(u, cnt, false); ind++; } } int deg2(vector<PII> & edg){ for(PII & e: edg){ G[e.first].push_back(e.second); G[e.second].push_back(e.first); } int res = 0; for(int i=1;i<=n;i++){ int cnt=0; if(!vis[i]){ dfs(i, cnt, true); } res += cnt/2; } for(int i=1;i<=n;i++){ G[i].clear(); vis[i] = false; } return res; } bool VC(int k, vector<PII> & edg){ if(k < 0) return false; for(PII & e: edg){ deg[e.first]++; deg[e.second]++; } int v, maxd = 0; for(int i=1;i<=n;i++){ if(deg[i] > maxd) maxd = deg[i], v = i; deg[i] = 0; } if(maxd <= 2){ if(deg2(edg) <= k) return true; return false; } int k_new = k; for(PII & e: edg){ if(e.first == v) taken[e.second] = true, k_new--; if(e.second == v) taken[e.first] = true, k_new--; } vector<PII> edg_new; for(PII & e: edg) if(!taken[e.first] && !taken[e.second]) edg_new.push_back(e); if(VC(k_new, edg_new)){ for(PII & e: edg){ if(e.first == v) taken[e.second] = false, ans[e.second] = true; if(e.second == v) taken[e.first] = false, ans[e.first] = true; } return true; } for(PII & e: edg){ if(e.first == v) taken[e.second] = false; if(e.second == v) taken[e.first] = false; } taken[v] = true; edg_new.clear(); for(PII & e: edg) if(!taken[e.first] && !taken[e.second]) edg_new.push_back(e); if(VC(k-1, edg_new)){ taken[v] = false; ans[v] = true; return true; } taken[v] = false; return false; } void solve(){ int m,k; cin>>n>>m>>k; edges.clear(); for(int i=1;i<=n;i++) ans[i] = false; for(int i=0;i<m;i++){ int a,b; cin>>a>>b; edges.push_back({a,b}); } sort(edges.begin(), edges.end()); edges.erase(unique(edges.begin(), edges.end()), edges.end()); //random_shuffle(edges.begin(), edges.end()); for(int i=1;i<=k;i++){ if(VC(i, edges)){ vector<int> ansV; for(int j=1;j<=n;j++) if(!ans[j]){ taken[j] = true; vector<PII> edg_new; for(PII & e: edges) if(!taken[e.first] && !taken[e.second]) edg_new.push_back(e); if(VC(i-1, edg_new)) ans[j] = true; taken[j] = false; } for(int j=1;j<=n;j++) if(ans[j]) ansV.push_back(j); cout<<i<<" "<<ansV.size()<<endl; for(int u: ansV)cout<<u<<" "; cout<<endl; return; } } cout<<-1<<endl; } int main(){ ios_base::sync_with_stdio(0); int Z; cin>>Z; while(Z--)solve(); }
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 | #include <bits/stdc++.h> using namespace std; typedef pair<int,int> PII; vector<PII> edges; bool taken[1005]; int deg[1005]; int n; vector<int> G[1005]; bool vis[1005]; bool ans[1005]; void dfs(int v, int & cnt, bool start){ vis[v] = true, cnt++; int ind=0; for(int u:G[v]){ if(start && ind == 1 && vis[u]) cnt++; if(!vis[u])dfs(u, cnt, false); ind++; } } int deg2(vector<PII> & edg){ for(PII & e: edg){ G[e.first].push_back(e.second); G[e.second].push_back(e.first); } int res = 0; for(int i=1;i<=n;i++){ int cnt=0; if(!vis[i]){ dfs(i, cnt, true); } res += cnt/2; } for(int i=1;i<=n;i++){ G[i].clear(); vis[i] = false; } return res; } bool VC(int k, vector<PII> & edg){ if(k < 0) return false; for(PII & e: edg){ deg[e.first]++; deg[e.second]++; } int v, maxd = 0; for(int i=1;i<=n;i++){ if(deg[i] > maxd) maxd = deg[i], v = i; deg[i] = 0; } if(maxd <= 2){ if(deg2(edg) <= k) return true; return false; } int k_new = k; for(PII & e: edg){ if(e.first == v) taken[e.second] = true, k_new--; if(e.second == v) taken[e.first] = true, k_new--; } vector<PII> edg_new; for(PII & e: edg) if(!taken[e.first] && !taken[e.second]) edg_new.push_back(e); if(VC(k_new, edg_new)){ for(PII & e: edg){ if(e.first == v) taken[e.second] = false, ans[e.second] = true; if(e.second == v) taken[e.first] = false, ans[e.first] = true; } return true; } for(PII & e: edg){ if(e.first == v) taken[e.second] = false; if(e.second == v) taken[e.first] = false; } taken[v] = true; edg_new.clear(); for(PII & e: edg) if(!taken[e.first] && !taken[e.second]) edg_new.push_back(e); if(VC(k-1, edg_new)){ taken[v] = false; ans[v] = true; return true; } taken[v] = false; return false; } void solve(){ int m,k; cin>>n>>m>>k; edges.clear(); for(int i=1;i<=n;i++) ans[i] = false; for(int i=0;i<m;i++){ int a,b; cin>>a>>b; edges.push_back({a,b}); } sort(edges.begin(), edges.end()); edges.erase(unique(edges.begin(), edges.end()), edges.end()); //random_shuffle(edges.begin(), edges.end()); for(int i=1;i<=k;i++){ if(VC(i, edges)){ vector<int> ansV; for(int j=1;j<=n;j++) if(!ans[j]){ taken[j] = true; vector<PII> edg_new; for(PII & e: edges) if(!taken[e.first] && !taken[e.second]) edg_new.push_back(e); if(VC(i-1, edg_new)) ans[j] = true; taken[j] = false; } for(int j=1;j<=n;j++) if(ans[j]) ansV.push_back(j); cout<<i<<" "<<ansV.size()<<endl; for(int u: ansV)cout<<u<<" "; cout<<endl; return; } } cout<<-1<<endl; } int main(){ ios_base::sync_with_stdio(0); int Z; cin>>Z; while(Z--)solve(); } |