1 10 2 0 1 0 2 0 3 1 4 1 5 2 6 3 7 4 8 6 9
2
/* Author:ZXPxx Memory: 5904 KB Time: 1201 MS Language: C++ Result: Accepted */ #include <cstdio> #include <cstring> #include <vector> using namespace std; const int mx = 1e5 + 5; vector<int> ma[mx]; int n, d, cnt; bool vis[mx]; void dfs(int p, int deep) { if (deep > d) return; vis[p] = 1; cnt++; int len = ma[p].size(); for(int i = 0; i < len; i++) if (!vis[ma[p][i]]) dfs(ma[p][i], deep + 1); } int main() { int t, a, b; scanf("%d", &t); while (t--) { for(int i = 0; i < n; i++) ma[i].clear(); memset(vis, 0, sizeof(vis)); cnt = 0; scanf("%d%d", &n, &d); for(int i = 0; i < n-1; i++) { scanf("%d%d", &a, &b); ma[a].push_back(b); ma[b].push_back(a); } dfs(0, 0); printf("%d\n", n - cnt); } return 0; }
/* Author:ZXPxx Memory: 10108 KB Time: 764 MS Language: C++ Result: Accepted */ #include <cstdio> #include <cstring> #include <vector> #define mx 100005 using namespace std; vector<int> mp[mx]; int deep[mx*10],Que[mx]; void BFS(int s) { int v,temp,front=0,rear=1; memset(deep,-1,sizeof(deep)); deep[s]=0; Que[front]=s; while(front<rear) { v=Que[front++]; for(int i=0; i<mp[v].size(); ++i) { temp=mp[v][i]; if(deep[temp]==-1) { deep[temp]=deep[v]+1; Que[rear++]=temp; } } } } int main() { int x,y,n,D,t; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&D); for(int i=0; i<mx; ++i) mp[i].clear(); for(int i=1; i<n; ++i) { scanf("%d%d",&x,&y); mp[x].push_back(y); mp[y].push_back(x); } BFS(0); int ans=0; for(int i=0; i<n; ++i) { if(deep[i]>D) ans++; } printf("%d\n",ans); } return 0; }
/* Author:ZXPxx Memory: 2112 KB Time: 577 MS Language: C++ Result: Accepted */ #include<cstring> #include<cstdio> using namespace std; const int mx=1e5+5; int a[mx]; int main() { int t,n,d,x,y;; scanf("%d",&t); while(t--) { scanf("%d%d",&n,&d); memset(a,0,sizeof(a)); for(int i=0; i<n-1; i++) { scanf("%d%d",&x,&y); a[y]=a[x]+1; } int ans=0; for(int i=0; i<n; i++) if(a[i]>d) ans++; printf("%d\n",ans); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/zhang_xueping/article/details/47808453