Description

放假了,小Z觉得呆在家里特别无聊,于是决定一个人去游乐园玩。进入游乐园后,小Z看了看游乐园的地图,发现可以将游乐园抽象成有n个景点、m条道路的无向连通图,且该图中至多有一个环(即m只可能等于n或者n-1)。小Z现在所在的大门也正好是一个景点。小Z不知道什么好玩,于是他决定,从当前位置出发,每次随机去一个和当前景点有道路相连的景点,并且同一个景点不去两次(包括起始景点)。贪玩的小Z会一直游玩,直到当前景点的相邻景点都已经访问过为止。小Z所有经过的景点按顺序构成一条非重复路径,他想知道这条路径的期望长度是多少?小Z把游乐园的抽象地图画下来带回了家,可是忘了标哪个点是大门,他只好假设每个景点都可能是大门(即每个景点作为起始点的概率是一样的)。同时,他每次在选择下一个景点时会等概率地随机选择一个还没去过的相邻景点。
Input

第一行是两个整数n和m,分别表示景点数和道路数。 接下来行,每行三个整数Xi, Yi, Wi,分别表示第i条路径的两个景点为Xi, Yi,路径长Wi。所有景点的编号从1至n,两个景点之间至多只有一条道路。
Output

共一行,包含一个实数,即路径的期望长度。
Sample Input
4 3
1 2 3
2 3 1
3 4 4
Sample Output
6.00000000

【样例解释】样例数据中共有6条不同的路径: 路径 长度 概率
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
因此期望长度 = 8/4 + 3/8 + 5/8 + 4/8 + 4/8 + 8/4 = 6.00
【评分方法】本题没有部分分,你程序的输出只有和标准答案的差距不超过0.01时,才能获得该测试点的满分,否则不得分。
【数据规模和约定】对于100%的数据,1 <= Wi <= 100。 测试点编号 n m 备注
1 n=10 m = n-1 保证图是链状
2 n=100 只有节点1的度数大于2
3 n=1000 /
4 n=100000 /
5 n=100000 /
6 n=10 m = n /
7 n=100 环中节点个数<=5
8 n=1000 环中节点个数<=10
9 n=100000 环中节点个数<=15
10 n=100000 环中节点个数<=20

一直WA,最后才发现环没仔细考虑,因为最后一个是不可能走到i的所以度数减1

就是求一个down和一个up,分别表示向下走的期望长度和向上走的期望长度

对于环,我们以每个环上的节点为根求一边down

然后枚举环上的点更新up值,环上点的up值就表示第一条边走环边的期望长度

然后用环上节点的up值更新其他点的up值

 const
