传送门
题意:
有一棵树,n个节点,每条边有边权ci,每个点的颜色为R,G,B三种颜色之一(R为红色,G为绿色,B为蓝色)
你要统计有序对(U,V)的数量,其中U,V是两个点集
它还需要满足以下条件:
1、U和V都必须是连通的
2、U的颜色只能是红色或者绿色,V的颜色只能是绿色或者蓝色
3、存在一个U,V均包含的点x,使得对于所有在U,V集合中的点y,有dis(x,y)≤w
答案对10^9+7取模
思路:
最终选出来的绿色的点一定构成一个连通块。
显然如果枚举每一种绿色的连通块然后计算可以产生的红/蓝连通块个数是可以不重不漏的。
现在对于每一个绿点的连通块,我们想让它只被计算一次,由于这个绿点的连通块的点数-边数恰好等于1。
于是分别枚举绿点和连着两个绿点的边统计答案即可。
代码:
#include<bits/stdc++.h>
#define ri register int
using namespace std;
inline int read(){
#define gc getchar
int ans=0;
char ch=gc();
while(!isdigit(ch))ch=gc();
while(isdigit(ch))ans=((ans<<2)+ans<<1)+(ch^48),ch=gc();
return ans;
#undef gc
}
typedef long long ll;
const int mod=1e9+7,N=2005;
inline int add(const int&a,const int&b){
return a+b>=mod?a+b-mod:a+b;}
inline int dec(const int&a,const int&b)