CF 11D A Simple Task (digital pressure DP)

Source: Internet
Author: User

Test instructions

n the graph of the non-point, the M-bar describes the graph.

Ask how many loops there are in this non-map.

(1≤ n ≤19, 0≤ m)

Ideas:

Example:

4 6

1 2

1 3

1 4

2 3

2 4

3 4

Answer: 7

Draw a picture to find that the direct violence DFS has too many repeated computations. With DP.

Enumeration points (states), the start and end points of each state (the starting point can be used without enumeration, because it is a ring anyway, who is the same as the starting point).

Dp[s][i]: State is s,i is the end point meaning: The number of scenarios from the first number s in S that arrive at point I. If S and I are added, the total scheme number Ans+=dp[s][i]

Dp[s][i]=sigma (dp[s '][i ')) s ' is the point set minus the point I, I ' belong to s ' and I ' and I are connected.

Code:

intn,m;Charg[ -][ -];ints[1<< +];//Global StateintCnS;intp[ -];//Global Pointersintcnp;ll dp[(1<< +)+5][ -];ll ans;voidFindsintNnintFnum,intNownum,intNowpos,intSs) {//The length is NN, altogether must put Fnum, now has put Nownum, now is nowpos position to carry on the first nownum+1 to place the attempt    if(nownum==fnum) {s[++cns]=Ss; return; }    intt1=fnum-Nownum; Rep (I,nowpos,nn-t1+1){        intNss=ss+ (1<< (I-1)); Finds (Nn,fnum,nownum+1, i+1, NSs); }}voidCalcintNnintS) {//The total length is NN, and the state S is calculated in the position of 1, which exists in global pointer P "". cnp=0; Rep (I,1, nn) {        intT= (1<< (I-1)); if((s&t) >0) {p[++cnp]=i; }    }}voidinit () {CnS=0; CnP=0; Finds (N,2,0,1,0); MEM (DP,0); Rep (I,1, CnS) {        intss=S[i];        Calc (N,SS); Rep (J,2, CnP) {            if(g[p[1]][p[j]]==1) {Dp[ss][p[j]]=1; }        }    }}voidSolve () {Rep (I,3, N) {//the state is composed of I pointscns=0; CnP=0; Finds (N,i,0,1,0); Rep (TT,1, CnS) {            intss=S[TT];            Calc (N,SS); Rep (J,2, CnP) {                intPJ=P[J];//End                intnss=ss-(1<< (pj-1));//Previous StateRep (K,2, CnP) {//Enumerate Endpoints                    if(K==J)Continue; if(g[pj][p[k]]==0)Continue; intpk=P[k]; DP[SS][PJ]+=DP[NSS][PK]; }                if(g[p[1]][pj]==1) {ans+=DP[SS][PJ]; }            }        }    }}intMain () {CIN>>n>>m; Mem (G,0);  while(m--){        intb; scanf ("%d%d",&a,&b); G[A][B]=g[b][a]=1; }    if(n==1|| n==2) {puts ("0"); }    Else{init (); Ans=0;        Solve (); printf ("%i64d\n", ans/2); }    return 0;}

CF 11D A Simple Task (digital pressure DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.