PAT甲级——A1013 Battle Over Cities
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 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 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
使用邻接矩阵存储,对于每一个被占领的城市,去除这个城市结点,就是把它标记为已经访问过,这样在深度优先遍历的时候,对于所有未访问的结点进行遍历,就能求到所有的连通分量的个数~记得因为有很多个要判断的数据,每一次输入被占领的城市之前要把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 ;
}
PAT甲级——A1013 Battle Over Cities的更多相关文章
- PAT甲级1013. Battle Over Cities
PAT甲级1013. Battle Over Cities 题意: 将所有城市连接起来的公路在战争中是非常重要的.如果一个城市被敌人占领,所有从这个城市的高速公路都是关闭的.我们必须立即知道,如果我们 ...
- 图论 - PAT甲级 1013 Battle Over Cities C++
PAT甲级 1013 Battle Over Cities C++ It is vitally important to have all the cities connected by highwa ...
- 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 A1013 Battle Over Cities (25 分)——图遍历,联通块个数
It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...
- A1013. Battle Over Cities
It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...
- 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 ...
- PAT A 1013. Battle Over Cities (25)【并查集】
https://www.patest.cn/contests/pat-a-practise/1013 思路:并查集合并 #include<set> #include<map> ...
- PAT_A1013#Battle Over Cities
Source: PAT A1013 Battle Over Cities (25 分) Description: It is vitally important to have all the cit ...
- PAT甲级题解分类byZlc
专题一 字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...
随机推荐
- Systm.IO.File.cs
ylbtech-Systm.IO.File.cs 1.程序集 mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c5619 ...
- 二分图最佳匹配KM算法 /// 牛客暑期第五场E
题目大意: 给定n,有n间宿舍 每间4人 接下来n行 是第一年学校规定的宿舍安排 接下来n行 是第二年学生的宿舍安排意愿 求满足学生意愿的最少交换次数 input 2 1 2 3 4 5 6 7 8 ...
- USACO 2007 February Silver The Cow Lexicon /// DP oj24258
题目大意: 输入w,l: w是接下来的字典内的单词个数,l为目标字符串长度 输入目标字符串 接下来w行,输入字典内的各个单词 输出目标字符串最少删除多少个字母就能变成只由字典内的单词组成的字符串 Sa ...
- Spring MVC上传、下载 文件
1,上传文件 public static String upload(MultipartFile file, SysUserBean sysUserBean, HttpServletRequest r ...
- Activiti常用类介绍
为什么要使用工作流? 传统的设计在流程发生变化时的弊端: 1. 流程相关的属性和业务对象的属性,都放到了业务对象中. 2. 流程相关的逻辑和业务逻辑,都放到的业务逻辑中 常用类 ProcessEngi ...
- 转为win64后, MS的lib问题
> 正在创建库 C:\Users\Administrator\Desktop\branch-Unicode-156\\Temp\Link\PointCloudMeasure\x64 ...
- JS流程控制语句 反反复复(while循环) 和for循环有相同功能的还有while循环, while循环重复执行一段代码,直到某个条件不再满足。
反反复复(while循环) 和for循环有相同功能的还有while循环, while循环重复执行一段代码,直到某个条件不再满足. while语句结构: while(判断条件) { 循环语句 } 使用w ...
- C++指针类型间强制转换
深入理解指针类型间的转换 C++中指针的强制转换 强制类型转换(int).(int&)和(int*)的区别 内存中的地址 地址的本质就是一串0和1的机器代码,内存中的地址没有明确数据类型,但地 ...
- promise体验
promise的执行流程 promise串行执行异步 job1.then(job2).then(job3).catch(handleError); // 0.5秒后返回input*input的计算结果 ...
- Android之TableLayout表格布局
1.相关属性 1.1.常用属性 android:collapseColumns 设置需要被隐藏的列的序列号 android:shrinkColumns 设置允许被收缩的列的序列号 android:st ...