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 ...
随机推荐
- Linux parted 分区
转自http://tilt.lib.tsinghua.edu.cn/node/889 如何使用parted对齐分区以得到最优性能 Sat, 03/08/2014 - 18:02 - tlblues ...
- 【VMware虚拟化解决方案】设计和配置VMware vCenter 5.5
在这之前,我们已经对VMware ESXi 5.5进行了整个环境的设计和规划,虽然安装VMware ESXi 5.5在CPU的选型.网络的设计.共享存储的方式.虚拟化资源的需求和安装ESXI的模式等一 ...
- 关于环信的WebIm的SDK一些使用注意
先打自己几下脸,不好好看接口文档,啪啪啪. 主要先说下回调,直接先copy文档的 conn.listen({ onOpened: function ( message ) { //连接成功回调 //以 ...
- javaweb学习总结十三(dom4j方式对XML文档进行解析以及Xpath的使用)
一:dom4j方式介绍 对于xml的解析总共有三种 1:jaxp方式,是sun公司开发的,分为sax方式和dom方式 2:jdom方式,后来其中部分人员参与开发dom4j 3:dom4j方式,是现在企 ...
- [改善Java代码]在接口中不要存在实现代码
第3章 类.对象及方法 书读得多而不思考,你会觉得自己知道的很多. 书读得多而思考,你会觉得自己不懂的越来越多. —伏尔泰 在面向对象编程(Object-Oriented Programming,O ...
- POJ 1159 - Palindrome (LCS, 滚动数组)
Palindrome Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 55018 Accepted: 19024 Desc ...
- Delphi版本号对照(转)
Delphi版本号对照 VER10 :Turbo Pascal 1VER20 : Turbo Pascal 2VER30 : Turbo Pascal 3VER40 : Turbo Pasca ...
- 基于Tengine的反向代理详细配置
系统环境: SUSE Linux Enterprise Server 10 SP1 (x86_64) 注:所有软件包都放置在/data/software目录下 nginx_tcp_proxy_modu ...
- JAVA实现上传下载共享文件
1.上传下载共享文件需要用到jcifs,先下载相关JAR包(开源项目的源码,demo,文挡.API应有尽有) https://jcifs.samba.org/src/
- 实例介绍Cocos2d-x物理引擎:HelloPhysicsWorld
我们通过一个实例介绍一下,在Cocos2d-x 3.x中使用物理引擎的开发过程,熟悉这些API的使用.这个实例的运行后的场景,当场景启动后,玩家可以触摸点击屏幕,每次触摸时候,就会在触摸点生成一个新的 ...