The mathematical model of the subject is: Divide \ (\mathcal{n}\) a set \ (\mathcal{p1,p2,..., pn}\) into as many groups as possible, so that the set of sets in each group is equal to the complete set. The collection \ (\mathcal{pi}\) is a collection of computers \ (\mathcal{i}\) and their neighboring computers, each of which corresponds to a service in the topic.
Notice that \ (\mathcal{n}\) is small, you can apply the binary notation mentioned in the algorithm contest to represent these collections, that is, in the code,
Each collection \ (\mathcal{pi}\) is actually a non-negative integer. Enter some code as follows:
for(int i=0;i<n;i++){ int m,x; scanf("%d",&m); P[i]=1<<i; while(m--) { scanf("%d",&x); P[i]|=(1<<x); }}
For convenience, Nest Meng uses \ (\mathcal{cover (S)}\) to represent a number of \ (\\mathcal{pi}\) of the Set (binary representation), that is, these \ (\mathcal{pi}\) "Bitwise OR" on a numeric value.
for{int S=0;S<(1<<n);S++){ cover[S]=0; for(int i=0;i<n;i++) if(S & (1<<i)) cover[S]=P[i];}
It is not difficult to think of such dynamic planning: Using \ (\mathcal{f (S)}\) to represent the subset \ (s\) can be divided into how many groups, then
\ (\mathcal{f (S) =max (f (s-s0) | s0}\)\ (\text{is a subset of S,}\)\ (\mathcal{cover[so]}\)\ (\text{equals complete)}\)\ (\text{+1}\ )
\ (\text{(Focus!!) }\)\ (\text{here is an important tip: Enumerate}\)\ (\mathcal{s}\)\ (\text{subset}\)\ (\mathcal{s0}\) .
See the following code:
f[0]=0;int All=(1<<n)-1;for(int S=1;S<(1<<n);S++{ f[S]=0; for(int S0=S;S0;S0=(S0-1)&S) if(cover[S0]==All f[S]=max(f[S],f[S^S0]+1);}printf("Case %d: %d\n",++kase,f[ALl]);
How to analyze the time complexity of the above algorithm? \ (qwq\)? It is equal to the sum of subsets of all subsets of the complete \ (\mathcal{(,..., N)}\) , which can also make
\ (\mathcal{c (S)}\) represents the number of subsets of set S (it equals \ (\text{2}\)\ (\mathcal{^|} \)\ (\mathcal{^s}\)\ (\mathcal{^|} \)), the time complexity of the subject is
\ (\mathcal{sum (c (S) | s0}\)\ (\text{is a subset of (,..., N)}\).
Notice that the number of elements of the same set, the number of subsets is the same, Nest Meng can be the number of elements "merge similar elements." The number of elements is \ (k\) for a collection with
\ (\mathcal{c (n,k)}\) , where each set has a \ (\text{2}\)\ (\mathcal{^k}\) subset (a tall \ (\mathcal{new}\ ) \ (\mathcal{knowledge}\)),
So the time complexity of the subject is
\ (\mathcal{sum (C (n,k)}\)\ (\text{2}\)\ (\mathcal{^k}\)\ (\mathcal{=}\)\ (\ text{(2+1)}\)\ (\mathcal{^n}\)\ (\mathcal{=}\)\ (\text{3}\)\ (\mathcal{^n}\) the first equals sign uses the two-item theorem (but the inverse (qwq\))
The subject is more abstract, we take some time to carefully understand OH ~ ~
$UVA $11825 "$Hackers $ ' $Crackdown $"