1 #include<iostream> 2 #include<cstring> 3 #include<vector> 4 using namespace std; 5 6 const int maxn=1000+10; 7 int n,s,k; 8 vector<int> tree[maxn],nodes[maxn]; 9 int fa[maxn]; 10 bool covered[maxn]; 11 12 void dfs(int u,int f,int d) 13 { 14 fa[u]=f; 15 int nc=tree[u].size(); 16 if(nc==1&&d>k) nodes[d].push_back(u); 17 for(int i=0;i<nc;i++) 18 if(tree[u][i]!=f) dfs(tree[u][i],u,d+1); 19 } 20 21 void dfs2(int u,int f,int d) 22 { 23 int nc=tree[u].size(); 24 covered[u]=true; 25 for(int i=0;i<nc;i++) 26 if(tree[u][i]!=f&&d<k) 27 dfs2(tree[u][i],u,d+1); 28 } 29 30 int solve() 31 { 32 int ans=0; 33 memset(covered,0,sizeof(covered)); 34 for(int d=n-1;d>k;d--) 35 for(int i=0;i<nodes[d].size();i++) 36 { 37 int u=nodes[d][i]; 38 if(covered[u]) continue; 39 int v=u; 40 for(int j=0;j<k;j++) v=fa[v]; 41 dfs2(v,-1,0); 42 ans++; 43 } 44 return ans; 45 } 46 47 int main() 48 { 49 int T; 50 cin>>T; 51 while(T--) 52 { 53 cin>>n>>s>>k; 54 for(int i=1;i<=n;i++) 55 { 56 tree[i].clear(); 57 nodes[i].clear(); 58 } 59 for(int i=1;i<=n-1;i++) 60 { 61 int a,b; 62 cin>>a>>b; 63 tree[a].push_back(b); 64 tree[b].push_back(a); 65 } 66 dfs(s,-1,0); 67 cout<<solve()<<endl; 68 } 69 return 0; 70 }
时间: 2024-10-19 01:29:00