您的位置:首页 > 教育 > 培训 > 溧阳网站开发_东莞企业营销_网络营销策划方案ppt_明星百度指数在线查询

溧阳网站开发_东莞企业营销_网络营销策划方案ppt_明星百度指数在线查询

2025/5/10 16:32:32 来源:https://blog.csdn.net/2403_83306207/article/details/145839739  浏览:    关键词:溧阳网站开发_东莞企业营销_网络营销策划方案ppt_明星百度指数在线查询
溧阳网站开发_东莞企业营销_网络营销策划方案ppt_明星百度指数在线查询

一开始用的dfs,但是好像是因为数据量太大,数据错误,而且会超时,然后使用bfs

dfs的代码(自留):

import java.util.*;public class F15 {public static void main(String[] args) {Scanner sc = new Scanner(System.in);int n = sc.nextInt();int m = sc.nextInt();int q = sc.nextInt();int result = 0;int[][] g = new int[n + 1][n + 1];for (int i = 0; i < m; i++) {int a = sc.nextInt();int b = sc.nextInt();g[a][b] = 1;g[b][a] = 1;}for (int i = 0; i < q; i++) {boolean[] dest = new boolean[n + 1];dest[0] = true;int start = sc.nextInt();int count = sc.nextInt();dfs(start, g, count, dest);for (int j = 1; j < dest.length; j++) {if (dest[j] == true) {result++;}}}double x = (double) result / q;System.out.printf("%.2f", x);}public static void dfs(int start, int[][] g, int count, boolean[] dest) {dest[start] = true;if (count == 0) return;for (int i = 1; i < g.length; i++) {if (i == start) continue;if (g[start][i] == 1 && dest[i] == false) {count--;dfs(i, g, count, dest);}}}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com