这题给定了一个图,我用DFS的思想,来求出在图中去掉某个点后还剩几个相互独立的区域(连通子图)。

在DFS中,每遇到一个未访问的点,则对他进行深搜,把它能访问到的所有点标记为已访问。一共进行了多少次这样的搜索,

就是我们要求的独立区域的个数。

#include <iostream>
#include <fstream>
#include <memory.h>
using namespace std; const int maxNum = 1001; bool visited[maxNum];
int edge[maxNum][maxNum];
int N, M, K; void DFS(int begin)
{
for(int i = 1; i <= N; i++)
{
if(edge[begin][i] && !visited[i])
{
visited[i] = true;
DFS(i);
}
}
} int main()
{
//fstream cin("a.txt"); cin>>N>>M>>K;
for(int i = 1; i <= M; i++)
{
int tmp1, tmp2;
cin>>tmp1>>tmp2;
edge[tmp1][tmp2] = edge[tmp2][tmp1] = 1;
}
memset(visited, 0, maxNum); int result = 0;
for(int i = 0; i < K; i++)
{
int k;
cin>>k;
visited[k] = true;
for(int j = 1; j <= N; j++)
{
if(!visited[j])
{
DFS(j);
result++;
}
}
cout<<result - 1<<endl;
memset(visited, 0, maxNum);
result = 0;
}
}

【PAT Advanced Level】1013. Battle Over Cities (25)的更多相关文章

  1. 【PAT Advanced Level】1008. Elevator (20)

    没什么难的,简单模拟题 #include <iostream> using namespace std; int main() { int num; cin>>num; int ...

  2. 【PAT Advanced Level】1004. Counting Leaves (30)

    利用广度优先搜索,找出每层的叶子节点的个数. #include <iostream> #include <vector> #include <queue> #inc ...

  3. 【PAT Advanced Level】1006. Sign In and Sign Out (25)

    关键在于清空字符数组和使用scanf进行输入 #include <stdio.h> #include <string.h> #include <fstream> # ...

  4. 【PAT Advanced Level】1014. Waiting in Line (30)

    简单模拟题,注意读懂题意就行 #include <iostream> #include <queue> using namespace std; #define CUSTOME ...

  5. 【PAT Advanced Level】1015. Reversible Primes (20)

    转换进制&&逆序可以在一起进行,有一点技巧,不要用十进制数来表示低进制,容易溢出. #include <iostream> #include <vector> ...

  6. 【PAT Advanced Level】1011. World Cup Betting (20)

    简单模拟题,遍历一遍即可.考察输入输出. #include <iostream> #include <string> #include <stdio.h> #inc ...

  7. 【PAT甲级】1013 Battle Over Cities (25 分)(并查集,简单联通图)

    题意: 输入三个整数N,M,K(N<=1000,第四个数据1e5<=M<=1e6).有1~N个城市,M条高速公路,K次询问,每次询问输入一个被敌军占领的城市,所有和该城市相连的高速公 ...

  8. PAT甲题题解-1013. Battle Over Cities (25)-求联通分支个数

    题目就是求联通分支个数删除一个点,剩下联通分支个数为cnt,那么需要建立cnt-1边才能把这cnt个联通分支个数求出来怎么求联通分支个数呢可以用并查集,但并查集的话复杂度是O(m*logn*k)我这里 ...

  9. PAT 解题报告 1013. Battle Over Cities (25)

    1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...

随机推荐

  1. WebApi入门

    饮水思源 http://www.cnblogs.com/guyun/p/4589115.html http://www.cnblogs.com/chutianshu1981/p/3288796.htm ...

  2. [ 原创 ]Centos 7.0下安装 Tomcat8.5.15

    Tomcat下载地址:http://tomcat.apache.org/download-80.cgi#8.5.15 上传到文件夹 并解压缩 出现问题: 解决方法: http://blog.csdn. ...

  3. Sublime Text 下的Install Package安装方法

    废话不多说.... 如果你是Sublime Text3用户,按下Ctrl+~呼出控制台,输入以下代码 import urllib.request,os,hashlib; h = '7183a2d3e9 ...

  4. [Luogu5161]WD与数列(后缀数组/后缀自动机+线段树合并)

    https://blog.csdn.net/WAautomaton/article/details/85057257 解法一:后缀数组 显然将原数组差分后答案就是所有不相交不相邻重复子串个数+n*(n ...

  5. Git 简易使用指南及补充

    Git最简易的使用指南 助你开始使用 git 的简易指南,木有高深内容,;) 安装 下载 git OSX 版 下载 git Windows 版 下载 git Linux 版 创建新仓库 创建新文件夹, ...

  6. Xcode 几个图标解释

    File.让您指定串联图的常规信息. Quick Help.提供有关对象的实用文稿. Identity.让您指定对象的自定类并定义其辅助功能属性. Attributes.让您自定对象的可视化属性. S ...

  7. Git_期末总结

    终于到了期末总结的时刻了! 经过几天的学习,相信你对Git已经初步掌握.一开始,可能觉得Git上手比较困难,尤其是已经熟悉SVN的童鞋,没关系,多操练几次,就会越用越顺手. Git虽然极其强大,命令繁 ...

  8. JS仿淘宝左侧菜单

    http://www.webdm.cn/webcode/1c724a06-06f4-4c4f-931a-c683285fa700.html

  9. Syslink Control in MFC 9.0(转)

    Visual Studio 2008 (formely code-named ‘Orcas’) has several important updates for VC++ and MFC. Amon ...

  10. High Voltage Boost Supply

    http://learn.adafruit.com/ice-tube-clock-kit/design Tubes such as VFDs, Nixies, Decatrons, etc requi ...