HDU2376——Average distance(思维+树形DP)

简介: HDU2376——Average distance(思维+树形DP)

原题链接

题意:

给定一棵树,有边权,求树上任意两点之间距离的和的平均值。

思路:

传统求树上两点距离是LCA,任意的话要枚举起点和终点,复杂度O(n*n)。

换个思路,考虑每条边的贡献:因为是树,所以任意删除一条边都会将该图分为两个连通块T1,T2,也就是说这条边对答案的贡献就是两个连通块点数的乘积再乘以权值,次数=cnt[T1]*cnt[T2].

维护每个点的子树所含的点的个数num,n-num就是另一个连通块的点数,树形dp维护即可。

代码:

注意用long long

#pragma GCC optimize(3)
#pragma GCC optimize("Ofast","unroll-loops","omit-frame-pointer","inline")
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll,ll>PLL;
typedef pair<int,int>PII;
typedef pair<double,double>PDD;
#define I_int ll
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
char F[200];
inline void out(I_int x) {
    if (x == 0) return (void) (putchar('0'));
    I_int tmp = x > 0 ? x : -x;
    if (x < 0) putchar('-');
    int cnt = 0;
    while (tmp > 0) {
        F[cnt++] = tmp % 10 + '0';
        tmp /= 10;
    }
    while (cnt > 0) putchar(F[--cnt]);
    //cout<<" ";
}
ll ksm(ll a,ll b,ll p){ll res=1;while(b){if(b&1)res=res*a%p;a=a*a%p;b>>=1;}return res;}
const int inf=0x3f3f3f3f,mod=998244353;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int maxn=1e6+100,maxm=3e5+7,N=1e6+7;
const double PI = atan(1.0)*4;
int h[maxn],idx,n;
struct node{
    int e,ne,w;
}edge[maxn*2];
void add(int u,int v,int w){
    edge[idx]={v,h[u],w};h[u]=idx++;
}
ll res=0;
int cnt[maxn];
void dfs(int u,int fa){
    cnt[u]=1;
    for(int i=h[u];~i;i=edge[i].ne){
        int j=edge[i].e,w=edge[i].w;
        if(j==fa) continue;
        dfs(j,u);
        cnt[u]+=cnt[j];
        res=res+(ll)(n-cnt[j])*cnt[j]*w;
    }
}
int main(){
    int T=read();
    while(T--){
        n=read();
        memset(h,-1,sizeof h);idx=0;res=0;
        for(int i=1;i<n;i++){
            int u=read(),v=read(),w=read();
            add(u,v,w);add(v,u,w);
        }
        dfs(1,-1);
        int tt=n*(n-1)/2;
        printf("%.6f\n",(double)res/tt);
    }
    return 0;
}

威海站C的前置题qwq


目录
相关文章
|
11月前
|
算法
poj 2479 Maximum sum(求最大子段和的延伸)
看完最大连续子段和 的 dp算法 这个很容易理解,我用dplift[i]保存第1到第i个之间的最大子段和,dpright[i]保存第i到第n个之间的最大子段和,最终结果就是dplift[i]+dpright[i+1]中最大的一个。
43 0
|
3月前
|
C++
【PAT甲级 - C++题解】1046 Shortest Distance
【PAT甲级 - C++题解】1046 Shortest Distance
62 0
|
机器学习/深度学习 C++
【PAT甲级 - C++题解】1059 Prime Factors
【PAT甲级 - C++题解】1059 Prime Factors
75 0
|
机器学习/深度学习
Codeforces1499——C. Minimum Grid Path(思维+分奇偶+贪心)
Codeforces1499——C. Minimum Grid Path(思维+分奇偶+贪心)
87 0
POJ-1475,Pushing Boxes(双BFS)
POJ-1475,Pushing Boxes(双BFS)
[UVA1364 | POJ | NC]Knights of the Round Table | Tarjan 求点双 | 二分图 | 综合图论
我们可以很轻松地发现,被提出的都是在点双连通分量之外的,比如该图中的1 和 5 ,那么怎么判断哪些点不在环中呢? 此时我们还可以逆向思考,不 在 环 中 的 = = 总 的 − 在 环 中 的,所以说现在问题就转换成了满足条件的环内的点的个数
121 0
[UVA1364 | POJ | NC]Knights of the Round Table | Tarjan 求点双 | 二分图 | 综合图论
PTA | 喊山 (30 分) BFS 拼题A
一个山头呼喊的声音可以被临近的山头同时听到。题目假设每个山头最多有两个能听到它的临近山头。给定任意一个发出原始信号的山头,本题请你找出这个信号最远能传达到的地方。
221 0
PTA | 喊山 (30 分) BFS 拼题A