title link : http://acm.hdu.edu.cn/showproblem.php?pid=4707
"Test Instructions": Linji's hamster was lost, he wanted to retrieve the hamster, he put a piece of cheese in room 0, and then give the room adjacent relations, the adjacent room distance is 1, the topic gives the cheese can attract the hamster's maximum distance D, and then find out how many rooms may be hamster location, That is to ask how many rooms are 0 of the distance from the room is greater than D.
Analysis
There are many DFS code on the Internet, I think most of them have errors, their code is the default of this condition: Enter the adjacent two rooms, the first input room distance room 0 recently, but the topic did not say their order, conditions are only adjacent , so the two cases need to be placed in the vector to do, with the VIS mark, first scan to the first mark.
Otherwise, the input order is changed, and the error result is output.
According to the characteristics of the vector dynamic array, we can use the vector to construct the tree, and then use DFS, or BFS to traverse the whole tree just fine;
"DFS"
#include <iostream> #include <vector> #include <cstdio> #include <cstring> #include < algorithm>using namespace Std;const int Maxn=100010;bool vis[maxn];struct nod{//Place the distance dis and data in a struct int b,dis;} Tnd;vector <Nod> g[maxn];int ans;void dfs (int s,int dis) {int i;vis[s]=true;if (dis<0) ans++;//over distance add 1;for (int i =0;i<g[s].size (); i++) {int id= g[s][i].b;//each group belongs to the I, which is the previous dimension push_back (), if (Vis[id]) Continue;dfs (id,dis-1);}} int main () {int t;scanf ("%d", &t), while (t--) {memset (vis,false,sizeof (VIS)); int n,d;scanf ("%d%d", &n,&d); int I;ans=0;for (i=0;i<n;i++) {g[i].clear ();} for (i=0;i<n-1;i++) {int a,b;scanf ("%d%d", &a,&b); tnd.b=b;tnd.dis=0; G[a].push_back (TND),//a,b are pushed into the corresponding vector so that the adjacent situation is determined tnd.b=a; G[b].push_back (TND);//push_back operation will extend the current vector dimension one dimension;}dfs (0,d);p rintf ("%d\n", ans);} return 0;}
"BFS"
#include <stdio.h> #include <algorithm> #include <iostream> #include <string.h> #include < vector> #include <queue> #include <set> #include <map> #include <string> #include <math.h > #include <stdlib.h> #include <time.h>using namespace std;const int maxn = 1000010;vector<int>vec[ Maxn];int dep[maxn];void BFS (int s) {memset (dep,-1,sizeof (DEP));//depth initialized to-1 dep[s] = 0; queue<int>q; Q.push (s); while (!q.empty ()) {int u = q.front (); Q.pop (); int sz = Vec[u].size (); for (int i = 0;i < sz;i++) {int v = vec[u][i]; if (dep[v]! =-1) continue;//Similarly, marked continue; Dep[v] = Dep[u] + 1;//A layer of deepening q.push (v); }}}int Main () {int T; int n; int D; scanf ("%d", &t); while (t--) {scanf ("%d%d", &n,&d); int u,v; for (int i = 0;i < n;i++) vec[i].clear (); for (int i = 1; i < n;i++) {scanf ("%d%d", &u,&v); Vec[u].push_back (v);//similarly Store vec[v].push_back (U) with each other; } BFS (0); int ans = 0; for (int i = 0;i < n;i++) if (Dep[i] > D) ans++; cout<<ans<<endl; } return 0;}
hdu4707 Pet (BFS & Dfs,vector)