zoukankan      html  css  js  c++  java
  • UVA

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3931

    题意

    一棵树,一个人从A节点出发,等可能选择到下一个点的路,有B、C两个结点,问先到C的概率。

    分析

    若A,B,C在一条链上,那么A到B,C的概率只与它们之间的长度有关。那么当A不在B,C所在的链上时,我们只需要找到进入这条链的入口,也就是B和C的公共祖先,通过这个公共祖先就能从A走到链上,而这之前的路对于B和C来说都是等价的,因此无关。

    #include<iostream>
    #include<cmath>
    #include<cstring>
    #include<queue>
    #include<vector>
    #include<cstdio>
    #include<algorithm>
    #include<map>
    #include<set>
    #define rep(i,e) for(int i=0;i<(e);i++)
    #define rep1(i,e) for(int i=1;i<=(e);i++)
    #define repx(i,x,e) for(int i=(x);i<=(e);i++)
    #define X first
    #define Y second
    #define PB push_back
    #define MP make_pair
    #define mset(var,val) memset(var,val,sizeof(var))
    #define scd(a) scanf("%d",&a)
    #define scdd(a,b) scanf("%d%d",&a,&b)
    #define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
    #define pd(a) printf("%d
    ",a)
    #define scl(a) scanf("%lld",&a)
    #define scll(a,b) scanf("%lld%lld",&a,&b)
    #define sclll(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
    #define IOS ios::sync_with_stdio(false);cin.tie(0)
    using namespace std;
    typedef long long ll;
    template <class T>
    void test(T a){cout<<a<<endl;}
    template <class T,class T2>
    void test(T a,T2 b){cout<<a<<" "<<b<<endl;}
    template <class T,class T2,class T3>
    void test(T a,T2 b,T3 c){cout<<a<<" "<<b<<" "<<c<<endl;}
    const int inf = 0x3f3f3f3f;
    const ll INF = 0x3f3f3f3f3f3f3f3fll;
    const ll mod = 1e9+7;
    int T;
    void testcase(){
        printf("Case %d: ",++T);
    }
    const int MAXN = 500;
    const int MAXM = 30;
    
    struct Edge{
        int to,nxt;
    }e[MAXN*2];
    int head[MAXN],tot;
    void addEdge(int u,int v){
        e[tot].to=v;
        e[tot].nxt=head[u];
        head[u]=tot++;
    }
    void init(){
        tot=0;
        mset(head,-1);
    }
    int fa[MAXN][20];
    int deg[MAXN];
    void BFS(int rt){
        queue<int> que;
        deg[rt]=0;
        fa[rt][0]=rt;
        que.push(rt);
        while(!que.empty()){
            int tmp = que.front();
            que.pop();
            for(int i=1;i<20;i++) fa[tmp][i]=fa[fa[tmp][i-1]][i-1];
            for(int i=head[tmp];~i;i=e[i].nxt){
                int v = e[i].to;
                if(v==fa[tmp][0]) continue;
                deg[v]=deg[tmp]+1;
                fa[v][0]=tmp;
                que.push(v);
            }
        }
    }
    
    
    int LCA(int u,int v){
        if(deg[u]>deg[v]) swap(u,v);
        int hu = deg[u],hv=deg[v];
        int tu = u,tv=v;
        for(int det=hv-hu,i=0;det;det>>=1,i++){
            if(det&1) tv=fa[tv][i];
        }
        if(tu==tv) return tu;
        for(int i=19;i>=0;i--){
            if(fa[tu][i]==fa[tv][i]){
                continue;
            }
            tu=fa[tu][i];
            tv=fa[tv][i];
        }
        return fa[tu][0];
    }
    
    int main() {
    #ifdef LOCAL
        freopen("in.txt","r",stdin);
    #endif // LOCAL
        int A,B,C,N;
        while(~scanf("%d%d%d%d",&N,&A,&B,&C)){
            init();
            int u,v;
            for(int i=1;i<N;i++){
                scdd(u,v);
                addEdge(u,v);
                addEdge(v,u);
            }
            BFS(A);
            int lca =LCA(B,C);
            int c=deg[C]-deg[lca];
            int b=deg[B]-deg[lca];
            double ans=0;
            if(c==0) ans=0;
            else if(b==0) ans=1;
            else ans=1.0*c/(c+b);
            printf("%.6f
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    shell 操作钉钉机器人实现告警提醒
    谨慎 mongodb 关于数字操作可能导致类型及精度变化
    数据库如何应对保障大促活动
    SQL Server Alwayson架构下 服务器 各虚拟IP漂移监控告警的功能实现 -1(服务器视角)
    通过 Telegraf + InfluxDB + Grafana 快速搭建监控体系的详细步骤
    MySQL数据库Inception工具学习与测试 笔记
    MongoDB 中数据的替换方法实现 --类Replace()函数功能
    MongoDB 中的【加减乘除】运算
    MySQL索引设计需要考虑哪些因素?
    关于SQL Server 数据库归档的一些思考和改进
  • 原文地址:https://www.cnblogs.com/fht-litost/p/9302193.html
Copyright © 2011-2022 走看看