hdu 4707 Pet
2016-02-22 19:38
351 查看
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4707
题目描述:
Total Submission(s): 2077 Accepted Submission(s): 1017
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test case, outputin a single line the number of possible locations in the school the hamster may be found.
[align=left]Sample Input[/align]
1
10 2
0 1
0 2
0 3
1 4
1 5
2 6
3 7
4 8
6 9
[align=left]Sample Output[/align]
2
[align=left]Source[/align]
2013 ACM/ICPC
Asia Regional Online —— Warmup
题意:比较好理解,我就不翻译了。
分析:一个无向图,顶点编号为0到n-1,问离顶点0的距离大于d的顶点的数目。方法比较多,可以使用广度搜索,深度搜索,看来网上的分析,还可以使用并查集来解决,求出深度大于d的顶点数目即可。刚开始犯了一个低级的错误,没有及时clear容器,导致各种问题:超时(我觉得不会超时,应该直接是wa,不明白为什么),超内存。
广度搜索:
网上的深度方法:
http://acm.hdu.edu.cn/showproblem.php?pid=4707
题目描述:
Pet
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2077 Accepted Submission(s): 1017
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each test case, outputin a single line the number of possible locations in the school the hamster may be found.
[align=left]Sample Input[/align]
1
10 2
0 1
0 2
0 3
1 4
1 5
2 6
3 7
4 8
6 9
[align=left]Sample Output[/align]
2
[align=left]Source[/align]
2013 ACM/ICPC
Asia Regional Online —— Warmup
题意:比较好理解,我就不翻译了。
分析:一个无向图,顶点编号为0到n-1,问离顶点0的距离大于d的顶点的数目。方法比较多,可以使用广度搜索,深度搜索,看来网上的分析,还可以使用并查集来解决,求出深度大于d的顶点数目即可。刚开始犯了一个低级的错误,没有及时clear容器,导致各种问题:超时(我觉得不会超时,应该直接是wa,不明白为什么),超内存。
广度搜索:
#include<stdio.h> #include<iostream> #include<vector> #include<string.h> #include<queue> using namespace std; const int N=100005; vector<int>g ; int vis ; int dist ; int main() { int n,d,t; cin>>t; while(t--) { scanf("%d%d",&n,&d); for(int i=0;i<n;i++) g[i].clear(); for(int i=1;i<n;i++) { int x,y; scanf("%d%d",&x,&y); g[x].push_back(y); g[y].push_back(x); } queue<int>Q; Q.push(0); memset(vis,0,sizeof(vis)); memset(dist,0,sizeof(dist)); vis[0]=1; dist[0]=0; while(!Q.empty()) { int t=Q.front(); Q.pop(); vis[t]=1; for(int i=0;i<g[t].size();i++) if(!vis[g[t][i]]) { Q.push(g[t][i]); dist[g[t][i]]=dist[t]+1; } } int ans=0; for(int i=0;i<n;i++) if(dist[i]>d) ans++; printf("%d\n",ans); } return 0; }
网上的深度方法:
#include<stdio.h> #include<iostream> #include<vector> #include<string.h> #include<queue> using namespace std; const int N=100005; vector<int>g ; int sum,n,d; void dfs(int i,int dep,int fa) { if(dep>d)sum++; int m=g[i].size(); for(int j=0;j<m;j++) if(fa!=g[i][j]) dfs(g[i][j],dep+1,i); } int main() { int t; scanf("%d",&t); while(t--) { for(int i=0;i<n;i++) g[i].clear(); scanf("%d%d",&n,&d); for(int i=1;i<n;i++) { int x,y; scanf("%d%d",&x,&y); g[x].push_back(y); g[y].push_back(x); } sum=0; dfs(0,0,-1); printf("%d\n",sum); } return 0; }
相关文章推荐
- HtmlParser中的各种Filter(1)
- Android项目旋转屏幕数据丢失的原因及解决
- PAT--完美数列-快速搜索
- PAT 5-8 File Transfer (25分)
- 使用装饰器实现一个简单的Tornado用户认证Demo(Basic authentication on Tornado with a decorator)
- css不定宽块级元素居中方法三
- javascript中常用的节点方法
- c++中换行\n和endl的区别
- Java多线程数量再次说明
- Java语言实现大数字乘除
- 海量数据处理问题
- javascript中form表单的处理
- Android消息推送(主: XMPP协议实现Android推送)
- javascript省市级联方式
- HDU 3466 Proud Merchants(01背包)
- Git 实践
- 开源项目
- URIEncoding与useBodyEncodingForURI 在tomcat中文乱码处理上的区别及CharacterEncodingFilter(备忘)
- mysql阅读笔记四
- shared library (.so) and static libarary (.a)