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
| #include <iostream> #include <vector> #include <deque> #include <bitset>
const int MAX_N=25;
using namespace std;
void divideGraph(const vector<bitset<MAX_N>> &graph,vector<vector<bitset<MAX_N>>> &vecChildGraph) {
size_t N =graph.size(); vector<size_t> vecVisited(N,false); for(size_t i=0;i<N;++i) { if(!vecVisited[i]) { vector<bitset<MAX_N>> ChildGraph; deque<size_t> que; que.push_back(i); vecVisited[i]=true; size_t front; while(!que.empty()) { front = que.front(); que.pop_front(); ChildGraph.push_back(graph[front]); for(size_t pos=0;pos<N;pos++) { if(graph[front].test(pos) && !vecVisited[pos]) { que.push_back(pos); vecVisited[pos]=true; } } } vecChildGraph.push_back(ChildGraph); } } }
void calBackCover(vector<vector<bitset<MAX_N>>> &vecChildGraph,vector<vector<bitset<MAX_N>>> &vecBackCover) { for(size_t idx=0;idx<vecChildGraph.size();idx++) { const vector<bitset<MAX_N>> &childGraph = vecChildGraph[idx]; size_t node = childGraph.size(); vecBackCover.push_back(vector<bitset<MAX_N>>(node,bitset<MAX_N>())); bitset<MAX_N> BackCover; for(;node>0;--node) { vecBackCover[idx][node-1]=childGraph[node-1]|BackCover; BackCover=vecBackCover[idx][node-1]; } } }
void DFS(const vector<bitset<MAX_N>> &graph,const vector<bitset<MAX_N>> &back,bitset<MAX_N> &cover,size_t curr,size_t cnt,const size_t bound,size_t &num) {
if(cnt==bound) { if(cover.count()==graph.size()) num=cnt; return; } bitset<MAX_N> newCover; for(size_t i=curr;i<graph.size();++i) { newCover = cover; newCover |=back[curr]; if(newCover.count()!=graph.size()) continue; newCover = cover; newCover |= graph[i]; if(newCover == cover) continue; DFS(graph,back,newCover,i+1,cnt+1,bound,num); if(num!=0) break; } }
int main() { size_t N=0,M=0; while(cin>>N>>M) { if(N==0 && M==0) break; vector<bitset<MAX_N>> graph(N,bitset<MAX_N>()); for(size_t v=0;v<N;++v) { graph[v].set(v); } size_t v1,v2; for(size_t m=0;m<M;++m) { cin>>v1>>v2; graph[v1-1].set(v2-1); graph[v2-1].set(v1-1); } vector<vector<bitset<MAX_N>>> vecChildGraph; vector<vector<bitset<MAX_N>>> vecBackCover; divideGraph(graph,vecChildGraph); calBackCover(vecChildGraph,vecBackCover); size_t total=0; for(size_t idx=0;idx<vecChildGraph.size();++idx) { const vector<bitset<MAX_N>> &childGraph = vecChildGraph[idx]; const vector<bitset<MAX_N>>& back=vecBackCover[idx]; bitset<MAX_N> cover; size_t num=0; for(size_t bound=1;bound<=childGraph.size();++bound) { DFS(childGraph,back,cover,0,0,bound,num); if(num!=0) break; } total+=num; } cout<<total<<endl; } return 0; }
|