【PAT Advanced Level】1013. Battle Over Cities (25)
这题给定了一个图,我用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)的更多相关文章
- 【PAT Advanced Level】1008. Elevator (20)
没什么难的,简单模拟题 #include <iostream> using namespace std; int main() { int num; cin>>num; int ...
- 【PAT Advanced Level】1004. Counting Leaves (30)
利用广度优先搜索,找出每层的叶子节点的个数. #include <iostream> #include <vector> #include <queue> #inc ...
- 【PAT Advanced Level】1006. Sign In and Sign Out (25)
关键在于清空字符数组和使用scanf进行输入 #include <stdio.h> #include <string.h> #include <fstream> # ...
- 【PAT Advanced Level】1014. Waiting in Line (30)
简单模拟题,注意读懂题意就行 #include <iostream> #include <queue> using namespace std; #define CUSTOME ...
- 【PAT Advanced Level】1015. Reversible Primes (20)
转换进制&&逆序可以在一起进行,有一点技巧,不要用十进制数来表示低进制,容易溢出. #include <iostream> #include <vector> ...
- 【PAT Advanced Level】1011. World Cup Betting (20)
简单模拟题,遍历一遍即可.考察输入输出. #include <iostream> #include <string> #include <stdio.h> #inc ...
- 【PAT甲级】1013 Battle Over Cities (25 分)(并查集,简单联通图)
题意: 输入三个整数N,M,K(N<=1000,第四个数据1e5<=M<=1e6).有1~N个城市,M条高速公路,K次询问,每次询问输入一个被敌军占领的城市,所有和该城市相连的高速公 ...
- PAT甲题题解-1013. Battle Over Cities (25)-求联通分支个数
题目就是求联通分支个数删除一个点,剩下联通分支个数为cnt,那么需要建立cnt-1边才能把这cnt个联通分支个数求出来怎么求联通分支个数呢可以用并查集,但并查集的话复杂度是O(m*logn*k)我这里 ...
- PAT 解题报告 1013. Battle Over Cities (25)
1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...
随机推荐
- iOS Sprite Kit教程之使用帮助文档以及调试程序
iOS Sprite Kit教程之使用帮助文档以及调试程序 IOS中使用帮助文档 在编写代码的时候,可能会遇到很多的方法.如果开发者对这些方法的功能,以及参数不是很了解,就可以使用帮助文档.那么帮助文 ...
- 如何破解安卓App
韩梦飞沙 yue31313 韩亚飞 han_meng_fei_sha 313134555@qq.com 如何破解安卓App
- [BZOJ4864][BeiJing2017Wc]神秘物质(splay)
首先merge就是先delete两次再insert,Max就是整个区间的最大值减最小值,Min就是区间中所有相邻两数差的最小值. Splay支持区间最大值,区间最小值,区间相邻差最小值即可. #inc ...
- 洛谷.4525.[模板]自适应辛普森法1(Simpson积分)
题目链接 Simpson积分公式:\[\int_a^bf(x)dx\approx\frac{b-a}{6}\left[f(a)+f(b)+4f(\frac{a+b}{2})\right]\] 推导过程 ...
- BZOJ5137[Usaco2017 Dec]Standing Out from the Herd
看了半天题 不知道怎么用SAM维护 于是借(chao)鉴(xi)的一发神犇的 只要判断这个子串之前被标记的记号(也就是他属于第几个串)和这次转移到的是否相同 如果不同就说明该子串属于多个串 直接标记- ...
- C++反汇编-继承和多重继承
学无止尽,积土成山,积水成渊-<C++反汇编与逆向分析技术揭秘> 读书笔记 一.单类继承 在父类中声明为私有的成员,子类对象无法直接访问,但是在子类对象的内存结构中,父类私有的成员数据依然 ...
- ARM FPGA Extended Memory Interface
Connect a ARM Microcontroller to a FPGA using its Extended Memory Interface (EMI) http://elinux.org/ ...
- Windows Phone Silverlight 8.1 apps
The Windows Phone Silverlight 8.1 app model gives Windows Phone 8 developers access to some of the n ...
- 给.DLL文件加一个数字签名的方法
给.dll文件加一个数字签名的方法 效果如图所示: 做法: 下载数字签名工具包:http://files.cnblogs.com/babyt/SignTool.rar /Files/JavaC ...
- java多线程实现主线程等待子线程执行完问题
本文介绍两种主线程等待子线程的实现方式,以5个子线程来说明: 1.使用Thread的join()方法,join()方法会阻塞主线程继续向下执行. 2.使用Java.util.concurrent中的C ...