maxn=;
var
first,d,fa:array[..maxn]of longint;
up,down:array[..maxn]of double;
vis,cir:array[..maxn]of boolean;
last,next,w:array[..maxn*]of longint;
huan,len:array[..maxn]of longint;
n,m,tot,cnt:longint;
ans:double;
flag:boolean; procedure insert(x,y,z:longint);
begin
inc(tot);
last[tot]:=y;
next[tot]:=first[x];
first[x]:=tot;
w[tot]:=z;
inc(d[y]);
end; procedure dfs(x:longint);
var
i,j:longint;
begin
vis[x]:=true;
i:=first[x];
while i<> do
begin
if flag then exit;
if (vis[last[i]])and(fa[x]<>last[i]) then
begin
len[last[i]]:=w[i];
j:=x;
while j<>fa[last[i]] do
begin
inc(cnt);cir[j]:=true;
huan[cnt]:=j;
j:=fa[j];
end;
flag:=true;exit;
end;
if not vis[last[i]] then
begin
fa[last[i]]:=x;len[last[i]]:=w[i];
dfs(last[i]);
end;
i:=next[i];
end;
end; procedure dfs1(x:longint);
var
i:longint;
begin
vis[x]:=true;
i:=first[x];
while i<> do
begin
if not vis[last[i]] then
begin
dfs1(last[i]);
down[x]:=down[x]+w[i]+down[last[i]]/(d[last[i]]-+longint(d[last[i]]=));
end;
i:=next[i];
end;
end; procedure dfs2(x:longint);
var
i:longint;
begin
if (not cir[x]) and (fa[x]<>) then up[x]:=len[x]+(up[fa[x]]+down[fa[x]]-len[x]-down[x]/(d[x]-+longint(d[x]=)))/(d[fa[x]]-+longint(d[fa[x]]=));
vis[x]:=true;
i:=first[x];
while i<> do
begin
if not vis[last[i]] then
begin
fa[last[i]]:=x;len[last[i]]:=w[i];
dfs2(last[i]);
end;
i:=next[i];
end;
end; procedure main;
var
i,x,y,z:longint;
s:double;
begin
read(n,m);cnt:=-;
for i:= to m do
begin
read(x,y,z);
insert(x,y,z);
insert(y,x,z);
end;
if m=n- then
begin
dfs1();
for i:= to n do vis[i]:=false;
dfs2();
for i:= to n do ans:=ans+(up[i]+down[i])/d[i];
end
else
begin
dfs();
for i:= to n do vis[i]:=false;
for i:= to cnt do vis[huan[i]]:=true;
for i:= to cnt do dfs1(huan[i]);
for i:= to cnt do
begin
x:=(i+)mod(cnt+);s:=;
while x<>i do
begin
if (x+)mod(cnt+)=i then up[huan[i]]:=up[huan[i]]+s*len[huan[(x+cnt)mod(cnt+)]]+s*down[huan[x]]/(d[huan[x]]-+longint(d[huan[x]]=))
else up[huan[i]]:=up[huan[i]]+s*len[huan[(x+cnt)mod(cnt+)]]+s*down[huan[x]]/(d[huan[x]]-);
s:=s/(d[huan[x]]-);
x:=(x+)mod(cnt+);
end;
x:=(i+cnt)mod(cnt+);s:=;
while x<>i do
begin
if (x+cnt)mod(cnt+)=i then up[huan[i]]:=up[huan[i]]+s*len[huan[x]]+s*down[huan[x]]/(d[huan[x]]-+longint(d[huan[x]]=))
else up[huan[i]]:=up[huan[i]]+s*len[huan[x]]+s*down[huan[x]]/(d[huan[x]]-);
s:=s/(d[huan[x]]-);
x:=(x+cnt)mod(cnt+);
end;
end;
for i:= to n do vis[i]:=false;
for i:= to cnt do vis[huan[i]]:=true;
for i:= to cnt do dfs2(huan[i]);
for i:= to n do ans:=ans+(up[i]+down[i])/d[i];
end;
writeln(ans/n::);
end; begin
main;
end.

