[环套树 树形DP 期望] BZOJ 2878 [Noi2012]迷失游乐园

21 篇文章 0 订阅
5 篇文章 0 订阅

瞎DP一下就好了 不过是搞得我蛮昏的
我懒 找一篇题解就当做我写过题解了

#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;

inline char nc(){
  static char buf[100000],*p1=buf,*p2=buf;
  return p1==p2&&(p2=(p1=buf)+fread(buf,1,100000,stdin),p1==p2)?EOF:*p1++;
}
inline void read(int &x){
  char c=nc(),b=1;
  for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
  for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}

const int N=100005;

struct edge{
  int u,v,w,next;
}G[N<<1];
int head[N],inum;
inline void add(int u,int v,int w,int p){
  G[p].u=u; G[p].v=v; G[p].w=w; G[p].next=head[u]; head[u]=p;
}
#define V G[p].v
int fat[N],depth[N];

int path[N],tot,cir[N],dis[N],faw[N];

inline void find(int u,int fa){
  fat[u]=fa; depth[u]=depth[fa]+1;
  for (int p=head[u];p;p=G[p].next)
    if (V!=fa)
      if (!depth[V])
    faw[V]=G[p].w,find(V,u);
      else if (depth[V]<depth[u]){
    int t=u;
    while (t!=V) path[tot]=t,cir[t]=1,dis[tot++]=faw[t],t=fat[t];
    path[tot]=V,cir[V]=1,dis[tot++]=G[p].w;
      }
}

double f[N],g[N];
int son[N];

inline void dfs1(int u,int fa){
  f[u]=0; son[u]=0;
  for (int p=head[u];p;p=G[p].next)
    if (V!=fa && !cir[V])
      dfs1(V,u),son[u]++,f[u]+=f[V]+G[p].w;
  if (son[u]) f[u]/=son[u];
}

int n,m;

inline void dfs2(int u,int fa){
  fat[u]=fa;
  if (fa){
    if (cir[fa]) g[u]=2*g[fa]; else g[u]=g[fa];
    int tmp=0;
    for (int p=head[fa];p;p=G[p].next)
      if (V!=fat[fa] && !cir[V])
    if (V!=u)
      g[u]+=G[p].w+f[V];
    else
      tmp=G[p].w;
    if (n==m)
      if (cir[fa])
    g[u]=g[u]/(son[fa]+1)+tmp;
      else
    g[u]=g[u]/son[fa]+tmp;
    else
      if (!fat[fa])
    g[u]=g[u]/(son[fa]-1)+tmp;
      else
    g[u]=g[u]/son[fa]+tmp;
  }
  for (int p=head[u];p;p=G[p].next)
    if (V!=fa && !cir[V])
      dfs2(V,u);
}


int main(){
  int iu,iv,iw;
  freopen("t.in","r",stdin);
  freopen("t.out","w",stdout);
  read(n); read(m);
  for (int i=1;i<=m;i++)
    read(iu),read(iv),read(iw),add(iu,iv,iw,++inum),add(iv,iu,iw,++inum);
  if (m==n-1){
    dfs1(1,0);
    dfs2(1,0);
    double ans=0;
    for (int i=1;i<=n;i++)
      if (i==1)
    ans+=f[i];
      else
    ans+=(son[i]*f[i]+g[i])/(son[i]+1);
    printf("%.5lf\n",ans/n);
  }else{
    find(1,0);
    for (int i=0;i<tot;i++)
      dfs1(path[i],0);
    for (int i=0;i<tot;i++){
      double tmp=0,p,sum;
      p=1.0/2; sum=0;
      for (int j=1;j<tot;j++){
    int k=(i+j)%tot;
    sum+=dis[(k+tot-1)%tot];
    if (son[path[k]] && j!=tot-1)
      tmp+=(sum+f[path[k]])*p*son[path[k]]/(son[path[k]]+1);
    if (j==tot-1)
      tmp+=(sum+f[path[k]])*p;
    p=p/(son[path[k]]+1);
      }
      p=1.0/2; sum=0;
      for (int j=1;j<tot;j++){
    int k=(i+tot-j)%tot;
    sum+=dis[k];
    if (son[path[k]] && j!=tot-1)
      tmp+=(sum+f[path[k]])*p*son[path[k]]/(son[path[k]]+1);
    if (j==tot-1)
      tmp+=(sum+f[path[k]])*p;
    p=p/(son[path[k]]+1);
      }
      g[path[i]]=tmp;
    }
    for (int i=0;i<tot;i++)
      dfs2(path[i],0);
    double ans=0;
    for (int i=1;i<=n;i++)
      if (cir[i])
    ans+=(son[i]*f[i]+2*g[i])/(son[i]+2);
      else
    ans+=(son[i]*f[i]+g[i])/(son[i]+1);
    printf("%.5lf\n",ans/n);
  }
  return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值