2878: [Noi2012] lost amusement park time limit: ten Sec Memory Limit: mbsec Special Judge
Submit: 415 Solved: 283
[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 the amusement park could be abstracted into an undirected graph with n attractions and M-roads, and that the graph had at most one ring (that is, m may be equal to n or n-1). Small Z Now the gate is also just one of the sights. Little Z did not know what fun, so he decided to start at the current location, each random to a and the current attractions have a road link to the attractions, and the same attractions do 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 that pass in order form a non-repeating path, and he wants to know what the expected length of the path is? Little Z took the abstract picture of the amusement park and brought it home, but forgetting to mark which point was the gate, he had to assume that each attraction could be the gate (i.e. the probability of each attraction being the same as the starting point). 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, each line of three integers xi, Yi, WI, respectively, represents the two points of the path of the road to Xi, Yi, the path of long Wi. All attractions are numbered from 1 to N and there is at most one road between the two attractions.
Output
A total row that contains a real number, which is the desired length of the path.
Sample Input4 3
1 2 3
2 3 1
3 4 4
Sample Output6.00000000
There are 6 different paths in the sample interpretation sample data: path length probability
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, to obtain the full score of the test point, otherwise do not score.
"Data size and convention" for 100% of data, 1 <= Wi <= 100. Test point number N m remark
1 n=10 m = n-1 Guarantee Diagram is chain-like
2 n=100 only node 1 has a degree 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
Acknowledgement LJCC offers SPJ
Tree dp+ base ring tree.
if the input is a tree (m=n-1), it can be solved with O (n):
1. First choose a point to do the root.
2.Dfs
Si represents the son node of I;
F[i] Indicates the desired length of the I node to go to the son;
D[i]=sigma (F[si]+edge (si,i));
Du[i] denotes the degree of the I node, that is, the number of sons +1 (father);
Apparently, f[i]=d[i]/(du[i]-1).
We can make the f[],d[],du[] array out of the tree-shaped DP.
3.dfs2
The next requirement is the desired length of the I node toward any of the leaf nodes, which has been sought for the desired length of the son, and what is required is the desired length toward the father P[i].
P[root]=f[root].
Next Dfs,dfs (i) His father X's p[x] has been found, then:
d[i]+= (D[x]-y[i]-edge (X,i))/(Du[x]-1) +edge (x,i)
P[i]=d[i]/du[i].
After a Dfs a little bit of p[] on the find out.
if it is m=n it?
then he is a tree of rings there is only one ring in this tree.
1.Findcir
Find this ring first.
2.Dfs
Take each point on the ring as the root, and the second step of doing m=n-1 to find the d[],f[of each point].
3.Calc
Because there is a ring, so the f[i of each node on the ring is not equal to p[i].
Then we need to enumerate each point on the ring to calculate the desired length from this point along the loop.
Note that the points on the ring are added in degrees 2.
4.dfs2
The d[i]/du[i]=p[i of each point on the ring, and then calculates the p[i of the point on the non-ring in the second step of m=n-1).
#include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include < cstdlib> #define M 100005#define ld long doubleusing namespace Std;int fa[m],c[m],v[m],tot=0,n,m,du[m],now=0,h[m], Root;ld g[m],f[m],d[m],gg[m];struct edge{int y,ne,l;} E[m*5];void addedge (int x,int y,int l) {Tot++;e[tot].y=y;e[tot].ne=h[x];e[tot].l=l;h[x]=tot;} void Dfs (int x) {d[x]=f[x]=0.000;v[x]=1;du[x]=0;for (int i=h[x];i;i=e[i].ne) {int y=e[i].y;if (v[y]| | C[y]) Continue;dfs (y);d u[x]++;d [x]=d[x]+f[y]+ (LD) E[I].L;} if (Du[x]) f[x]=d[x]/(LD) du[x];if (x!=root) du[x]++;} void Dfs2 (int x) {v[x]=1;for (int i=h[x];i;i=e[i].ne) {int y=e[i].y;if (v[y]| | C[y]) Continue;int k=du[x]-1;if (!k) k++;d [y]=d[y]+ (d[x]-f[y]-(LD) E[I].L)/(LD) (k) + (LD) E[I].L;DFS2 (y);}} void Findcir (int x) {v[x]=++now;for (int i=h[x];i;i=e[i].ne) {int y=e[i].y;if (!v[y]) {fa[y]=x; Findcir (y);} else if (Y!=fa[x]&&v[y]<v[x]) {c[y]=1;while (x!=y) {c[x]=1;x=fa[x];} return;}}} void Calc (int x,int fa) {bool last=true;g[x]=0.000; for (int i=h[x];i;i=e[i].ne) {int y=e[i].y;if (Y!=root&&y!=fa&&c[y]) {last=false; Calc (y,x); g[x]=g[x]+g[y]+ (LD) E[I].L;}} int k=du[x];if (!K) k++;if (last) g[x]=d[x]/(LD) K;else {k=du[x]+1;if (x!=root) g[x]= (G[x]+d[x])/(LD) K;else {return;}} int main () {scanf ("%d%d", &n,&m), for (int i=1;i<=m;i++) {int x,y,l;scanf ("%d%d%d", &x,&y,&l); Addedge (x,y,l); Addedge (y,x,l);} if (n==m+1) {Root=1;dfs (1); for (int i=1;i<=n;i++) V[I]=0;DFS2 (1);} Else{findcir (1); for (int i=1;i<=n;i++) v[i]=0;for (int. i=1;i<=n;i++) if (C[i]) Root=i,dfs (i); for (int i=1;i<=n; i++) if (C[i]) Root=i,calc (i,0), gg[i]=g[i];for (int i=1;i<=n;i++) if (C[i]) du[i]+=2,d[i]+=gg[i];for (int i=1;i<=n ; i++) v[i]=0;for (int i=1;i<=n;i++) if (C[i]) Dfs2 (i);} Double ans=0.000;for (int i=1;i<=n;i++) ans=ans+d[i]/(LD) du[i];p rintf ("%.5lf\n", ans/(double) n); return 0;}
Sentiment:
1.RE: Is in Findcir in the v[] array is not differentiated, the assigned value is 1, then in the search for the ring will be error: After finding the ring does not immediately exit the subroutine but continue to find, then will find the ring just looked for, and then will never go out.
Later WA several times (after a long day to find the problem):
D[X] is updated directly in Calc, you should use an d[x]!!! that is not updated when you calculate points on other rings. So the result is too big.
2. For the base ring tree problem, the number of points on the general ring is not much, so we can consider each point on the enumeration ring to do ~
"Bzoj 2878" [Noi2012] lost amusement park