我的PAT-ADVANCED代码仓:https://github.com/617076674/PAT-ADVANCED

原题链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805500414115840

题目描述:

题目翻译:

1013 城市之间的战争

在战争中,所有的城市都通过高速公路连接在一起,这一点是至关重要的。如果一个城市被敌人占领了,那么所有连接这个城市的高速公路都会被封闭。我们必须马上知道为了使得余下的城市保持连接状态,我们是否需要修建其他的高速公路。给你一张城市地图,上面标识出了所有余下的高速公路,你需要快速说出需要修建的高速公路的数量。

举个例子,如果我们有3座城市,2条高速公路分别连接city1-city2、city1-city3。如果city1被敌人占领了,我们就需要修建一条高速公路,那就是city2-city3。

输入格式:

每个输入文件包含一个测试用例。对每个测试用例,第一行包含3个数字:N(<= 1000)表示城市总数量,M表示高速公路数量,K表示需要检查的城市数量。接下来的M行,每行用2个整数描述一条高速公路,这2个整数分别代表这条高速公路所连接的两个城市的编号。城市编号从1到N。最后一行有K个数字,代表了我们关注的城市。

输出格式:

对K个城市中的每一个城市,分别在1行中输出如果该城市被敌人占领所需要修建的高速公路的数量。

输入样例:

3 2 3
1 2
1 3
1 2 3

输出样例:

1
0
0

知识点:图的深度优先遍历、并查集

思路一:图的深度优先遍历(邻接矩阵实现)

本题的实质是求除去某个点之外,图中有几个连通块。用图的深度优先遍历算法即可。

时间复杂度是O(K * N)。空间复杂度是O(N ^ 2)。

注意点:

城市编号是1 ~ N,而不是0 ~ N - 1。

C++代码:

#include<iostream>
#include<vector>using namespace std;int N, M, K;
vector<int> graph[1001];
int lost_city, count;
bool visited[1001];void dfs(int nowVisit);int main(){scanf("%d %d %d", &N, &M, &K);int city1, city2;for(int i = 0; i < M; i++){scanf("%d %d", &city1, &city2);graph[city1].push_back(city2);graph[city2].push_back(city1);}for(int i = 0; i < K; i++){fill(visited + 1, visited + N + 1, false);count = 0;scanf("%d", &lost_city);for(int j = 1; j <= N; j++){if(j == lost_city){continue;}if(!visited[j]){dfs(j);count++;}}printf("%d\n", count - 1);}return 0;
} void dfs(int nowVisit){visited[nowVisit] = true;for(int i = 0; i < graph[nowVisit].size(); i++){int v = graph[nowVisit][i];if(!visited[v] && v != lost_city){dfs(v);}}
}

C++解题报告:

思路二:并查集(邻接表实现)

时间复杂度是O(kN)。空间复杂度是O(N + M)。

注意为并查集添加路径压缩操作。

C++代码:

#include<iostream>
#include<vector>
#include<set>using namespace std;struct edge{int u, v;edge(int _u, int _v){u = _u;v = _v;}
};int N, M, K;
vector<edge> edges;
int lost_city, count;
int father[1001];
set<int> fathers;int findFather(int x);
void unionTwo(int a, int b);int main(){scanf("%d %d %d", &N, &M, &K);int city1, city2;for(int i = 0; i < M; i++){scanf("%d %d", &city1, &city2);edges.push_back(edge(city1, city2));}for(int i = 0; i < K; i++){for(int j = 1; j < N + 1; j++){father[j] = j;}count = 0;fathers.clear();scanf("%d", &lost_city);for(int j = 0; j < edges.size(); j++){if(edges[j].u == lost_city || edges[j].v == lost_city){continue;}unionTwo(edges[j].u, edges[j].v);}for(int j = 1; j < N + 1; j++){if(j == lost_city){continue;}fathers.insert(findFather(j));}printf("%d\n", fathers.size() - 1);}return 0;
} int findFather(int x){int a = x;while(x != father[x]){x = father[x];}while(a != father[a]){int z = a;a = father[a];father[z] = x;}return x;
}void unionTwo(int a, int b){int a_father = findFather(a);int b_father = findFather(b);if(a_father != b_father){father[a_father] = b_father;}
}

C++解题报告:

思路三:图的广度优先遍历(邻接表实现)

本题的实质是求除去某个点之外,图中有几个连通块。用图的广度优先遍历算法也可以实现。

时间复杂度是O(K * N)。空间复杂度是O(N + K)。

C++代码:

