zoukankan      html  css  js  c++  java
  • 【codeforces 500D】New Year Santa Network

    【题目链接】:http://codeforces.com/problemset/problem/500/D

    【题意】

    有n个节点构成一棵树;
    让你随机地选取3个不同的点a,b,c;
    然后计算dis(a,b)+dis(b,c)+dis(a,c)的期望;
    不止如此;
    这里边还会减小;
    要求你动态维护这个期望

    【题解】

    我们在选取这3个点的时候;
    从最后的结果出发;
    先取其中两个点(a,b);
    则第三个点必然是从剩余的n-2个点中取出的;
    也就是说有n-2条(a,b)路径最后需要算进答案;
    而对于任选的a,b都是这样;
    可知所有的dis(a,b)+dis(b,c)+dis(a,c);
    最后就为任意两点之间的距离的和的n-2倍;
    这个当成分子;
    然后分母是C(N,3);
    就是它的期望了;
    任意两点之间的距离和很容易算出来的;
    然后改变的话和算的时候类似的方法;
    都是考虑这条改变的边的两边有多少个点;
    然后相乘再加起来;减掉这个改变量就好;

    【Number Of WA

    0

    【完整代码】

    #include <bits/stdc++.h>
    using namespace std;
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define LL long long
    #define rep1(i,a,b) for (int i = a;i <= b;i++)
    #define rep2(i,a,b) for (int i = a;i >= b;i--)
    #define mp make_pair
    #define pb push_back
    #define fi first
    #define se second
    #define ms(x,y) memset(x,y,sizeof x)
    #define Open() freopen("F:\rush.txt","r",stdin)
    #define Close() ios::sync_with_stdio(0),cin.tie(0)
    
    typedef pair<int,int> pii;
    typedef pair<LL,LL> pll;
    
    const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
    const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
    const double pi = acos(-1.0);
    const int N = 1e5+100;
    
    struct abc
    {
        int x,y,z;
    };
    
    LL n,siz[N],fz;
    vector <pii> G[N];
    abc bian[N];
    
    void dfs(int x,int fa)
    {
        siz[x] = 1;
        for (pii temp:G[x])
        {
            int y = temp.fi;
            LL w = temp.se;
            if (y==fa) continue;
            dfs(y,x);
            fz = fz + (n-siz[y])*siz[y]*w;
            siz[x] = siz[x]+siz[y];
        }
    }
    
    int main()
    {
        //Open();
        Close();//scanf,puts,printf not use
        //init??????
        cin >> n;
        rep1(i,1,n-1)
        {
            int x,y,z;
            cin >> x >> y >> z;
            bian[i] = {x,y,z};
            G[x].pb(mp(y,z));
            G[y].pb(mp(x,z));
        }
        dfs(1,0);
        //cout << fz << endl;
        int q;
        cin >> q;
        while (q--)
        {
            LL x,y;
            cin >>x>>y;
            LL d = bian[x].z-y;
            bian[x].z = y;
            int u = bian[x].x,v = bian[x].y;
            int k;
            if (siz[u]<siz[v])
                k = u;
            else
                k = v;
            fz-=siz[k]*(n-siz[k])*d;
            //cout << fz<<endl;
            //return 0;
            double q = 6*fz,w = n*(n-1);
            cout << fixed << setprecision(10) << q/w << endl;
        }
        return 0;
    }
  • 相关阅读:
    P2711 小行星 最小割
    bzoj2141: 排队 cdq分治
    bzoj 4237: 稻草人 cdq分治
    P1527 [国家集训队]矩阵乘法 整体二分
    P2617 Dynamic Rankings 整体二分
    P3834 【模板】可持久化线段树 1(主席树) 整体二分
    SPREAD for Windows Forms 代码片段
    PHP+Oracle Instant Client
    SQL利用CASE按分组显示合计
    SPREAD for Windows Forms 控制输入法
  • 原文地址:https://www.cnblogs.com/AWCXV/p/7626301.html
Copyright © 2011-2022 走看看