Bzoj 2878: [Noi2012] Lost amusement park (tree-shaped DP)

Source: Internet
Author: User

A tree, the direct tree-shaped DP (to find out the desired length of the downward and upward walk). If it is a ring set of trees, each tree on the ring does its own tree DP, and then the violent enumeration (the point <=20 on the ring) on each point on the ring runs through the path on the ring is OK.

---------------------------------------------------------------------------------------------

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MAXN = 100009; int N, M, C, Root, DEG[MAXN], L[MAXN], R[MAXN], LW[MAXN], RW[MAXN];bool VIS[MAXN];Double EU[MAXN], ED[MAXN];struct Edge {int T, W;edge* N;} E[MAXN << 1], *pt = E, *H[MAXN]; inline void Addedge (int u, int v, int w) {deg[pt->t = v]++, pt->w = w, pt->n = H[u], h[u] = pt++;}void Dfs_d (int x) {vis[x] = true;ed[x] = 0;For (edge* e = h[x]; e; e = e->n) if (!vis[e->t]) {dfs_d (e->t);Ed[x] + = ed[e->t] + e->w;}if (x = = Root) {Ed[x]/= max (1, (Deg[x]-(M < N 0:2));} else if (Deg[x] > 1)Ed[x]/= deg[x]-1;}void Dfs_u (int x) {vis[x] = true;For (edge* e = h[x]; e; e = e->n) if (!vis[e->t]) {int d = deg[x];if (x! = Root) {d--;} else if (M = = N)D-= 2;Double t = (ed[x] * d-ed[e->t]-e->w);if (x = = Root) {if (M < N)Eu[e->t] = T/max (1, d-1);Elseeu[e->t] = (t + eu[x] * 2)/(d + 1);} elseeu[e->t] = (t + eu[x])/D;Eu[e->t] + = e->w;Dfs_u (e->t);}}void Dfs_u (int x, int nxt[], int nxtw[], double &t, int len, double p = 0.5) {if (nxt[x]! = Root) {T + = (Ed[x] + len) * p * (Deg[x]-2)/(Deg[x]-1);Dfs_u (Nxt[x], NXT, NXTW, T, Len + nxtw[x], p/(Deg[x]-1));} elseT + = (Ed[x] + len) * p;}bool Dfs_c (int x, edge* r = NULL) {vis[x] = true;For (edge* e = h[x]; e; e = e->n) if (E! = r) {l[e->t] = x;r[x] = e->t;lw[e->t] = rw[x] = e->w;if (Vis[e->t]) {C = e->t;return true;}if (Dfs_c (e->t, E + ((e-e) ^ 1)) return true;}return false;}void Init () {scanf ("%d%d", &n, &m);int U, V, W;for (int i = 0; i < M; i++) {scanf ("%d%d%d", &u, &v, &w);u--, v--;Addedge (U, V, W);Addedge (V, U, W);}} void Work () {double ans = 0;if (M < N) {memset (Vis, 0, sizeof vis);dfs_d (Root = 0);memset (Vis, 0, sizeof vis);eu[0] = 0;Dfs_u (Root = 0);ans = ed[0];for (int i = 1; i < N; i++)ans + = (ed[i] * (Deg[i]-1) + eu[i])/deg[i];} else {memset (Vis, 0, sizeof vis);Dfs_c (0);Root = C;Do {memset (Vis, 0, sizeof vis);Vis[l[root]] = Vis[r[root]] = true;dfs_d (Root);} while ((Root = L[root])! = C);Root = C;Do {Dfs_u (L[root], L, Lw, Eu[root], lw[root]);Dfs_u (R[root], R, Rw, Eu[root], rw[root]);} while ((Root = L[root])! = C);Root = C;Do {memset (Vis, 0, sizeof vis);Vis[l[root]] = Vis[r[root]] = true;Dfs_u (Root);} while ((Root = L[root])! = C);memset (Vis, 0, sizeof vis);Root = C;Do {ans + = (ed[root] * (Deg[root]-2) + eu[root] * 2)/deg[root];Vis[root] = true;} while ((Root = L[root])! = C);for (int i = 0; i < N; i++)if (!vis[i]) ans + = (ed[i] * (Deg[i]-1) + eu[i])/deg[i];}printf ("%.5lf\n", ans/n);}int main () {Init ();Work ();return 0;}

---------------------------------------------------------------------------------------------

2878: [Noi2012] lost amusement park time limit: ten Sec Memory Limit: MB
Submit: 752 Solved: 443
[Submit] [Status] [Discuss] 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 of one row containing a real number, which is the desired length of the path, preserving five decimal places

Sample Input4 3
1 2 3
2 3 1
3 4 4
Sample Output6.00000

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

Bzoj 2878: [Noi2012] Lost amusement park (tree-shaped 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.