标签:
#include <vector>
#include <cstring>
#include <cstdio>
using namespace std;
#define CLR(x,y) memset((x),(y),sizeof((x)))
#define FOR(x,y,z) for(int (x)=(y);(x)<(z);++(x))
#define FORD(x,y,z) for(int (x)=(y);(x)>=(z);--(x))
const int maxn = 1000 + 10;
const int maxe = maxn * (maxn - 1);
int n,s,k,fa[maxn],covered[maxn];
vector<int> nodes[maxn];
vector<int> g[maxn];
void dfs(int u,int f,int d){
fa[u] = f;
int nc = g[u].size();
//叶子节点
if(nc == 1 && d > k) nodes[d].emplace_back(u);
FOR(i,0,nc){
int v = g[u][i];
if(v != f) dfs(v,u,d + 1);
}
}
void dfs2(int u,int f,int d){
covered[u] = 1;
int nc = g[u].size();
FOR(i,0,nc){
int v = g[u][i];
if(v != f && d < k) dfs2(v,u,d+1);
}
}
int solve(){
int ans = 0;
CLR(covered,0);
FORD(d,n-1,k + 1){
int tmpsize = nodes[d].size();
FOR(i,0,tmpsize){
int u = nodes[d][i];
if(covered[u]) continue;
int v = u;
FOR(j,0,k) v = fa[v];
dfs2(v,-1,0);
++ans;
}
}
return ans;
}
int main(){
int t,u,v;
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&s,&k);
FOR(i,1,n+1){
g[i].clear();
nodes[i].clear();
}
FOR(i,0,n - 1){
scanf("%d%d",&u,&v);
g[u].emplace_back(v);
g[v].emplace_back(u);
}
dfs(s,-1,0);
printf("%d\n",solve());
}
return 0;
}
[2016-03-19][UVALive][3902][Network]
标签:
原文地址:http://www.cnblogs.com/qhy285571052/p/902bf668e08464ed421fc06e671d51c9.html