http://poj.org/problem?id=1202

难度集中在输出格式上,因为输出格式所以是高精度

递推式:

血缘肯定只有从双亲传到儿子的,所以,设f,m为双亲,son为儿子,
p[i][j]为i和j之间的血缘关系,p[j][i]=p[i][j]

则:

p[son][f]=p[son][m]=0.5+0.5*p[f][m]

对于兄弟和其他不是双亲的节点j,则有

p[son][j]=0.5*(p[f][j]+p[m][j])

另外:http://swerc.up.pt/2002/Data/有数据

代码交了好几次都是RE,突然交了一次居然AC了

import java.math.BigDecimal;
import java.util.*; class mes{
int son,nxt;
mes(){son=nxt=0;}
}
public class Main {
final static int maxn=650;
static int head[];
static BigDecimal p[][];
static mes s[];
static int fa[][];
static int n,k,m;
static int vis[];
static int que[]; static void addedge(int f,int t,int ind)
{
s[ind].nxt=head[f];
s[ind].son=t;
head[f]=ind;
}
static Scanner scanner;
static void init()
{
for(int i=0;i<maxn;i++){
head[i]=-1;
}
scanner = new Scanner(System.in);
n=scanner.nextInt();
k=scanner.nextInt();
for(int i=0; i<k; i++)
{
int son,f,m;
son=scanner.nextInt();
f=scanner.nextInt();
m=scanner.nextInt();
addedge(f,son,2*i);
addedge(m,son,2*i+1);
fa[son][0]=f;
fa[son][1]=m;
}
for(int i=1; i<=n; i++)
{
p[i][i]=BigDecimal.ONE;
}
} static void calc()
{
int front=0,tail=0;
final BigDecimal mul=BigDecimal.valueOf(0.5);
for(int i=1; i<=n; i++)
{
if(fa[i][0]==0&&fa[i][1]==0&&vis[i]==0)
{
que[tail++]=i;
vis[i]=1;
}
}
while(tail!=front)
{
int tp=que[front];front++;
vis[tp]=2;
int f=fa[tp][0],m=fa[tp][1];
if(f!=0&&m!=0)
{
p[tp][f]=p[tp][m]=p[f][tp]=p[m][tp]=(p[f][m].add(BigDecimal.ONE)).multiply(mul);
for(int i=1; i<=n; i++)
{
if(i!=f&&i!=tp&&i!=m)
{
p[i][tp]=p[i][tp].add((p[i][m].add(p[i][f])).multiply(mul));
p[tp][i]=p[i][tp];
}
}
} for(int sp=head[tp]; sp!=-1; sp=s[sp].nxt)
{
int son=s[sp].son;
if(vis[son]!=0)continue;
if(vis[fa[son][0]]==2&&vis[fa[son][1]]==2)
{
que[tail++]=son;
vis[son]=1;
}
}
}
}
static void show()
{
m=scanner.nextInt();
for(int i=0; i<m; i++)
{
int f,t;
f=scanner.nextInt();
t=scanner.nextInt();
String ans = p[t][f].multiply(BigDecimal.valueOf(100)).toPlainString();
if(ans.indexOf(".")>0&&ans.indexOf(".")<ans.length()){
while(ans.length()>1&&ans.endsWith("0")){
ans=ans.substring(0,ans.length()-1);
}
}
if(ans.endsWith(".")&&ans.length()>1)ans=ans.substring(0,ans.length()-1);
System.out.println(ans+"%");
}
}
public static void main(String[] args) {
head=new int[maxn];
p=new BigDecimal[maxn][maxn];
s=new mes[2*maxn];
fa=new int[maxn][2];
que=new int[maxn];
vis = new int[maxn];
for(int i=0;i<maxn;i++){p[i]=new BigDecimal[maxn];s[i]=new mes();fa[i]=new int[2];}
for(int i=0;i<maxn;i++){for(int j=0;j<maxn;j++){p[i][j]=BigDecimal.ZERO;}}
init();
calc();
show();
}
}

