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 ...
随机推荐
- JMX笔记(一)
上篇 JMX初体验 使用HtmlAdaptorServer提供的界面实现了调用MBean,除此之外,还可以使用rmi方式连接调用MBeanServer 要连接,自然要有url:service:jmx: ...
- 关于Windows下mysql忘记root密码的解决方法
原文链接: http://www.cnblogs.com/andy_tigger/archive/2012/04/12/2443652.html 1. 首先检查mysql服务是否启动,若已启动则先将其 ...
- 关于Eclipse的工作空间设置默认个数和配置
&
- scikit-learn点滴
scikit-learn点滴 scikit-learn是非常漂亮的一个机器学习库,在某些时候,使用这些库能够大量的节省你的时间,至少,我们用Python,应该是很难写出速度快如斯的代码的. sciki ...
- Eclipse去除jquery引入错误
之前在写Java项目时,总是出现引入jquery报错,虽然对其方法的应用没有什么影响,但是感觉难受,经过百度得到解决的方法: 第一步:去除eclipse的JS验证:将windows->prefe ...
- 初识CSS3之媒体查询(2015年05月31日)
一.什么是媒体查询 媒体查询是面向不同设备提供不同样式的一种实现方式,它可以为每种类型的用户提供最佳的体验,也是响应式设计的实现方式. 现今每天都有更多的手机和平板电脑问市.消费者能够拥有可想象到的各 ...
- Linux 命令 - ftp: 网络文件传输工具
命令格式 ftp [-pinegvd] [host] 命令参数 -A 传输文件模式为主动模式. -p 传输文件模式为被动模式. -i 关闭交互模式. -n 关闭自动登录功能. -e 不记录历史命令. ...
- Unity3d发布错误:could not allocate memery:system out of memery!
可能出现的原因: 1.项目太大了2.项目坏了3.资源坏了4.单个资源定点数超了e. 解决办法:删除了一些模型.是模型太大,面数.顶点数太多的原因. Unity3d里查看模型的顶点数 展开fbx ...
- PC电脑运行Android模拟器总是弹出“视频源”窗体的原因和解决方案
原因: PC电脑运行Android模拟器时弹出“视频源”窗体,会让你"选择视频设备",如:HP TruevIsion HD,而这个视频设备就是你电脑中的内置摄像头,这个内置摄像头在 ...
- 企业SAAS的春天,将以手机应用的形式,即将到来
派尔科技吴春福 *本文是派尔为什么要投身企业移动应用的内部分享文章: *我没有仔细核查资料,仅代表个人看法,思路也是在整理过程,逻辑未必很完整,看官将就着看. 企业SAAS,概念起源是N年前,先行者也 ...