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 ...
随机推荐
- 【约瑟夫环变形】UVa 1394 - And Then There Was One
首先看到这题脑子里立刻跳出链表..后来继续看如家的分析说,链表法时间复杂度为O(n*k),肯定会TLE,自己才意识到果然自个儿又头脑简单了 T^T. 看如家的分析没怎么看懂,后来发现这篇自己理解起来更 ...
- 最长非降/下降子序列问题(DP)(待续...)
注意:抽象成以下描述即为最长非降/下降子序列问题(一维状态) 问题描述:在一个无序的序列a1,a2,a3,a4…an里,找到一个最长的序列满足:(不要求连续) ai<=aj<=ak…< ...
- HTML+CSS 实现水流流动效果
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- Linux 命令 - mkdir: 创建目录
命令格式 mkdir [OPTION]... DIRECTORY... 命令参数 -m, --mode=MODE 设置文件的模式,类似于 chmod 命令. -p, --parents 需要时创建指定 ...
- 在web界面调用水晶报表导出文件时莫名错误
原因是水晶报表未破解版有字段限制,不能超过90(具体个数没仔细测)个字段. 建议那些select *的朋友检查一下字段个数
- 我是如何自学Android,资料分享
我是如何自学Android,资料分享(2015 版) 已经完成,我的建议是先把这一篇看完,再看2015 版的.关于我在学习中开发的项目代码,已经发布在: 爱开发-源码搜索,集成了上万个App源码 ...
- BUG修改纪录
刚进入现在的公司,接手了一个遗留的项目,BUG频出,最近一个星期都在加班改BUG,身心疲惫,为了 现在,将来不会再被相同BUG困扰,特来纪录一下. 1.数据库设计字段时,对于int等类型最好设置默认值 ...
- Linux下c基于openssl生成MD5的函数
Linux下openssl提供了一系列哈希及加密的函数,如果调用openssl提供的MD5函数生成任意字符串的MD5呢?下面提供了一段代码实现Linux下c字符串生成md5的函数. 具体代码: 1 2 ...
- 设置一个View的背景图片的集中方法
控制器view的背景图片的方法, 四种: 1.直接在控制器view上添加一个imageView大小设置的和view一样 UIImageView *beijingimage = [UIImageView ...
- windows搭建virtualbox虚拟机安装的android环境
1.首先安装virtualbox,从官网下载,安装完成之后在本地连接里面有virtualbox虚拟的网卡,可能会影响网络连接,一般禁用 2.下载android的镜像,完整名称是:android-x86 ...