When there are safe roads between two cities, they are mutually reachable, and this relationship satisfies reflexivity, symmetry, and transitivity,
Therefore, it is an equivalence relation, which corresponds to a connected block in graph theory.
In a connected block, the current point is a point-connected edge that does not affect the other connected blocks, so as long as the points within the current connected block are recorded.
N<=30, the array is not open, and the state transfer is very small, only the number of binary 1 increase in the direction of the transfer, so use map to save. (The most extreme should be the 2^29 ...)
Suitable for writing with memory search.
It is easy to think of the transfer equation is
s represents the current connected block, p is the probability of moving to the fast inner point, trans to a point of probability, the news that goes to the current connected block outside the new connected block.
If you use this formula to calculate infinitely recursive, do a little bit of deformation is good
The DP (s) calculation does not depend on DP (s) itself.
#include <bits/stdc++.h>using namespacestd;Const intMAXN = -;intn,m;intPA[MAXN];intWEI[MAXN];intFDST (intx) {returnx = = Pa[x]? X:PA[X] =FDST (pa[x]);}voidMrge (intXinty) { intA = FDST (x), B =fdst (y); if(A! =b) {Pa[a]=b; WEI[B]|=Wei[a]; }}voidInituf () { for(inti =0; I < n; i++) {Pa[i]=i; Wei[i]=1<<i; }}map<int,Double>Meo;map<int,Double>:: iterator it;#defineMP Make_pair#defineFi first#defineSe SecondDoubletr;Doubledpints) { if((it = Meo.find (s))! = Meo.end ())returnIt->second; DoubleAns =0; intLKS =0; for(inti = n; i--;){ if(s>>i&1) {lks++;Continue; } Ans+ = DP (s|Wei[i]); } ans= (n1.) /(n-lks) * (Tr*ans +1); Meo.insert (MP (S,ans)); returnans;}//#define LOCALintMain () {#ifdef LOCAL freopen ("In.txt","R", stdin);#endif intT, KS =0; Cin>>u; while(t--) {scanf ("%d%d",&n,&m); printf ("Case %d:",++KS); if(n = =1) {puts ("0"); Continue; } inituf (); for(inti = m; i--; ) { intx, y; scanf"%d%d",&x,&y); Mrge (x-1, Y1); } TR=1./(n1); for(inti = n; i--;) {Wei[i]=WEI[FDST (i)]; } meo.clear (); Meo.insert (MP (1<<n)-1,0)); printf ("%lf\n", DP (wei[0])); } return 0;}
UVA 11600 Masud Rana (probabilistic DP)