立志用最少的代码做最高效的表达


PAT甲级最优题解——>传送门


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.
I
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​​.

IInput Specification:
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.

IOutput Specification:
For each of the K cities, output in a line the number of highways need to be repaired if that city is lost.

ISample Input:
3 2 3
1 2
1 3
1 2 3

Sample Output:
1
0
0


题意:n座城,m条路,k个沦陷的城市, 要求每个城市之间都要有通信, 求某城市沦陷后至少修几条路才能达到要求。

分析:经典连通块问题。 计算城市网中有多少连通块,连通块个数-1就是要修的道路数(连通块彼此相连,则所有城市相连)。

解法:DFS解法与拓扑排序解法皆可。 先写了DFS解法,拓扑排序解法明天更新~

注意:如果用cin、cout输出,最好加上ios::sync_with_stdio(false);这句代码,可以减少大概100ms的时间规模(比scanf、printf还快)。

关闭流同步前:

关闭流同步后:


代码一:DFS解法

#include<bits/stdc++.h>
using namespace std;int n, m, k, lost_city, G[1010][1010], vis[1010];void dfs(int u) {for(int v = 1; v <= n; v++) if(G[u][v] == 1 && vis[v] == 0 && v != lost_city) {vis[v] = 1;dfs(v);
//          vis[v] = 0;        求连通块时无回溯 }
}int main() {ios::sync_with_stdio(false);cin >> n >> m >> k;for(int i = 0; i < m; i++) {int a, b; cin >> a >> b;G[a][b] = G[b][a] = 1;}for(int i = 0; i < k; i++) {cin >> lost_city;memset(vis, 0, sizeof(vis));  //数组初始化int num = -1;       //一定有一块连通块,因此初值-1 for(int j = 1; j <= n; j++) {   //逐个点遍历 if(vis[j] == 0 && j != lost_city) {  //没遍历过且不等于x(x城已消失) vis[j] = 1; dfs(j);num++;}cout << num << '\n';} return 0;
}

代码二:拓扑排序解法(待更)


耗时:

【解析】1013 Battle Over Cities (25 分)_31行代码AC相关推荐

  1. 【最详细解析】1070 结绳 (25分)_18行代码AC

    立志用更少的代码做更高效的表达 Pat乙级最优化代码+题解+分析汇总-->传送门 给定一段一段的绳子,你需要把它们串成一条绳.每次串连的时候,是把两段绳子对折,再如下图所示套接在一起.这样得到的 ...

  2. 1013 Battle Over Cities (25分)(用割点做)

    仅以此题纪念脑瘫的我.好吧看见1000的范围没有多想什么,直接跑了一遍tarjan,md其实暴力就可以...乌鱼子 题解: tarjan记录割点能把图分成几个. tarjan可以适用于1e5数量级别的 ...

  3. 1013 Battle Over Cities (25 分) 【难度: 中 / 知识点: 连通块】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805500414115840 将这些连通块,连接起来最少的边,即是答案. ...

  4. 【详细解析】1080 MOOC期终成绩 (25分)_45行代码AC

    立志用更少的代码做更高效的表达 PAT乙级最优题解-->传送门 对于在中国大学MOOC(http://www.icourse163.org/ )学习"数据结构"课程的学生,想 ...

  5. 【附超时原因】1055 The World‘s Richest (25 分)_42行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Forbes magazine publishes every year its list of billionaires bas ...

  6. 【简便解法】1035 插入与归并 (25分)_37行代码AC

    立志用更少的代码做更高效的表达 PAT乙级最优题解-->传送门 根据维基百科的定义: 插入排序是迭代算法,逐一获得输入数据,逐步产生有序的输出序列.每步迭代中,算法从输入序列中取出一元素,将之插 ...

  7. 【简便解法】1090 危险品装箱 (25分)_33行代码AC

    立志用最少的代码做最高效的表达 PAT乙级最优题解-->传送门 集装箱运输货物时,我们必须特别小心,不能把不相容的货物装在一只箱子里.比如氧化剂绝对不能跟易燃液体同箱,否则很容易造成爆炸. 本题 ...

  8. 案例4-1.6 树种统计 (25 分)_18行代码AC

    立志用最少的代码做最高效的表达 随着卫星成像技术的应用,自然资源研究机构可以识别每一棵树的种类.请编写程序帮助研究人员统计每种树的数量,计算每种树占总数的百分比. 输入格式: 输入首先给出正整数N(≤ ...

  9. 1097 Deduplication on a Linked List (25 分)_35行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 Given a singly linked list L with integer keys, you are supposed ...

最新文章

  1. 路由器无服务器无响应是怎么回事啊,wifi服务器无响应怎么解决(图文)
  2. centos6下的mysql的安装
  3. html使用共同的头部导航
  4. Java高并发编程(一):并发编程的挑战
  5. GraphQL:简单开开始一个查询
  6. deepin部署python开发环境_deepin系统下部署Python3.5的开发及运行环境
  7. 在Anaconda中安装TensorFlow1.14.0与TensorFlow2.0.0
  8. MFC如何让输入框只能输入数字
  9. POJ3122-Pie
  10. Kali Linux 软件源与更新源和更新命令
  11. 电机与拖动课程最全思维导图笔记
  12. 手机用计算机微信运动记步吗,微信运动怎么开启 微信运动开启计步功能教程...
  13. 开源配置管理系统的选择和搭建
  14. 电路设计经验总结(以软件cadence allegro为例)
  15. 高通linux平台(mdm9x07,sdx12)连接qact
  16. 期中考试数据库插入查询
  17. RiPro美化二开详细修改路径介绍
  18. Linux下的光盘刻录
  19. CRM(01 线索)
  20. Java中的System.getenv()和System.getProperty()

热门文章

  1. 个推异常值检测和实战应用
  2. 数据结构与算法 | 二叉树四种的遍历方法(递归与非递归)
  3. Win下通过 Navica t连接Ubuntu下MySQL数据库
  4. 一定要牢牢掌握的技术!Spark+Flink+推荐系统+数据挖掘等
  5. 作为程序员,这些实用工具你必须要知道!
  6. Java8 中用法优雅的 Stream,性能也优雅吗?
  7. 当SRS遇到K8s:如何构建海量推流源站?
  8. 【大会】海量高清视频服务端架构设计的变与不变
  9. 董海冰:2018风云再起 ,期待AV1、AI和AR
  10. Javascript 多线程编程​的前世今生