标签:ons cout back clear void queue iostream int using
1 #include <iostream> 2 #include <vector> 3 #include <queue> 4 #include <string.h> 5 #include <stdlib.h> 6 #include <stdio.h> 7 using namespace std; 8 const int MAXN=1000010; 9 vector<int> vec[MAXN]; 10 int dep[MAXN]; 11 //int pre[MAXN]; 12 void bfs(int s) 13 { 14 memset(dep,-1,sizeof(dep)); 15 dep[s]=0; 16 queue<int> q; 17 q.push(s); 18 while(!q.empty()){ 19 int u = q.front(); 20 q.pop(); 21 int sz = vec[u].size(); 22 for(int i=0;i<sz;i++){ 23 int v = vec[u][i]; 24 if(dep[v]!=-1) continue; 25 dep[v]=dep[u]+1; 26 // pre[v]=u; 27 q.push(v); 28 } 29 } 30 31 } 32 void dfs(int x) 33 { 34 35 for(unsigned i=0;i<vec[x].size();i++) 36 { 37 dep[vec[x][i]]=dep[x]+1; 38 dfs(vec[x][i]); 39 } 40 return; 41 } 42 43 int main() 44 { 45 int T; 46 int n; 47 int D; 48 scanf("%d",&T); 49 while(T--) 50 { 51 scanf("%d%d",&n,&D); 52 int u,v; 53 for(int i=0;i<n;i++){ 54 vec[i].clear(); 55 } 56 57 for(int i=1;i<n;i++){ 58 scanf("%d%d",&u,&v); 59 vec[u].push_back(v); 60 vec[v].push_back(u); 61 } 62 bfs(0); 63 int ans=0; 64 for(int i=0;i<n;i++){ 65 if(dep[i] > D) 66 ans++; 67 } 68 cout<<ans<<endl; 69 70 71 } 72 73 return 0; 74 }
标签:ons cout back clear void queue iostream int using
原文地址:http://www.cnblogs.com/vector11248/p/6249738.html