#include<iostream>
#include<vector>
#include<queue>using namespace std;int N, M, K;
vector<int> graph[1001];  //无向图
bool inq[1001];
int lost_city, count;void bfs(int nowVisit);int main() {scanf("%d %d %d", &N, &M, &K);int city1, city2;for(int i = 0; i < M; i++) {scanf("%d %d", &city1, &city2);graph[city1].push_back(city2);graph[city2].push_back(city1);}for(int i = 0; i < K; i++) {scanf("%d", &lost_city);count = 0;fill(inq + 1, inq + N + 1, false);for(int j = 1; j < N + 1; j++) {if(j == lost_city) {continue;}if(!inq[j]) {bfs(j);count++;}}printf("%d\n", count - 1);}return 0;
}void bfs(int nowVisit) {queue<int> q;q.push(nowVisit);inq[nowVisit] = true;while(!q.empty()) {int now = q.front();q.pop();for(int i = 0; i < graph[now].size(); i++) {if(graph[now][i] != lost_city && !inq[graph[now][i]]) {q.push(graph[now][i]);inq[graph[now][i]] = true;}}}
}

C++解题报告:

PAT-ADVANCED1013——Battle Over Cities相关推荐

  1. PAT甲级1013 Battle Over Cities:[C++题解]并查集、结构体存边

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:并查集题目. 不清楚并查集的小伙伴,请移步并查集原理并查集板子:acwing836. 合并集合. 题意:给定一个连通图,当删掉任意1个 ...

  2. PAT TOP 1001 Battle Over Cities - Hard Version (35)

    问题描述: 1001 Battle Over Cities - Hard Version (35 分) It is vitally important to have all the cities c ...

  3. PAT (Advanced Level) Practise 1013. Battle Over Cities (25)

    1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...

  4. 1013 Battle Over Cities(并查集解法)

    关于背景的介绍见1013 Battle Over Cities(图的DFS解法) DFS就是不算特定结点后数连通子图的总数,再减一.我想着那么并查集就是数不算特定节点后,集合元素(根)的个数.但是我弄 ...

  5. 1013. Battle Over Cities (25) 连通图

    1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...

  6. Battle over Cities

    Battle over Cities Time Limit : 10000/5000ms (Java/Other)   Memory Limit : 65536/65536K (Java/Other) ...

  7. 【PAT甲级 - 1013】Battle Over Cities (25分)(并查集)

    题干: It is vitally important to have all the cities connected by highways in a war. If a city is occu ...

  8. 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 occ ...

  9. 【解析】1013 Battle Over Cities (25 分)_31行代码AC

    立志用最少的代码做最高效的表达 PAT甲级最优题解-->传送门 It is vitally important to have all the cities connected by highw ...

  10. PAT1013. Battle Over Cities (25)

    题目如下: It is vitally important to have all the cities connected by highways in a war. If a city is oc ...

最新文章

  1. chapter15 机器学习之大数据与mapreduce
  2. 常规操作中浏览器缓存检测与服务器请求机制总结
  3. SQL Server 2008 : 基于策略的管理(Policy-Based Management)
  4. 数据可用不可见!揭秘蚂蚁区块链摩斯安全计算平台
  5. 虚拟器件—虚拟化技术的新利刃 | 时光机
  6. 互联网人必读 | 大数据思维的十大核心原理
  7. MyBatis使用log4j输出日志
  8. Unity对接Steam SDK
  9. Qt图像处理技术二:对QImage图片简单滤镜(暖色,冷色,反色,老照片,灰度)
  10. 第三方支付(微信支付)支付流程分析
  11. python绘制科赫曲线
  12. Oracle table move tablespace
  13. 佐治亚理工计算机科学在线硕士,佐治亚理工学院计算机研究生申请要求及截止时间一览...
  14. 解决webpack打包css时CssSyntaxError的问题
  15. 玩转控件DTPicker
  16. 使用IE浏览器,禁止访问,显示 Internet Explorer增强安全配置正在阻止来自下列网站的从应用程序中的内容
  17. cad是计算机辅助设计什么,“什么是cad软件“cad是什么
  18. gitlab修改附件上传文件大小限制
  19. M5311接入onenet(LwM2M方式)
  20. 用计算机做出来的歌,拜拜了小白(音乐制作篇)电脑音乐制作到底是啥

热门文章

  1. vmsysjack-tupian
  2. MPI和OpenMP实现矩阵相乘
  3. ubuntu如何安装本地deb文件
  4. Python实现汉字人名按拼音或笔画顺序排序
  5. web小白,实战操作拿到网站后台账户和密码
  6. 孩子必听的数学家故事——笛卡尔
  7. uwp浏览器java源码_从网站打开UWP应用程序
  8. java.lang.ArithmeticException: Division undefined
  9. 第一章---近红外光谱概述2(近红外光谱分析难点及解决思路)
  10. 【axios】get和post请求用法