2878: [Noi2012]迷失游乐园 - BZOJ的更多相关文章

  1. BZOJ 2878: [Noi2012]迷失游乐园( 树形dp )

    一棵树的话直接树形dp(求出往下走和往上走的期望长度). 假如是环套树, 环上的每棵树自己做一遍树形dp, 然后暴力枚举(环上的点<=20)环上每个点跑经过环上的路径就OK了. -------- ...

  2. 【BZOJ 2878】 2878: [Noi2012]迷失游乐园 (环套树、树形概率DP)

    2878: [Noi2012]迷失游乐园 Description 放假了,小Z觉得呆在家里特别无聊,于是决定一个人去游乐园玩.进入游乐园后,小Z看了看游乐园的地图,发现可以将游乐园抽象成有n个景点.m ...

  3. bzoj 2878 [Noi2012]迷失游乐园——树上的期望dp

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2878 很好的树上概率题的思路,就是分成up和down. 代码中有众多小细节.让我弃疗好几天的 ...

  4. bzoj 2878: [Noi2012]迷失游乐园

    #include<iostream> #include<cstring> #include<cstdio> #define M 100005 #define ld ...

  5. bzoj 2878: [Noi2012]迷失游乐园【树上期望dp+基环树】

    参考:https://blog.csdn.net/shiyukun1998/article/details/44684947 先看对于树的情况 设d[u]为点u向儿子走的期望长度和,du[u]为u点的 ...

  6. [bzoj2878][Noi2012]迷失游乐园(基环树dp)

    [bzoj2878][Noi2012]迷失游乐园(基环树dp) bzoj luogu 题意:一颗数或是基环树,随机从某个点开始一直走,不走已经到过的点,求无路可走时的路径长期望. 对于一棵树: 用两个 ...

  7. 【BZOJ 2878】 [Noi2012]迷失游乐园

    Description 放假了,小Z觉得呆在家里特别无聊,于是决定一个人去游乐园玩.进入游乐园后,小Z看了看游乐园的地图,发现可以将游乐园抽象成有n个景点.m条道路的无向连通图,且该图中至多有一个环( ...

  8. NOI2012 : 迷失游乐园

    终于补完NOI2012了好开心~ 题目大意:给定一棵树或者环套外向树,求出从中随机选一条简单路径的期望长度,环上点数不超过20. 设 d[x]表示x的度数,ch[x]表示x孩子个数 up[x]表示x向 ...

  9. 【bzoj2878】 Noi2012—迷失游乐园

    http://www.lydsy.com/JudgeOnline/problem.php?id=2878 (题目链接) 题意 求基环树上以任意点为起点的简单路径期望长度. Solution 啊啊啊好丑 ...

随机推荐

  1. Part 11 Search filter in AngularJS

    As we type in the search textbox, all the columns in the table must be searched and only the matchin ...

  2. Part 3 ViewData and ViewBag in mvc

    ViewBag and ViewData is a mechanism(机制) to pass data from controller to view. We use '@' symbol(符号) ...

  3. ASP.NET,web.config 中SessionState的配置

    web Form 网页是基于HTTP的,它们没有状态, 这意味着它们不知道所有的请求是否来自同一台客户端计算机,网页是受到了破坏,以及是否得到了刷新,这样就可能造成信息的丢失. 于是, 状态管理就成了 ...

  4. ssh-key 与 git账户配置以及多账户配置

    http://www.cnblogs.com/dubaokun/p/3550870.html 在使用git的时候,git与远程服务器是一般通过ssh传输的(也支持ftp,https),我们在管理远程分 ...

  5. webkit常见问题汇总

    前段时间有人问我一个简单的问题,html如何创建解析的? 我讲了一大堆,什么通过DocumentLoader, CachedResourceLoader, CacheResource, Resourc ...

  6. Oracle在所有内容前追加一些内容的方法

     参照下面的sql语句. SQL> SELECT * FROM UserInfo; NAME                    CHINESE -------------------- ...

  7. Cocos2d-JS中的cc.LabelTTF

    cc.LabelTTF是使用系统中的字体,它是最简单的标签类.cc.LabelTTF类图如下图所示,可以cc.LabelTTF继承了cc.Node类,具有cc.Node的基本特性. LabelTTF类 ...

  8. (转)mongoDB 禁用大内存页面 transparent_hugepage=never

    最近在学mongoDB,安装倒没什么困难,有yum仓库.不过接入ctl后的一条warning倒挺让人烦心的. 1 2 2015-03-22T09:27:00.222+0800 I CONTROL  [ ...

  9. eclipse优化(部分)

    1. 增强Eclipse(MyEclipse)输入代码提示功能 一般设置: (1). 打开Eclipse,选择打开" Window -- Preferences". (2). 在目 ...

  10. OC 内存管理机制总结

    OC 内存管理机制总结 一:OC内存管理机制目前分为两块,其一自动内存管理机制,其二手动内存管理机制: 1.首先我们从自动内存管理机制讲起: 1)什么是自动内存管理机制,自动内存管理机制就是程序中所创 ...