题目如下:

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

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.

Output

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 即为答案。用一个数组visited表示是否被访问过,然后深度优先搜索即可。每次遍历一个连通分量,最后算出连通分量的数目。代码如下:

#include <iostream>
#include <vector>
using namespace std;
int highways[1001][1001];   //用邻接矩阵表示城市之间的连通情况
bool visited[1001]; //是否被访问过
int N, M, K;
void dfs(int index)
{visited[index] = true;for (int i = 1; i < N + 1; i++){if (highways[index][i] == 1 && !visited[i]){dfs(i);}}
}int main()
{cin >> N >> M >> K;int city1, city2,city,count;for (int i = 0;i < M;i++){cin >> city1 >> city2;highways[city1][city2] = 1;highways[city2][city1] = 1;}for (int i = 0; i < K;i++){cin >> city;count = 0;fill(visited, visited + 1001, false);visited[city] = true;for (int j = 1; j < N + 1;j++){if (!visited[j]){dfs(j);count++;}}cout << count - 1<< endl;}return 0;
}

一次通过用例。主要是对图还不熟悉,需要进行相关复习。

PAT1013. Battle Over Cities (25)相关推荐

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

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

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

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

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

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

  4. 【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 ...

  5. 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 ...

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

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

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

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

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

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

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

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

最新文章

  1. 剑指offer 刷题 01
  2. centos安装JDK、Tomcat、mysql
  3. 现在比较流行的三级菜单,京东商城,淘宝网效果等等。(修正版)
  4. 幻兽天下修复版java_宠物天下四-幻兽天下
  5. 即时通讯软件几个主要下载分类
  6. Android Camera详解
  7. Pytorch:初始化
  8. 杭电oj-----Nightmare(BFS)
  9. php+分割文本文件,python实现:将文本文件分割成多个小文本文件(php也可实现)...
  10. 爱普森针式打印机怎么连接计算机,爱普生针式打印机怎么安装 爱普生针式打印机安装步骤详解【图文】...
  11. 高等数学下——平面与直线
  12. 读张满胜之《英语语法新思维初级教程》
  13. 自称骇客的基本都是骗子
  14. LeetCode练习题:斐波那契数列
  15. 谢惠民,恽自求,易法槐,钱定边编数学分析习题课讲义23.2.3练习题参考解答[来自陶哲轩小弟]...
  16. MacBook Pro电脑一键切换输入法
  17. 去耦电容与旁路电容的应用技巧
  18. 优秀程序员的七大特征,你具备几条?
  19. 东北大学应用数理统计第二章知识点总结——参数估计
  20. 人工神经网络 经验公式_文科生看了会沉默,理科生看了会流泪的物理公式!你知道几个?(看完长见识)...

热门文章

  1. 讯飞线上赛总结(一)
  2. 给自己看的unity知识点随手记(记录)
  3. 【成长之路】本科比赛经验分享
  4. 计算机技术应用培训教案,计算机应用知识培训教案
  5. 由于找不到MSVCR120.dll,无法继续执行代码,重新安装程序可能会解决此问题。
  6. 苹果x和xsmax有什么区别_支持4G的苹果11Pro和支持5G的苹果12Pro有什么区别呢?
  7. slice和splice
  8. c语言iq测试,IQ智商测试题目及答案分析
  9. Fedora12下播放歌曲 rhythmbox
  10. error: refname refs/heads/master not found fatal: Branch rename failed