#include <iostream> #include <vector> #include <bits/stdc++.h> #include <algorithm> int k; int n,m; std::vector<std::pair<int,std::vector<int>>> men; std::vector<bool> result; std::vector<bool> selected; void bactrack(int kt, int ilu){ //std::cout<<"kt"<<kt<<" "<<men[kt].first << " "<<ilu<<std::endl; if(kt==n+1){ int il=0; for(int i=0;i<=n;i++) if(selected[i]) il++; if(il<k){ k=il; for(int i=0;i<=n;i++) { result[i] = selected[i]; //if(selected[i])std::cout<<"s"<<i<<" "; } //std::cout<<std::endl<<ilu<<std::endl; } else { for (int i = 0; i <= n; i++) if (selected[i]) { result[i] = true; //std::cout << "a" << i << " "; } //std::cout<<std::endl<<ilu<<std::endl; } return; } if(selected[men[kt].first]) bactrack(kt+1,ilu); //std::cout<<n<<std::endl; bool all_selected=true; for(auto x:men[kt].second) if(!selected[x]){ //std::cout<<"not selected"<<x<<std::endl; all_selected=false; break; } if(all_selected==true) bactrack(kt+1,ilu); else { selected[men[kt].first] = true; if(ilu<k) bactrack(kt + 1, ilu + 1); selected[men[kt].first] = false; std::vector<int> lis; for(auto x:men[kt].second) if(!selected[x]){ //std::cout<<"sel "<<x<<std::endl; selected[x] = true; ilu++; lis.emplace_back(x); } //std::cout<<"xx"<<men[kt].first<<" "<<ilu<<std::endl; if(ilu<=k) bactrack(kt+1,ilu); for(auto x:lis) selected[x]=false; } } int main(){ std::ios_base::sync_with_stdio(0); int t; std::cin>>t; for(int tt=0;tt<t;tt++){ int a,b; std::cin>>n>>m>>k; result.clear(); men.clear(); selected.clear(); for(int i=0;i<=n;i++){ selected.emplace_back(false); result.emplace_back(false); men.emplace_back(i,std::vector<int>()); } for(int i=0;i<m;i++){ std::cin>>a>>b; men[a].second.emplace_back(b); men[b].second.emplace_back(a); } for(int i=0;i<=n;i++){ sort( men[i].second.begin(), men[i].second.end() ); men[i].second.erase( unique( men[i].second.begin(), men[i].second.end() ), men[i].second.end() ); } sort(men.begin(),men.end(),[](const std::pair<int,std::vector<int>> & a, const std::pair<int,std::vector<int>> & b) {return a.second.size() > b.second.size();}); //for(auto m:men) // std::cout<<"m "<<m.first<<" "<<m.second.size()<<std::endl; bactrack(0,0); int ilu=0; for(int i=0;i<=n;i++) ilu+=result[i]; if(ilu==0) std::cout<<-1<<std::endl; else { std::cout << k << " " << ilu << std::endl; for(int i=0;i<=n;i++) if(result[i]) std::cout<<i<<" "; std::cout<<std::endl; } } }
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 | #include <iostream> #include <vector> #include <bits/stdc++.h> #include <algorithm> int k; int n,m; std::vector<std::pair<int,std::vector<int>>> men; std::vector<bool> result; std::vector<bool> selected; void bactrack(int kt, int ilu){ //std::cout<<"kt"<<kt<<" "<<men[kt].first << " "<<ilu<<std::endl; if(kt==n+1){ int il=0; for(int i=0;i<=n;i++) if(selected[i]) il++; if(il<k){ k=il; for(int i=0;i<=n;i++) { result[i] = selected[i]; //if(selected[i])std::cout<<"s"<<i<<" "; } //std::cout<<std::endl<<ilu<<std::endl; } else { for (int i = 0; i <= n; i++) if (selected[i]) { result[i] = true; //std::cout << "a" << i << " "; } //std::cout<<std::endl<<ilu<<std::endl; } return; } if(selected[men[kt].first]) bactrack(kt+1,ilu); //std::cout<<n<<std::endl; bool all_selected=true; for(auto x:men[kt].second) if(!selected[x]){ //std::cout<<"not selected"<<x<<std::endl; all_selected=false; break; } if(all_selected==true) bactrack(kt+1,ilu); else { selected[men[kt].first] = true; if(ilu<k) bactrack(kt + 1, ilu + 1); selected[men[kt].first] = false; std::vector<int> lis; for(auto x:men[kt].second) if(!selected[x]){ //std::cout<<"sel "<<x<<std::endl; selected[x] = true; ilu++; lis.emplace_back(x); } //std::cout<<"xx"<<men[kt].first<<" "<<ilu<<std::endl; if(ilu<=k) bactrack(kt+1,ilu); for(auto x:lis) selected[x]=false; } } int main(){ std::ios_base::sync_with_stdio(0); int t; std::cin>>t; for(int tt=0;tt<t;tt++){ int a,b; std::cin>>n>>m>>k; result.clear(); men.clear(); selected.clear(); for(int i=0;i<=n;i++){ selected.emplace_back(false); result.emplace_back(false); men.emplace_back(i,std::vector<int>()); } for(int i=0;i<m;i++){ std::cin>>a>>b; men[a].second.emplace_back(b); men[b].second.emplace_back(a); } for(int i=0;i<=n;i++){ sort( men[i].second.begin(), men[i].second.end() ); men[i].second.erase( unique( men[i].second.begin(), men[i].second.end() ), men[i].second.end() ); } sort(men.begin(),men.end(),[](const std::pair<int,std::vector<int>> & a, const std::pair<int,std::vector<int>> & b) {return a.second.size() > b.second.size();}); //for(auto m:men) // std::cout<<"m "<<m.first<<" "<<m.second.size()<<std::endl; bactrack(0,0); int ilu=0; for(int i=0;i<=n;i++) ilu+=result[i]; if(ilu==0) std::cout<<-1<<std::endl; else { std::cout << k << " " << ilu << std::endl; for(int i=0;i<=n;i++) if(result[i]) std::cout<<i<<" "; std::cout<<std::endl; } } } |