题目链接:点击查看
题目大意:给出一棵 n n n 个节点组成的树,问选出 k k k 个节点满足相互之间距离相等的方案数有多少
题目分析: n = 100 n=100 n=100,感觉数据范围越小的题目越难发现 tag。补完这个题目后发现每个知识点自己都已经掌握了,但是组合起来就分析不出来,分析题目的能力还是有所欠缺
回到本题,首先特判 k = = 2 k==2 k==2 的情况,样例也给出了提示。
需要思考出的一个思维点就是, k k k 个节点相互之间的距离相等,等价于存在一个中间点 x x x,满足任意两个节点 d i s ( a i , x ) = d i s ( a j , x ) dis(a_i,x)=dis(a_j,x) dis(ai,x)=dis(aj,x) 成立。所以我们可以去枚举中间点作为根节点 r o o t root root,然后将 n n n 个节点按照深度分类讨论即可。需要注意的是,在同一深度下选出的两个节点,其路径必须要经过点 r o o t root root 才有意义,换句话说,必须要保证任意两个点的 L C A = r o o t LCA=root LCA=root 才行。其实处理方法非常简单,设 s o n son son 集合为 r o o t root root 的相邻子节点,对于 s o n son son 中每个节点所代表的的子树中,我们至多选择一个节点就好啦,稍微转换一下就是每个子树至多被选一次,那么这个用 d p [ i ] [ j ] dp[i][j] dp[i][j] 代表前 i i i 个子树中,选择了 j j j 个子树的方案数,用 dp 转移就好了
至此理论部分结束,时间复杂度是枚举 r o o t root root 的 O ( n ) O(n) O(n),枚举深度的 O ( n ) O(n) O(n) 和 d p dp dp 的 O ( n 2 ) O(n^2) O(n2),共 O ( n 4 ) O(n^4) O(n4)
代码:
// Problem: F. Equidistant Vertices
// Contest: Codeforces - Codeforces Round #734 (Div. 3)
// URL: https://codeforces.com/contest/1551/problem/F
// Memory Limit: 256 MB
// Time Limit: 1000 ms
//
// Powered by CP Editor (https://cpeditor.org)
// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast","inline","-ffast-math")
// #pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<list>
#include<unordered_map>
#define lowbit(x) (x&-x)
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
template<typename T>
inline void read(T &x)
{
T f=1;x=0;
char ch=getchar();
while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();
x*=f;
}
template<typename T>
inline void write(T x)
{
if(x<0){x=~(x-1);putchar('-');}
if(x>9)write(x/10);
putchar(x%10+'0');
}
const int inf=0x3f3f3f3f;
const int N=1e6+100;
const int mod=1e9+7;
int n,k;
vector<int>node[N],son;
int cnt[110][110];//cnt[i][j]:第i个子树深度为j的孩子个数
LL dp[110][110];//dp[i][j]:前i个子树中选择了j个子树的方案数
void dfs(int u,int fa,int root,int deep) {
cnt[root][deep]++;
for(auto v:node[u]) {
if(v==fa) {
continue;
}
dfs(v,u,root,deep+1);
}
}
LL cal(vector<int>son) {
memset(dp,0,sizeof(dp));
int n=son.size()-1;
dp[0][0]=1;
for(int i=1;i<=n;i++) {
for(int j=0;j<=i;j++) {
if(j) {
dp[i][j]+=dp[i-1][j-1]*son[i];
}
dp[i][j]+=dp[i-1][j];
dp[i][j]%=mod;
}
}
return dp[n][k];
}
int main()
{
#ifndef ONLINE_JUDGE
// freopen("data.in.txt","r",stdin);
// freopen("data.out.txt","w",stdout);
#endif
// ios::sync_with_stdio(false);
int w;
cin>>w;
while(w--) {
read(n),read(k);
for(int i=1;i<=n;i++) {
node[i].clear();
}
for(int i=1;i<n;i++) {
int u,v;
read(u),read(v);
node[u].push_back(v);
node[v].push_back(u);
}
if(k==2) {
cout<<n*(n-1)/2<<endl;
continue;
}
LL ans=0;
for(int root=1;root<=n;root++) {
memset(cnt,0,sizeof(cnt));
for(auto v:node[root]) {
dfs(v,root,v,1);
}
for(int dep=1;dep<=n;dep++) {
son.clear();
son.push_back(-1);//哨兵节点
for(auto v:node[root]) {
son.push_back(cnt[v][dep]);
}
ans=(ans+cal(son))%mod;
}
}
cout<<ans<<endl;
}
return 0;
}