zoukankan      html  css  js  c++  java
  • 杭电4707--Pet (Dfs)***

    Pet

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 1788    Accepted Submission(s): 863


    Problem Description
    One day, Lin Ji wake up in the morning and found that his pethamster escaped. He searched in the room but didn’t find the hamster. He tried to use some cheese to trap the hamster. He put the cheese trap in his room 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 his room. The trap’s manual mention that the pet will 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 is still hiding in somewhere in the school and distance between each adjacent locations is always one distance unit.
     

     

    Input
    The 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 two positive integer N (0<N<=100000) and D(0<D<N), separated by a single space. N is the number of locations in the school and D is the affective distance of the trap. The following N-1lines descripts the map, each has two integer x and y(0<=x,y<N), separated by a single space, meaning that x and y is adjacent in the map. Lin Ji’s room is always at location 0.
     

     

    Output
    For each test case, outputin a single line the number of possible locations in the school the hamster may be found.
     

     

    Sample Input
    1
    10 2
    0 1
    0 2
    0 3
    1 4
    1 5
    2 6
    3 7
    4 8
    6 9
     
    Sample Output
    2
     

     

    Source
     

     

    Recommend
    liuyiding   |   We have carefully selected several similar problems for you:  5352 5351 5350 5349 5348 
     
    RE:  宠物丢了, 主人根据地图找, 求找到宠物有几种可能(距根节点距离 > n →→ 宠物可以找到)。 Rt,  题意我也是醉了。

     //STL真是强大。 

     1 #include <vector>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <iostream>
     5 #include <algorithm>
     6 using namespace std;
     7 int dis[100005];  vector<int>v[100005];
     8 void Dfs(int x)
     9 {
    10     for(int i = 0; i < v[x].size(); i++)
    11     {
    12         dis[v[x][i]] = dis[x] + 1;
    13         Dfs(v[x][i]);
    14     }
    15     return;
    16 }
    17 int main()
    18 {
    19     int t;
    20     scanf("%d", &t);
    21     while(t--)
    22     {
    23         int n, m, x, y;
    24         scanf("%d %d", &n, &m);
    25         for(int i=1; i < n; i++)
    26             v[i].clear();
    27         for(int i=1; i<n; i++)
    28         {
    29             scanf("%d %d", &x, &y); 
    30             v[x].push_back(y);
    31         }
    32         memset(dis, 0, sizeof(dis));  //数组清0, 保存路径经过结点个数;  
    33         Dfs(0);
    34         int ans = 0;
    35         for(int i=1; i<n; i++)   
    36         {
    37             if(dis[i] > m)
    38             ans++;
    39         }
    40         printf("%d
    ", ans);
    41     }
    42     return 0;
    43 }
    vector建树, 深搜
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <iostream>
     4 using namespace std;
     5 int len;                  
     6 int ans;    
     7 
     8 struct tree
     9 {
    10     int to;           //弧头节点序号;
    11     int next;         //下一条边的序号; 
    12     int step;         //用作权值; 
    13 } a[100005];          //第几条边; 
    14 
    15 int head[100005], m, n;
    16 
    17 void Add(int x, int y)
    18 {
    19     a[len].to = y;
    20     a[len].next = head[x];
    21     head[x] = len++;  //序号为i的弧尾节点发出的第一条边的序号; 
    22 }
    23 
    24 void Dfs(int x, int y)
    25 {
    26     int i, j, k;
    27     if(head[x] = -1)
    28         return;
    29     for(i = head[x]; i != -1; i = a[i].next)   //x弧尾节点序号, i为x所发边序号, 
    30     {
    31         k = a[i].to;                             //弧头节点序号; 
    32         a[i].step += 1;
    33         if(a[i].step > n)
    34             ans++;
    35         Dfs(k, a[i].step); 
    36     }
    37 }
    38 
    39 int main()
    40 {
    41     int t;
    42     scanf("%d", &t);
    43     while(t--)
    44     {
    45         int m, n;
    46         memset(a, 0, sizeof(a));
    47         memset(head, -1, sizeof(head));
    48         scanf("%d %d", &m, &n);
    49         len  = 0;
    50         for(int i = 1; i < m; i++)
    51         {
    52             int x, y;
    53             scanf("%d %d", &x, &y);
    54             Add(x, y);
    55         }
    56         ans = 0;
    57         Dfs(0, 0);
    58         printf("%d
    ", ans);
    59     }
    60     return 0;
    61 } 
    结构体建树,  深搜
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <cstring>
     4 using namespace std;
     5 int m, n;
     6 int father[100005];
     7 void init()
     8 {
     9     for(int i = 0; i < m; i++)
    10         father[i] = i;
    11 }
    12 int find(int a, int flag)   
    13 {
    14     int sum = 0;
    15     while(a != father[a])    
    16     {
    17         a = father[a];           ////记录查找次数; 
    18         sum++;
    19     }
    20     if(flag)
    21         return a;
    22     else
    23         return sum;
    24 }
    25 int main()
    26 {
    27     int t;
    28     scanf("%d", &t);
    29     while(t--)
    30     {
    31         scanf("%d %d", &m, &n);
    32         init();
    33         int total = 0;
    34         for(int i = 1; i < m; i++)
    35         {
    36             int a, b;
    37             scanf("%d %d", &a, &b);
    38             father[b] = a;                //只建立关系, 不合并; 
    39             if(find(b, 1) == 0 && find(b, 0) > n)
    40                 total++;
    41         }
    42         printf("%d
    ", total);
    43     }
    44     return 0;    
    45 } 
    并查集, 神思路
     1 #define MAXNODES 100
     2 #define MAXEDGES 1005
     3 int first[MAXNODES], next[MAXEDGES];  //记录邻接表信息; 
     4 //first[i]可以理解为第i条链的头,它指向所有以编号为i的点为起点的边中的第一条边(存储的是边的编号) 
     5 //next[i]是什么呢?它其实就是链表结点的next指针,它指向下一条边(同样存储的是边的编号)。
     6 int u[MAXEDGES], v[MAXEDGES], w[MAXNEDGES]; //起点, 终点, 边权; 
     7 int e;    //边的条数; 
     8 
     9 void init(){
    10     memset(first, -1, sizeof(first));
    11     e = 0;    
    12 } 
    13 
    14 //add函数是怎么链表的插入过程呢?
    15 //在这里,因为first[i]记录的是第i个点指向的第一条边的编号,所以在插入链表的时候,我们插入的是链表的头部,而不是尾部。
    16 void add(int a, int t, int c){
    17     u[e] = s; v[e] = t; w[e] = c;
    18 //那么这两句就很好理解了。先把e这条边的下一条设置成之前的链表头,然后再把表头指向现在的这条边e。
    19 //注意顺序不能反,就像链表的插入和删除一样,顺序很重要,不能把链给断了。
    20     next[e] = first;
    21     first[s] = e++;
    22 }
    临接表, 详解
  • 相关阅读:
    [BZOJ3829][Poi2014]FarmCraft 贪心
    【BZOJ 3144】 [Hnoi2013]切糕 真·最小割
    【BZOJ1458】士兵占领 最大流的模板题
    【COGS 14】 [网络流24题] 搭配飞行员 网络流板子题
    【BZOJ 4832】 [Lydsy2017年4月月赛] 抵制克苏恩 期望概率dp
    【BZOJ4325】NOIP2015 斗地主 搜索+贪心
    【BZOJ 1409】 Password 数论(扩展欧拉+矩阵快速幂+快速幂)
    【NOIP模拟赛】天神下凡 动态开点线段树
    【NOIP模拟赛】藏宝图 最小生成树
    【NOIP模拟赛】黑红树 期望概率dp
  • 原文地址:https://www.cnblogs.com/soTired/p/4707546.html
Copyright © 2011-2022 走看看