The minimum number of questions for a tree.
The dominating set, which divides the points of the graph into two sets, all the points in the non-dominant set are adjacent to a point within the dominant set.
It is said that even the binary graph, the solution of the minimum dominating set is also not polynomial algorithm. And the tree of the minimum domination set tree DP is OK.
Each node on the tree can have three states as the root of its subtree:
- Not part of the domination set and not yet dominated.
- Not belonging to the domination set but controlled by its children
- belongs to the dominating set
Then it is to use dp[u][1\2\3] to indicate the state of the motion return.
123 How to transfer the transfer is how to transfer. The final result is min (dp[root][2],dp[root][3]).
Note that for some nodes the first 2 states may not exist, such as the leaf node does not exist in the 2nd state, there is no child is a leaf node node does not exist in the 1th state, these non-existent state to be processed at the time of transfer.
1#include <cstdio>2#include <cstring>3#include <algorithm>4 using namespacestd;5 #defineINF 1234566 #defineMAXN 1111117 structedge{8 intU,v,next;9}edge[maxn<<1];Ten intNE,HEAD[MAXN]; One voidAddedge (intUintv) { AEdge[ne].u=u; Edge[ne].v=v; edge[ne].next=Head[u]; -head[u]=ne++; - } the intd[maxn][3]; - intdpintUintKintFA) { - if(d[u][k]!=-1)returnD[u][k]; - intres=0, Diff=inf;BOOLflag=0, isleaf=1; + for(intI=head[u]; i!=-1; I=Edge[i].next) { - intv=edge[i].v; + if(V==FA)Continue; Aisleaf=0; at if(k==0){ - if(DP (V,1, u) ==inf)returnd[u][k]=INF; -RES+=DP (V,1, u); -}Else if(k==1){ - if(DP (V,2, u) <=dp (V,1, u)) { -RES+=DP (V,2, u); inflag=1; -}Else{ to if(DP (V,1, u) ==inf)returnd[u][k]=INF; +RES+=DP (V,1, u); -Diff=min (DIFF,DP (V,2, u)-dp (V,1, u)); the } *}Else{ $Res+=min (Min (DP (V,0, u), DP (V,1, u)), DP (V,2, u));Panax Notoginseng } - } the if(k==1&& isleaf)returnd[u][k]=INF; + if(k==1&&!flag) res+=diff; A returnD[u][k]=res+ (k==2); the } + intMain () { - intn,a,b; $scanf"%d",&n); $Ne=0; -memset (head,-1,sizeof(head)); - for(intI=1; i<n; ++i) { thescanf"%d%d",&a,&b); - Addedge (A, b); Addedge (b,a);Wuyi } thememset (d,-1,sizeof(head)); -printf"%d", Min (DP (1,1,0), DP (1,2,0))); Wu return 0; -}
POJ3659 Cell Phone Network (Minimum tree dominance set: Tree DP)