Pet
Time limit:4000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1788 Accepted Submission (s): 863
Problem Descriptionone Day, Lin Ji wake up in the morning and found that he pethamster escaped. He searched in the didn ' t find the hamster. He tried to use some cheese to trap the hamster. He put the cheese trap in his, and waited for three days. Nothing but cockroaches was caught. He got the map of the school and Foundthat there is no cyclic path and every location in the school can be reached from hi S. The trap ' s manual mention that the pet would always come back if it still in somewhere nearer than distance D. Your task is-to-help Lin Ji-to-find out how many possible locations the hamster may found given the map of the school. Assume that the hamster are still hiding in somewhere in the school and distance between each adjacent locations are always One distance unit.
Inputthe input contains multiple test cases. Thefirst line is a positive integer T (0<t<=10), the number of test cases. For each test cases, the first line has both positive integer N (0<n<=100000) and D (0<d<n), separated by a sing Le space. N is the number of locations in the school and D are the affective distance of the trap. The following N-1lines descripts the map, each have a integer x and y (0<=x,y<n), separated by a single space, Meani ng that X and y are adjacent in the map. Lin Ji ' s hostel is always at location 0.
Outputfor each test case, outputin a single line the number of possible locations in the school the hamster is found.
Sample INPUT1 10 2 0 1 0 2 0 3 1 4 1 5 2 6 3 7 4 8 6 9
Sample Output2
Source2013 ACM/ICPC Asia Regional Online--warmup
recommendliuyiding | We have carefully selected several similar problems for you:5352 5351 5350 5349 5348 RE: Pet lost, Master according to the map to find a pet there are several possible (from the root section Point distance > n→→ pets can be found). Rt, test instructions I was drunk, too.
1#include <cstdio>2#include <cstring>3#include <iostream>4 using namespacestd;5 6 structnode7 {8 intnext, to; 9 intStep;Ten} a[100005]; One A inthead[100005]; - intN, D, Len, ans; - the voidADD (intXinty) - { -A[len].to =y; -A[len].next =Head[x]; +HEAD[X] = len++; - } + A voidDfs (intXintStep) at { - intI, J, K; - if(Head[x] = =-1) - return; - for(i = head[x]; i!=-1; i =a[i].next) - { inK =a[i].to; -A[i].step = step+1;//the step here is different from the inside of the structure; to if(a[i].step>d) +ans++; -Dfs (K,a[i].step);//The structure has been zeroed out. the } * $ }Panax Notoginseng - intMain () the { + intT, I, J, X, y; A while(~SCANF ("%d", &t)) the { + while(t--) - { $Memset (Head,-1,sizeof(head)); $Memset (A,0,sizeof(a)); -scanf"%d%d", &n, &d); -Len =0; the for(i=1; i<n; i++) - {Wuyiscanf"%d%d", &x, &y); the Add (x, y); - } WuAns =0; -Dfs (0,0); Aboutprintf"%d\n", ans); $ } - } - return 0; -}
construction, deep search
STL is really powerful.
1#include <vector>2#include <cstdio>3#include <cstring>4#include <iostream>5#include <algorithm>6 using namespacestd;7 intdis[100005]; vector<int>v[100005];8 voidDfs (intx)9 {Ten for(inti =0; I < v[x].size (); i++) One { ADis[v[x][i]] = dis[x] +1; - Dfs (V[x][i]); - } the return; - } - intMain () - { + intT; -scanf"%d", &t); + while(t--) A { at intN, m, x, y; -scanf"%d%d", &n, &m); - for(intI=1; I < n; i++) - v[i].clear (); - for(intI=1; i<n; i++) - { inscanf"%d%d", &x, &y); - v[x].push_back (y); to } +memset (DIS,0,sizeof(dis));//Array Clear 0, save the path through the number of nodes; -Dfs (0); the intAns =0; * for(intI=1; i<n; i++) $ {Panax Notoginseng if(Dis[i] >m) -ans++; the } +printf"%d\n", ans); A } the return 0; +}
vector achievements, deep search
Hangzhou Electric 4707--pet (DFS)