1013. Battle Over Cities (25)(DFS遍历)
For example, if we have 3 cities and 2 highways connecting city1-city2 and city1-city3. Then if city1 is occupied by the enemy, we must have 1 highway repaired, that is the highway city2-city3.
Input
Each input file contains one test case. Each case starts with a line containing 3 numbers N (<1000), M and K, which are the total number of cities, the number of remaining highways, and the number of cities to be checked, respectively. Then M lines follow, each describes a highway by 2 integers, which are the numbers of the cities the highway connects. The cities are numbered from 1 to N. Finally there is a line containing K numbers, which represent the cities we concern.
Output
For each of the K cities, output in a line the number of highways need to be repaired if that city is lost.
Sample Input
- 3 2 3
- 1 2
- 1 3
- 1 2 3
Sample Output
- 1
- 0
- 0
题目大意:给出n个城市之间有相互连接的m条道路,当删除一个城市和其连接的道路的时候,
问其他几个剩余的城市至少要添加多少个路线才能让它们重新变为连通图,其实就是求连通分支数
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- int graph[][];
- int visited[];
- int n,m,k;
- void dfs( int a)
- {
- int i;
- visited[a]=;
- for( i=; i<=n; i++)
- {
- if( visited[i]== && graph[a][i]==)
- dfs(i);
- }
- }
- int main()
- {
- int cnt=,temp;
- int i,j;
- int a,b;
- scanf("%d%d%d",&n,&m,&k);
- for( i=; i<m; i++) //创建图
- {
- scanf("%d%d",&a,&b);
- graph[a][b]=graph[b][a]=;
- }
- for( i=; i<k ; i++)
- {
- cnt=;
- memset( visited,,sizeof(visited)); //每次都将visited全置0
- scanf("%d",&temp);
- visited[temp]=;
- for( j=; j<=n; j++)
- {
- if( visited[j]==)
- {
- dfs(j);
- cnt++; //连通分支数
- }
- }
- printf("%d\n",cnt-); //需要建的高速为连通分支数减1
- }
- return ;
- }
1013. Battle Over Cities (25)(DFS遍历)的更多相关文章
- 1013 Battle Over Cities (25分) DFS | 并查集
1013 Battle Over Cities (25分) It is vitally important to have all the cities connected by highways ...
- PAT 甲级 1013 Battle Over Cities (25 分)(图的遍历,统计强连通分量个数,bfs,一遍就ac啦)
1013 Battle Over Cities (25 分) It is vitally important to have all the cities connected by highway ...
- PAT 解题报告 1013. Battle Over Cities (25)
1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...
- PAT Advanced 1013 Battle Over Cities (25) [图的遍历,统计连通分量的个数,DFS,BFS,并查集]
题目 It is vitally important to have all the cities connected by highways in a war. If a city is occup ...
- 1013 Battle Over Cities (25分) 图的连通分量+DFS
题目 It is vitally important to have all the cities connected by highways in a war. If a city is occup ...
- 1013 Battle Over Cities (25 分)
It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...
- 1013. Battle Over Cities (25)
题目如下: It is vitally important to have all the cities connected by highways in a war. If a city is oc ...
- PAT A 1013. Battle Over Cities (25)【并查集】
https://www.patest.cn/contests/pat-a-practise/1013 思路:并查集合并 #include<set> #include<map> ...
- 1013. Battle Over Cities 用dfs计算联通分量
使用一个标记数组,标记 节点是否已访问 int 连通度=0 dfs(node i) {标记当前节点为以访问 for(每一个节点) {if(当前几点未访问 并且 从i到当前节点有直接路径) dfs(当前 ...
随机推荐
- 143 Reorder List 重排链表
给定一个单链表L:L0→L1→…→Ln-1→Ln,重新排列后为: L0→Ln→L1→Ln-1→L2→Ln-2→…必须在不改变节点的值的情况下进行原地操作.例如,给定链表 {1,2,3,4},按要求重排 ...
- .net core跨域设置
services.AddCors(options => options.AddPolicy("AllowSameDomain", builder => builder. ...
- sys模块详解
1.sys.argv argv是「argument variable」参数变量的简写形式,一般在命令行调用的时候由系统传递给程序.这个变量其实是一个List,argv[0] 一般是“被调用的脚本文件名 ...
- Android RxJava小结
一.如何使用 在build.gradle中添加依赖 dependencies { api 'io.reactivex:rxandroid:1.2.1' api 'io.reactivex:rxjava ...
- RecyclerView 缓存机制学习笔记2
RecyclerView 初始化所有的视图后,调用 去缓存(StaggeredGridLayoutManager), 而不是初始化一次缓存一次 存储后系统又会去调用tryGetViewHolderFo ...
- PMP项目管理学习笔记(10)——范围管理之收集需求
一个星期没看书,没记录笔记,没能坚持下来,感觉好罪过.现在我要重新上路! 收集需求 收集需求就是与项目的所有干系人坐在一起,得出他们的需求是什么,这就是收集需求过程中要做的事情.你的项目要想成功,你就 ...
- vijos 1053 Easy sssp
描述 输入数据给出一个有N(2 <= N <= 1,000)个节点,M(M <= 100,000)条边的带权有向图. 要求你写一个程序, 判断这个有向图中是否存在负权回路. 如果从一 ...
- Window.Event.KeyCode的含义
Window.Event.KeyCode=13的含义(转载) 2011-04-16 09:41:18| 分类: html | 标签:keycode event realkey var do ...
- Python3简明教程(十四)—— Collections模块
collections 是 Python 内建的一个集合模块,提供了许多有用的集合类. 在这个实验我们会学习 Collections 模块.这个模块实现了一些很好的数据结构,它们能帮助你解决各种实际问 ...
- Vue的 $parent,并不能准确找到上一层的控件,所以如果需要,需要填坑这个 bug,递归寻找下上级
Vue的 $parent,并不能准确找到上一层的控件,所以如果需要,需要填坑这个 bug,递归寻找下上级 // Find components upward function findComponen ...