POJ 1202 Family 概率,DP,高精 难度:2的更多相关文章

  1. 洛谷P4608 [FJOI2016]所有公共子序列问题 【序列自动机 + dp + 高精】

    题目链接 洛谷P4608 题解 建个序列自动机后 第一问暴搜 第二问dp + 高精 设\(f[i][j]\)为两个序列自动机分别走到\(i\)和\(j\)节点的方案数,答案就是\(f[0][0]\) ...

  2. [CEOI2007]树的匹配Treasury(树形DP+高精)

    题意 给一棵树,你可以匹配有边相连的两个点,问你这棵树的最大匹配时多少,并且计算出有多少种最大匹配. N≤1000,其中40%的数据答案不超过 108 题解 显然的树形DP+高精. 这题是作为考试题考 ...

  3. poj 2096 Collecting Bugs 概率dp 入门经典 难度:1

    Collecting Bugs Time Limit: 10000MS   Memory Limit: 64000K Total Submissions: 2745   Accepted: 1345 ...

  4. [P1005][NOIP2007] 矩阵取数游戏 (DP+高精)

    我不会高精…… 也不会DP…… 这道题即考高精又考DP…… 我要死了 给一个不是高精的代码(当然不能满分) #include<cstdio> #include<iostream> ...

  5. 矩阵取数问题(dp,高精)

    题目描述 帅帅经常跟同学玩一个矩阵取数游戏:对于一个给定的n×mn \times mn×m的矩阵,矩阵中的每个元素ai,ja_{i,j}ai,j​均为非负整数.游戏规则如下: 每次取数时须从每行各取走 ...

  6. POJ 3156 - Interconnect (概率DP+hash)

    题意:给一个图,有些点之间已经连边,现在给每对点之间加边的概率是相同的,问使得整个图连通,加边条数的期望是多少. 此题可以用概率DP+并查集+hash来做. 用dp(i,j,k...)表示当前的每个联 ...

  7. BZOJ1089 [SCOI2003]严格n元树 【dp + 高精】

    Description 如果一棵树的所有非叶节点都恰好有n个儿子,那么我们称它为严格n元树.如果该树中最底层的节点深度为d (根的深度为0),那么我们称它为一棵深度为d的严格n元树.例如,深度为2的严 ...

  8. poj 1322 Chocolate (概率dp)

    ///有c种不同颜色的巧克力.一个个的取.当发现有同样的颜色的就吃掉.去了n个后.到最后还剩m个的概率 ///dp[i][j]表示取了i个还剩j个的概率 ///当m+n为奇时,概率为0 # inclu ...

  9. BZOJ5300 [Cqoi2018]九连环 【dp + 高精】

    题目链接 BZOJ5300 题解 这题真的是很丧病,,卡高精卡到哭 我们设\(f[i]\)表示卸掉前\(i\)个环需要的步数 那么 \[f[i] = 2*f[i - 2] + f[i - 1] + 1 ...

随机推荐

  1. JS---------->数组练习!

      var arr = [4, 0, 7, 9, 0, 0, 2, 6, 0, 3, 1, 0]; 要求将数组中的0项去掉,将不为0的值存入一个新的数组,生成新的数组 <!doctype htm ...

  2. (三)uboot源码分析

    一.九鼎官方uboot和三星原版uboot对比(1)以九鼎官方的uboot为蓝本来学习的,以三星官方的这份为对照.(2)不同版本的uboot或者同一版本不同人移植的uboot,可能目录结构和文件内容都 ...

  3. unique-substrings-in-wraparound-string(好)

    https://leetcode.com/problems/unique-substrings-in-wraparound-string/ 好,我自己做出来的.多总结规律,多思考. package c ...

  4. Nexus中自定义私服,每个项目都用独立的工厂,仓库

    原文:http://blog.csdn.net/mexican_jacky/article/details/50278045 第一步:创建工厂仓库 第二步:cms项目,那么我们就只能cms项目组用,那 ...

  5. 如何在Mac OSX上安装xgboost

    听说xgboost效果很不错,于是准备学习下,但是发现大多数资料都是在讲如何在windows或linux下安装xgboost,而且照着官方文档也没有正确的安装好多线程的xgboost.最后还是从the ...

  6. linux mount命令的用法详细解析

    挂接命令(mount)首先,介绍一下挂接(mount)命令的使用方法,mount命令参数非常多,这里主要讲一下今天我们要用到的.命令格式:mount [-t vfstype] [-o options] ...

  7. JavaScript设计模式与开发实践 - 单例模式

    引言 本文摘自<JavaScript设计模式与开发实践> 在传统开发工程师眼里,单例就是保证一个类只有一个实例,实现的方法一般是先判断实例存在与否,如果存在直接返回,如果不存在就创建了再返 ...

  8. Windows不重启就使环境变量修改生效

    以修改环境变量“PATH”为例,修改完成后,进入DOS命令提示符,输入:set PATH=C: ,关闭DOS窗口.再次打开DOS窗口,输入:echo %PATH% ,可以发现“我的电脑”->“属 ...

  9. JSP HTML error code

    <html> <head> <title>Setting HTTP Status Code</title> </head> <body ...

  10. html+asp.net上传文件

    type="file" 的name以及id一定要写,并且名字相同 http://niunan.iteye.com/blog/479605 <form id="for ...