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(当前 ...
随机推荐
- HDU 4565 So Easy! 数学 + 矩阵 + 整体思路化简
http://acm.hdu.edu.cn/showproblem.php?pid=4565 首先知道里面那个东西,是肯定有小数的,就是说小数部分是约不走的,(因为b限定了不是一个完全平方数). 因为 ...
- P1936 水晶灯火灵
题目描述 ,刚好符合以下③条规则:①m.n∈{1,2,……,k}②(n²-m*n-m²)²=1③m.n为整数. 输入输出格式 输入格式: Only one:k.(What?还是失败?) 输出格式: 共 ...
- idea安装mybatis插件
简介 mybatis_plus主要的作用是自动导航,如下图 点击箭头会跳转到对应的dao接口中,同样,dao接口中也有这样的箭头,点击之后会跳转到对应的sql映射语句处. 还有一个功能就是检查mapp ...
- RabbitMQ八:交换机类型Exchange Types--Topic介绍
前言 上一章节,我们说了两个类型,本章我们说一下其三:Topic Exchange Topic Exchange Topic Exchange – 将路由键和某模式进行匹配.此时队列需要绑定要一个模 ...
- 【学习笔记】Base64编码解码原理及手动实现(C#)
1.[Base64编码原理]@叶落为重生 -base64的编码都是按字符串长度,以每3个8bit的字符为一组,-然后针对每组,首先获取每个字符的ASCII编码,-然后将ASCII编码转换成8bit的二 ...
- 【经验总结】VS2010下建立MFC程序
孙鑫的MFC教学视频非常不错,但是由于视频中孙鑫老师采用VC6.0版本,而现在 许多人都转向了使用VS,VS为我们生成了许多不需要的代码,这也导致在这节课的学习编程中总是遇到一些困难.那么,如何去掉这 ...
- xutils3批量上传文件
前几天开发安卓要用到文件批量上传,就是上传图片,视频,文件之类的用到Xutil3框架,用 RequestParams params = new RequestParams(url); params.a ...
- Intel手册 Chapter23 VMX的简单介绍
23.2 虚拟机架构 1: VMX为处理器上的虚拟机定义了处理器级的支持.VMX主要支持两类,VMM和VM 2: VMM作为HOST可以完全控制处理器和其他平台硬件. 每个VM都支持一个栈,并且由O ...
- OFDM、FTTx、SCTP、Ad Hoc、WSN术语简介
上课提到一些术语,下来查了一下,总结在这里. OFDM: OFDM(Orthogonal Frequency Division Multiplexing)即正交频分复用技术,实际上OFDM是MCM(M ...
- C# 递归读取XML菜单数据
在博客园注册了有4年了,很遗憾至今仍未发表过博客,趁周末有空发表第一篇博客.小生不才,在此献丑了! 最近在研究一些关于C#的一些技术,纵观之前的开发项目的经验,做系统时显示系统菜单的功能总是喜欢把数据 ...