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(当前 ...
随机推荐
- [已读]基于MVC的Javascript Web 富应用开发
这本书是12年出版,我买的时间应该是13年,书架上唯一一本盗版→ → 但是看完是在今年. 因为刚拿到的时候,读起来很是磕磕绊绊,就搁置了蛮久.到第二次拿起来的时候,发现已经有部分内容过时,但我还是觉得 ...
- nginx 80端口重定向到443端口
server { listen ; server_name www.域名.com; rewrite ^(.*)$ https://${server_name}$1 permanent; } serve ...
- mysql多表查询20题
+-------------------+| Tables_in_dapeng3 |+-------------------+| class || course || s1 || score || s ...
- struct和union
struct的小秘密 C语言中的struct可以看做变量的集合,struct的问题: 空结构体占用多大内存? 例子1:空结构体的大小 #include<stdio.h> struct ST ...
- asp.net 中文部分显示问号
很神奇的事情,今天部署了一个网站,页面从数据读取新闻后,有些新闻标题全部显示问题号,有几个新闻能正确显示汉字,然后查看新闻页面又能正常显示汉字. 解决办法: 在异常的页面上加上 < %@ COD ...
- Sublime折腾记录
本文可以理解为FAQ,主要是为了大家GET一些技能,具体内容包括LICENSE.重置.Package Control的安装,其他内容以后可能补充... 最后说明一下自己的版本:Build 3114 L ...
- How to proxy a web site by apache2 in Ubuntu
Install apache2 To execute the install command in terminal: sudo apt-get install apache2 Then, we ca ...
- iOS开发内购全套图文教程
2015年最全的内购图文教程,首先是填各种资料,最后是代码,废话不多说,直接上图 ======================第一部分协议=============== 第一步 第二步 第三步 第四步 ...
- _ 下划线 vue mixins 混入 变量前有下划线 变量不起作用
_ 下划线 vue mixins 混入 变量前有下划线 变量不起作用
- Vue 2.0 右键菜单组件 Vue Context Menu
Vue 2.0 右键菜单组件 Vue Context Menu https://juejin.im/entry/5976d14751882507db6e839c