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& ...
随机推荐
- 尚学linux课程---7、linux系统管理命令
尚学linux课程---7.linux系统管理命令 一.总结 一句话总结: 查网络:netstat -ntpl 查进程:ps 1.需要下载163yum源(从外部源同步仓库)里面的所有rpm文件? re ...
- re.groups取出来的空元祖??
源自学习笔记: day23_1_re_ groups方法取出来的字符是空的元组??为啥? ''' # ------------------------------------------------- ...
- Astyle 快速入门,常用指令
--style=java -n -p -c !E astyle是一个命令行工具,命令语法很简单: astyle [options] < original > Beauti ...
- PAT甲级——A1134 Vertex Cover【25】
A vertex cover of a graph is a set of vertices such that each edge of the graph is incident to at le ...
- import socket模块二
---恢复内容开始--- 优化两个小脚本实现不间断聊天: server.py: import socket sk = socket.socket() # 创建socket addess = ('127 ...
- C++的注释
### 1.2 注释 **作用**:在代码中加一些说明和解释,方便自己或其他程序员程序员阅读代码 **两种格式** 1. **单行注释**:// 描述信息 - 通常放在一行代码的上方,或者一条语句的 ...
- Loadrunner 性能测试工具笔记
性能的是的基础知识 什么是负载? 系统实际用户:可能会有很多人使用同一个系统,但并不是所有用户都回同时使用该系统,所以系统的实际用户是一个容量问题,而不是负载的问题 系统在线用户:当系统用户对系统进行 ...
- POJ 2318 /// 判断点与直线的位置关系
题目大意: n块玩具箱隔板 m个玩具落地点 给定玩具箱的左上和右下两个端点 接下来给定n块隔板的上点的x和下点的x(因为y就是玩具箱的上下边缘) 接下来给定m个玩具落地点 输出n+1个区域各有的玩具数 ...
- 面试系列15 如何保证Redis的高并发和高可用
就是如果你用redis缓存技术的话,肯定要考虑如何用redis来加多台机器,保证redis是高并发的,还有就是如何让Redis保证自己不是挂掉以后就直接死掉了,redis高可用 我这里会选用我之前讲解 ...
- 锋利的Jquery(p的onclick()事件)
1.一个p元素的点击事件 <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="serve ...