HDU4707pet

来源:互联网 发布:java开发常见错误 编辑:程序博客网 时间:2024/06/01 07:38

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

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

Hint

题意

题解:

查找节点深度大于2的个数
把每个节点的连接的点都存入vector 然后从0开始bfs记录每个点的深度

AC代码

#include<cstdio>#include<cstring>#include<stack>#include <set>#include <queue>#include <vector>#include<iostream>#include<algorithm>using namespace std;typedef long long LL;const int mod = 1e9+7;int vis[100005];int step[100005];vector <int > v[100005];queue<int> q;void init(){    for (int i = 0;i < 100005; ++i){        v[i].clear();        step[i] = 0;        vis[i] = 0;    }    while (!q.empty()) q.pop();}int main(){    int t;    scanf("%d",&t);    while (t--){        int n,k;        scanf("%d%d",&n,&k);        init();        int x,y;        for (int i = 0;i < n-1; ++i){            scanf("%d%d",&x,&y);            if (x!=y){                v[x].push_back(y);                v[y].push_back(x);            }        }        q.push(0);        vis[0] = 1;        while (!q.empty()){            int tp = q.front();            q.pop();            for (int i = 0;i < v[tp].size(); ++i){                if (!vis[v[tp][i]]){                    q.push(v[tp][i]);                    step[v[tp][i]] = step[tp] + 1;                    vis[v[tp][i]] = 1;                }            }        }        int sum = 0;        for (int i = 0;i < n; ++i){            if (step[i]>k){                sum++;            }        }        printf("%d\n",sum);    }    return 0;}
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 修改wifi密码ip地址怎么办 苹果6s接电话声音小怎么办 k歌录音器失败怎么办 想报警但不能说话怎么办 微粒贷要家人电话怎么办 4g网络信号差怎么办 手机移动网络信号不好怎么办 移动的4g网络差怎么办 4g移动网络慢怎么办 房间没有4g网络怎么办 oppo显示2g网络怎么办 oppo只有2g网络怎么办 移动卡4g网络慢怎么办 易信专线电话用完了怎么办 手机拨打电话时黑屏怎么办 网易号文章一直审核中怎么办 登陆积分会员忘记密码怎么办 易信密码忘记了怎么办 滴滴永久封停的号怎么办 微信版本过低怎么办 报关时通关单号没录怎么办 公司注销了行政许可证怎么办呢 报关项数超过50项怎么办 出口报关件数报多了怎么办 关税少交被海关缉私查到怎么办 外贸报关hs编码报错怎么办 报关金额少报了怎么办 进口报关金额少报了怎么办 报关重量报少了怎么办 金额报多100倍怎么办 ems没扫描到单号怎么办 回国海关被税了怎么办 代购买错东西了怎么办 征信报告有逾期怎么办 evus信息填错了怎么办 清关一个月了该怎么办 清关一直不发怎么办 清关一个月了怎么办 香港ems到西安海关税怎么办 寄东西被海关扣留怎么办 天津港新舱单品名错误核销怎么办