It is vitally important to have all the cities connected by highways in a war. If a city is occupied by the enemy, all the highways from/toward that city are closed. We must know immediately if we need to repair any other highways to keep the rest of the cities connected. Given the map of cities which have all the remaining highways marked, you are supposed to tell the number of highways need to be repaired, quickly.

For example, if we have 3 cities and 2 highways connecting city​1​​-city​2​​ and city​1​​-city​3​​. Then if city​1​​ is occupied by the enemy, we must have 1 highway repaired, that is the highway city​2​​-city​3​​.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 3 numbers N (<), 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 Knumbers, which represent the cities we concern.

Output Specification:

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
分析:添加的最少的路线,就是他们的连通分量数-1,因为当a个互相分立的连通分量需要变为连通图的时候,只需要添加a-1个路线,就能让他们相连。所以这道题就是求去除了某个结点之后其他的图所拥有的连通分量数
使用邻接矩阵存储,对于每一个被占领的城市,去除这个城市结点,就是把它标记为已经访问过,这样在深度优先遍历的时候,对于所有未访问的结点进行遍历,就能求到所有的连通分量的个数~记得因为有很多个要判断的数据,每一次输入被占领的城市之前要把visit数组置为false~~
 
 #include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int v[][];
bool visit[];
int n;
void dfs(int node) {
visit[node] = true;
for (int i = ; i <= n; i++) {
if (visit[i] == false && v[node][i] == )
dfs(i);
}
}
int main() {
int m, k, a, b;
scanf("%d%d%d", &n, &m, &k);
for (int i = ; i < m; i++) {
scanf("%d%d", &a, &b);
v[a][b] = v[b][a] = ;
}
for (int i = ; i < k; i++) {
fill(visit, visit + , false);
scanf("%d", &a);
int cnt = ;
visit[a] = true;
for (int j = ; j <= n; j++) {
if (visit[j] == false) {
dfs(j);
cnt++;
}
}
if(n>)
printf("%d\n", cnt - );
else
printf("%d\n", cnt);
}
return ;
}

最新文章

  1. zepto和jquery的区别,zepto的不同使用8条小结
  2. poj1743 后缀数组求不可重叠的重复出现的子串最长长度
  3. 【BZOJ-4568】幸运数字 树链剖分 + 线性基合并
  4. iOS GCD 必读推荐,有关于单例使用问题
  5. 【TYVJ】1467 - 通向聚会的道路(spfa+特殊的技巧)
  6. linux驱动程序之电源管理之regulator机制流程 (1)
  7. SPRING IN ACTION 第4版笔记-第二章WIRING BEANS-008-在Java配置文件中引入xml配置文件@Import、@ImportResource
  8. visualSVN server库迁移(转)
  9. OpenGl绘制螺旋线
  10. 基于SUSE Linux做NFS文件挂载
  11. wifi 破解笔记
  12. #研发解决方案#研发协作平台CloudEngine
  13. jenkins拉源码设置参数化构建选项为tagname
  14. linux下mysql 文件导入导出
  15. c#NamedPipe命名管道通信例子
  16. pytest.1.快速开始
  17. stm32架构初认识
  18. Swift开发教程--设置UIViewController的背景透明
  19. hdu-题目1421:搬寝室
  20. spark读取gz文件

热门文章

  1. JAVA判断一个对象生存还是死亡
  2. PAT甲级——A1139 First Contact【30】
  3. neo4j中cypher语句多个模糊查询
  4. java关键字之instanceof
  5. 2019-10-4-C#-极限压缩-dotnet-core-控制台发布文件
  6. passwd的使用例子
  7. LINUX交换分区
  8. Python全栈开发:pymysql
  9. Jsp Layout 布局页
  10. MySQL数据库(安装+增删改查)