2878: [Noi2012] lost paradise time limit: ten Sec Memory Limit: mbsec Special Judge
Submit: 319 Solved: 223
[Submit] [Status] Description
Holiday, small z think stay at home particularly bored, so decided to go to the amusement park to play. After entering the amusement park, Little Z looked at the map of the amusement park and found that it was possible to abstract the amusement park into an undirected graph with n attractions and M roads, and that the graph had at most one ring (that is, m could only be equal to N or n-1).
Small Z Now the gate is also just a spot. Little Z does not know what fun, so he decided to start from the current location, each time randomly to a and the current attractions have a road link to the attractions. And the same attraction does not go two times (including the starting points). The playful little Z will always play until the neighboring attractions of the current attraction have been visited so far. Small Z All the sights are in order to form a non-repeating path, he wondered what the expected length of the path would be? Little Z took the abstract picture of the amusement park and brought it home, but forgot to mark which point was the gate, and he was only good if every attraction could be the gate (that is, the probability of each attraction as the starting point is the same). At the same time, every time he chooses the next attraction, he randomly chooses a neighboring attraction that he has not visited.
Input
The first line is two integers n and m, each representing the number of attractions and the number of roads. Next line. Three integers per line XI, Yi, Wi. The two points of the path of the article I are XI, Yi, and the path is long wi. All attractions are numbered from 1 to N, and there is at most one road between the two attractions.
Output
A total of one row. Include a real number. That is, the desired length of the path.
Sample Input4 3
1 2 3
2 3 1
3 4 4
Sample Output6.00000000
The "example interpretation" example data has 6 different paths in common: path length probabilities
1-->4 8 1/4
2-->1 3 1/8
2-->4 5 1/8
3-->1 4 1/8
3-->4 4 1/8
4-->1 8 1/4
Therefore the desired length = 8/4 + 3/8 + 5/8 + 4/8 + 4/8 + 8/4 = 6.00
"Scoring method" is not part of the subject, your program output only and the answer to the gap of not more than 0.01 when the ability to obtain the test of the full score, otherwise do not score.
"Data size and convention" for 100% of data, 1 <= Wi <= 100.
Test pilot number N m remark
1 n=10 m = n-1 Guarantee Diagram is chain-like
2 n=100 only has node 1 in degrees greater than 2
3 n=1000/
4 n=100000/
5 n=100000/
6 n=10 m = n/
7 Number of nodes in n=100 ring <=5
8 Number of nodes in n=1000 ring <=10
9 Number of nodes in n=100000 ring <=15
Number of nodes in n=100000 ring <=20
HINT
Source
SEARCH=%E9%B8%A3%E8%B0%A2LJCC%E6%8F%90%E4%BE%9BSPJ "style=" color:blue; Text-decoration:none "> Acknowledgements LJCC offers SPJ
Analysis of the subject found m=n-1 or m=n, and graph connectivity.
It is a tree or a ring and an outward tree.
Tree:
to Node X. Consider walking down and up two situations. Expected to be down[x] and up[x, respectively)
Ring Plus outward tree:
down[of each tree on the ring], ibid.
Consider the up[of the nodes on the ring]. K^2 Big violence Hard calculation
up[of non-link points]. Can be obtained from the ring node up[], the method is still ibid.
PS: When considering "no way to go", expect =0.0
The Erase (Q.begin (), Q.begin () +k) function of vector in the subject (delete [0,k) interval)
#include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include < functional> #include <iostream> #include <cmath> #include <cctype> #include <ctime> #include <vector>using namespace std; #define for (I,n) for (int. i=1;i<=n;i++) #define FORK (i,k,n) for (int i=k;i<=n;i++ ) #define REP (I,n) for (int. i=0;i<n;i++) #define ForD (I,n) for (int. i=n;i;i--) #define REPD (i,n) for (int i=n;i>=0;i--) #define FORP (x) for (int p=pre[x];p; p=next[p]) #define LSON (x<<1) #define Rson ((x<<1) +1) #define MEM (a) memset (A,0,sizeof (a)), #define MEMI (a) memset (A,127,sizeof (a)), #define MEMI (a) memset (A,128,sizeof (a)), #define INF ( 2139062143) #define F (100000007) #define MAXN (100000+10) #define MAXM (300000+10) long long mul (long long A,long long b) {RET Urn (a*b)%F;} Long Long (long A,long long B) {return (a+b)%F;} Long Long sub (long A,long long B) {return (a-b+ (a)/f*f+f)%F;} typedef long Long Ll;typedef long double ld;int N,M,EDGE[MAXM], pre[maxn]={0},next[maxm]={0},weight[maxm],size=0;void addedge (int u,int v,int W) {edge[++size]=v;weight[size]=w; Next[size]=pre[u],pre[u]=size;} void Addedge2 (int u,int v,int W) {Addedge (u,v,w), Addedge (v,u,w);} Long double up[maxn]={0.0},down[maxn]={0.0};int fa[maxn]={0},son[maxn]={0};void dfs_down (int x) {Forp (x) {int v=edge[p ];if (V^fa[x]) {fa[v]=x;son[x]++;d fs_down (v);d own[x]+= (LD) (Weight[p]+down[v]);}} if (Son[x]) down[x]/= (LD) son[x];} void dfs_up (int x) {ld sum=0.0; Forp (x) {int v=edge[p];if (v^fa[x]) {sum+= (LD) WEIGHT[P]+DOWN[V];}} if (Fa[x]) sum+=up[x]; Forp (x) {int v=edge[p];if (v^fa[x]) {up[v]=sum-(LD) weight[p]-down[v];if ((son[x]-(!fa[x))) up[v]/= (son[x]-(!fa[x))) ; up[v]+= (LD) weight[p];d fs_up (v);}} BOOL b[maxn]={0},b2=0;vector<int> q,q2;void print () {/*cout<<q.size () <<endl; Rep (I,q.size ()) cout<<q[i]<< "; Cout<<endl;cout<<q2.size () <<endl; Rep (I,q2.size ()) cout<<q2[i]<< "; cout<<endl;*/}void find_circle (int x) {b[x]=1;q.push_back (x);Forp (x) {int v=edge[p];if (!b[v]) {q2.push_back (weight[p]); Find_circle (v); if (B2) return;} else if (q[q.size () -2]^v) {int k=0;while (q[k]^v) k++;p rint (); Q.erase (Q.begin (), Q.begin () +k), Q2.erase (Q2.begin (), Q2.begin () +k); Q2.push_back (Weight[p]); b2=1;return;}} Q.pop_back (); Q2.pop_back ();} void Circle_down () {int size=q.size (); Rep (i,size) {int x=q[i];ld sum=0.0; Forp (x) {int v=edge[p];if ((v^q[(i-1+size)%size]) &&v^ (q[(i+1)%size]) {fa[v]=x;son[x]++;d fs_down (v);d own[x ]+= (LD) (Weight[p]+down[v]);}} if (Son[x]) down[x]/= (LD) son[x];}} void Circle_up () {int size=q.size (); Rep (i,size) {int x=q[i];//processing ring ld p=1.0/(LD) (2), w_past=0.0; for (j,size-1) {w_past+=q2[(i+j-1)%size];int v=q[(i+j)%size];if (j!=size-1) up[x]+=p*son[v]/(LD) (son[v]+1) * (w_past+ DOWN[V]); else up[x]+=p* (W_past+down[v]);p/= (LD) son[v]+1;} p=1.0/(LD) (2), w_past=0.0; for (j,size-1) {w_past+=q2[(i-j+size)%size];int v=q[(i-j+size)%size];if (j!=size-1) up[x]+=p*son[v]/(LD) (son[v]+1) * (W_past+down[v]); else up[x]+=p* (W_past+down[v]);p/= (LD) son[v]+1;} Processing child Nodes LD sum=0.0; Forp (x) {int v=edge[p];if ((v^q[(i-1+size)%size]) &&v^ (q[(i+1)%size]) {sum+= (LD) WEIGHT[P]+DOWN[V];}} Forp (x) {int v=edge[p];if ((v^q[(i-1+size)%size]) &&v^ (q[(i+1)%size])) {up[v]=sum-(LD) weight[p]-down[v];up[ V]/= (LD) (son[x]+1), up[v]+= (2.0/(LD) (son[x]+1)) *up[x];up[v]+= (LD) weight[p];d fs_up (v);}}} int main () {//freopen ("bzoj2878.in", "R", stdin),//freopen ("Bzoj2878.out", "w", stdout), scanf ("%d%d", &n,&m); for (i,m) {int u,v,w;scanf ("%d%d%d", &u,&v,&w); Addedge2 (u,v,w);} for (i,n)//forp (i) cout<<i<< ' <<edge[p]<< ' <<WEIGHT[P]<<ENDL;COUT.SETF (iOS :: Fixed); cout.precision (5); if (m==n-1) {dfs_down (1);d fs_up (1);//for (I,n) cout<<i<< ":" <<down[i] << ' <<up[i]<<endl;ld ans=0.0; for (I,n) if (Fa[i]) ans+= ((LD) son[i]*down[i]+up[i])/(son[i]+1); else ans+=down[i];ans/= (LD) n;cout<<ans< <endl;} Else{find_circle (1);p rint (); Circle_down (); circle_up ();//for (i,n) cout<<i<< ': ' <<DOWN[I]≪< ' <<up[i]<<endl; MEM (b); Rep (I,q.size ()) B[q[i]]=1;ld ans=0.0; for (I,n) if (B[i]) ans+=2.0/(LD) (son[i]+2) *up[i]+ (LD) (Son[i])/(LD) (son[i]+2) *down[i];else ans+=1.0/(LD) (son[i]+1) * up[i]+ (LD) (Son[i])/(LD) (son[i]+1) *down[i];ans/= (LD) N;cout<<ans<<endl;} return 0;}
Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.
Bzoj 2878 ([noi2012]-Lost amusement park Tree dp+ outbound rings plus + future prospects Dp+vector Erase)