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;}
- HDU4707pet
- HDU4707Pet(并查集)
- hdu4707Pet最短路(spfa)
- hdu4707Pet(并查集)深度问题
- 用Unity2017做一个简易的CardBoardVR应用
- Java序列化与反序列化
- CC2640R2F BLE5.0 BTool的使用
- C++变量和常量
- 关于QT写好的软件的打包问题。
- HDU4707pet
- (转)Qt Model/View 学习笔记 (三)——Model类
- spark操作mysql数据库
- maven打包jar
- LeetCode习题记录(3)Longest Substring Without Repeating Characters
- Android开发之减小APK大小
- MySQL:语句汇总
- python常用模块之time模块
- 渗透脚本快速生成工具Intersect