标签:style blog color io ar for 2014 sp log
所有房子组成一颗树,求出离根节点0的距离大于d的节点数目
DFS+vector存边 水过
#include "stdio.h" #include "string.h" #include "vector" using namespace std; vector<int>mapp[100010]; int ans,d; void dfs(int cur,int pre,int op) { int i; for (i=0;i<mapp[cur].size();i++) if (mapp[cur][i]!=pre) { if (op>=d) ans++; dfs(mapp[cur][i],cur,op+1); } return ; } int main() { int t,n,a,b,i; scanf("%d",&t); while (t--) { scanf("%d%d",&n,&d); for (i=0;i<n;i++) mapp[i].clear(); for (i=2;i<=n;i++) { scanf("%d%d",&a,&b); mapp[a].push_back(b); mapp[b].push_back(a); } ans=0; dfs(0,-1,0); printf("%d\n",ans); } return 0; }
标签:style blog color io ar for 2014 sp log
原文地址:http://blog.csdn.net/u011932355/article/details/39032245