题面:
题意:
给一棵树,a,b,c 以一些点为起点(输入的最后三行就是他们各自的起点), 然后他们三从各自的起点 共同去一个距离和最小的点,求距离和的数学期望.
其实一开始不太会,因为不懂这三个点,之间的关系,但看到这个dis(a,b,c)=(dis(a,b)+dis(a,c)+dis(b,c))/2, 秒会
题解思路:
三点之间的关系 能这样转成 三个的两点之间的关系:
d i s ( a , b , c ) = ( d i s ( a , b ) + d i s ( a , c ) + d i s ( b , c ) ) / 2 dis(a,b,c)=(dis(a,b)+dis(a,c)+dis(b,c) ) / 2 dis(a,b,c)=(dis(a,b)+dis(a,c)+dis(b,c))/2
证明就略了,在纸上画下几种情况 ,差不多就猜到是这个关系了.
那现在对于每条边,我们只要算几个式子就行:
- now1 =now1 + 子树里面 a 的个数 乘 (总b-子树中 b 的个数) + 子树里面 b 的个数 乘 (总a-子树中 a 的个数) // now1 是 a 和 b 的
- now2 =now2 + 子树里面 b 的个数 乘 (总c-子树中 c 的个数)+ 子树里面 c 的个数 乘 (总b-子树中 b 的个数) // now2 是 c 和 b 的
- now3 =now3 + 子树里面 a 的个数 和 (总c-子树中 b 的个数)+子树里面 c 的个数 乘 (总a-子树中 a 的个数) // now 是 a 和 b 的
最后各自算下自己的数学期望加起来 除个 2 就行了
#include<bits/stdc++.h>
#define ks ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define ft first
#define sd second
#define pb push_back
#define pf push_front
#define mp make_pair
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int>pa;
typedef set<int>::iterator sit;
typedef multiset<int>::iterator msit;
template<class T>inline void read(T &res){
char c;T flag=1;
while((c=getchar())<'0'||c>'9')if(c=='-')flag=-1;res=c-'0';
while((c=getchar())>='0'&&c<='9')res=res*10+c-'0';res*=flag;
}
void wenjian(){freopen("concatenation.in","r",stdin);freopen("concatenation.out","w",stdout);}
void tempwj(){freopen("hash.in","r",stdin);freopen("hash.out","w",stdout);}
ll gcd(ll a,ll b){return b == 0 ? a : gcd(b,a % b);}
ll qpow(ll a,ll b,ll mod){a %= mod;ll ans = 1;while(b){if(b & 1)ans = ans * a % mod;a = a * a % mod;b >>= 1;}return ans;}
struct chongzai{int c; bool operator<(const chongzai &b )const{ return c>b.c; } }sss;
const int maxn=1e6+177;
const int maxm=1e6+177;
const ll mod=20000311;
const int inf=0x3f3f3f3f;
const ll INF=0x3f3f3f3f3f3f3f3f;
struct Node{
int next;
int to;
ll vul;
}edge[maxn];
int tot;
int head[maxn];
ll ind[5][maxn];
ll siz[5][maxn];
ll sum[5];
int n;
ll now1,now2,now3;
void add(int from,int to,ll vul){
edge[++tot].next=head[from];
edge[tot].to=to;
edge[tot].vul=vul;
head[from]=tot;
}
void dfs1(int x,int fa){
if(ind[1][x]){
siz[1][x]=1;
}
if(ind[2][x]){
siz[2][x]=1;
}
if(ind[3][x]){
siz[3][x]=1;
}
for(int i=head[x];i;i=edge[i].next){
int y=edge[i].to;
if(y==fa)continue;
dfs1(y,x);
siz[1][x]+=siz[1][y];
siz[2][x]+=siz[2][y];
siz[3][x]+=siz[3][y];
}
}
void dfs2(int x,int fa){
for(int i=head[x];i;i=edge[i].next){
int y=edge[i].to;
ll z=edge[i].vul;
if(y==fa)continue;
dfs2(y,x);
now1+=siz[1][y]*(sum[2]-siz[2][y])*z+siz[2][y]*(sum[1]-siz[1][y])*z; // a—b的
now2+=siz[2][y]*(sum[3]-siz[3][y])*z+siz[3][y]*(sum[2]-siz[2][y])*z; // b—c的
now3+=siz[1][y]*(sum[3]-siz[3][y])*z+siz[3][y]*(sum[1]-siz[1][y])*z; // a-c的
}
}
void init(){
tot=0;
for(int i=0;i<n+177;i++){
head[i]=0;
for(int j=1;j<=3;j++){
ind[j][i]=0;
siz[j][i]=0;
}
}
now1=0;
now2=0;
now3=0;
sum[1]=0;
sum[2]=0;
sum[3]=0;
}
int main(){
scanf("%d",&n);
init();
int be,en;
ll vu;
for(int i=0;i<n-1;i++){
scanf("%d%d%lld",&be,&en,&vu);
add(be,en,vu);
add(en,be,vu);
}
int x;
for(int i=1;i<=3;i++){
scanf("%lld",&sum[i]);
for(int j=1;j<=sum[i];j++){
scanf("%d",&x);
ind[i][x]++;
}
}
dfs1(1,0);
dfs2(1,0);
double tmp1,tmp2,tmp3;
tmp1=sum[1]*sum[2];
tmp1=now1/tmp1;
tmp2=sum[2]*sum[3];
tmp2=now2/tmp2;
tmp3=sum[3]*sum[1];
tmp3=now3/tmp3;
double ans=(tmp1+tmp2+tmp3)/2.0;
printf("%lf\n",ans);
}