Battle Over Cities (25)(DFS、连通图)
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
主要思想:一张连通图,去掉一个节点后,想把个个节点连通起来,至少需要添加的边的数目n。n = 该连通图,去掉一个节点后,所形成的 极大连通子图的个数 - 1.
所以 只要把去掉的点定义为检测点,DFS不要去遍历它,计算出极大连通子图的个数便可,算法如下:
void DFS(int x,int N) //N为检测点 { visit[x]=; for(int i=;i<Ladj[x].size();i++) { if(visit[Ladj[x][i]]==&&Ladj[x][i]!=N)//不等于被检测的点 DFS(Ladj[x][i],N); } }
int count =; for(j=;j<=n;j++) { if(visit[j]==&&j!=check[i])//不等于被检测的点,check数组保存检测点 { count++;//极大连通子图的个数 DFS(j,check[i]); } } cout<<count-<<endl;
AC代码:
#include <iostream> #include <vector> using namespace std; vector<int> Ladj[]; int check[];//存放要检测的点 int visit[]; void DFS(int x,int N) { visit[x]=; for(int i=;i<Ladj[x].size();i++) { if(visit[Ladj[x][i]]==&&Ladj[x][i]!=N)//不等于被检测的点 DFS(Ladj[x][i],N); } } int main() { int n,m,k,i,j; while(cin>>n) { cin>>m>>k; for(i=;i<m;i++) { int a,b; cin>>a>>b; Ladj[a].push_back(b); Ladj[b].push_back(a); } for(i=;i<k;i++) { cin>>check[i]; } for(i=;i<k;i++) { int count=; for(j=;j<=n;j++)//初始化 { visit[j]=; } for(j=;j<=n;j++) { if(visit[j]==&&j!=check[i])//不等于被检测的点 { count++; DFS(j,check[i]); } } cout<<count-<<endl; } } return ; }
Battle Over Cities (25)(DFS、连通图)的更多相关文章
- 1013 Battle Over Cities (25分) DFS | 并查集
1013 Battle Over Cities (25分) It is vitally important to have all the cities connected by highways ...
- PAT 解题报告 1013. Battle Over Cities (25)
1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...
- PAT 甲级 1013 Battle Over Cities (25 分)(图的遍历,统计强连通分量个数,bfs,一遍就ac啦)
1013 Battle Over Cities (25 分) It is vitally important to have all the cities connected by highway ...
- pat1013. Battle Over Cities (25)
1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...
- 1013. Battle Over Cities (25)(DFS遍历)
For example, if we have 3 cities and 2 highways connecting city1-city2 and city1-city3. Then if city ...
- PAT-1013 Battle Over Cities (25 分) DFS求连通块
It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...
- PAT Advanced 1013 Battle Over Cities (25) [图的遍历,统计连通分量的个数,DFS,BFS,并查集]
题目 It is vitally important to have all the cities connected by highways in a war. If a city is occup ...
- 1013 Battle Over Cities (25分) 图的连通分量+DFS
题目 It is vitally important to have all the cities connected by highways in a war. If a city is occup ...
- 1013. Battle Over Cities (25)
题目如下: It is vitally important to have all the cities connected by highways in a war. If a city is oc ...
随机推荐
- [课程相关]homework-05
零.准备工作 队伍成员:梁杰,夏天晗,谢祖三. 周五晚上吃完饭,我们就开始了讨论. 这次的要求是写服务器,客户端以及游戏结果动态显示.很明显是三个部分,我们也就顺其自然, 一人一个部分.我负责服务器, ...
- MSP430常见问题之FLASH存储类
Q1:用IAR Embedded Workbench for MSP430 通过JTAG往MSP430上写程序.为了知道片内程序的版本,必须读出Flash 中内容.什么工具软件可以通过JTAG口实现这 ...
- 学习ajax 总结
一.服务器客户端基础知识 通信是指不同计算机程序的通信,单单通过ip地址就能知道你找的是哪一台计算机,但是不知道是计算机上的哪个应用程序,要想知道是哪个程序就必须通过端口.这时候就可以问端口到底是什么 ...
- 基本STRUTS标签-学习笔记-Bean标签
<bean:include> 和标准的JSP标签<jsp:include>很相似,都可以用来包含其他Web资源的内容,区别在于<bean:include>标签把其它 ...
- ActiveMQ(5.10.0) - Building a custom security plug-in
If none of any built-in security mechanisms works for you, you can always build your own. Though the ...
- Spring(3.2.3) - Beans(3): Bean 实例的创建方式
创建一个 Bean 实例对象的方法通常有如下方式: 调用构造器创建 Bean 实例 调用静态工厂方法创建 Bean 实例 调用实例工厂方法创建 Bean 实例 使用构造器创建 Bean 实例 XML ...
- Oracle中NVARCHAR2字符集不匹配问题
Oracle中在做字符匹配时 遇到 NVARCHAR2 类型时报错,提示 字符集不匹配. 对使用 NVARCHAR2 的地方,需要对字段进行字符转换,加上 to_char(nvarchar2 字段) ...
- C#判断输入的是否是汉字
第一种方法:正则表达式 string text = "是不是汉字"; for (int i = 0; i < text.Length; i++) { if (Regex.Is ...
- JavaScript、jQuery、HTML5、Node.js实例大全-读书笔记1
技术很多,例子很多,只好慢慢学,慢慢实践!!现在学的这本书是[JavaScript实战----JavaScript.jQuery.HTML5.Node.js实例大全] 第 3 章 用 JavaScri ...
- java计时器
//用于计时,要求xx秒后开始发送短信,短信之间间隔xx秒 public static Date dateOperateBySecond(Date date,int second){ Calendar ...