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 ...
随机推荐
- byte[] bytes和string转换
public static string ToHexString ( byte[] bytes ) // 0xae00cf => "AE00CF " { ...
- CentOS/Linux安装VNCserver
VNC全称是Virtual Network Computing,属于远程控制类软件.其优点是支持跨操作系统的远程图形化控制.在日常工作中,服务器常常是存在机房,不可能每次需要图形界面操作就跑到机房,因 ...
- ArcGIS Server 10.2 实战(五)spatial etl tool 格式转换服务
上不同的地图服务平台对地图文件格式的要求多种多样,arcgis使用的文件很难应用于其他平台上,因此需要有格式转换的服务来克服这种使用不同平台带来的麻烦,下面以TIFF格式转GEOTIFF格式为例. 首 ...
- Windows Azure 微软公有云体验(三) IIS中文编码解决方案
Windows Azure 微软公有云已经登陆中国有一段时间了,现在是处于试用阶段,Windows Azure的使用将会给管理信息系统的开发.运行.维护带来什么样的新体验呢? Windows Azur ...
- Wince 设备环境和画笔应用
本文主要讲到的是画笔应用,在Wince -06环境下,画笔应用很广泛,很有技巧,这里笔者要着重介绍. 设备环境可以用一下图表示,主要是让大家大致了解Wince -06的设备环境,下面在图形舍虚设计中会 ...
- 2012蓝桥杯C组本科决赛答案
题目: 脱氧核糖核酸即常说的DNA,是一类带有遗传信息的生物大分子.它由4种主要的脱氧核苷酸(dAMP.dGMP.dCMT和dTMP)通过磷酸二酯键连接而成.这4种核苷酸可以分别记为:A.G.C.T. ...
- Linux free字段解析
下面是free的运行结果,一共有4行.为了方便说明,我加上了列号.这样可以把free的输出看成一个二维数组FO(Free Output).例如: FO[2][1] = 24677460 FO[3][2 ...
- Java Concurrency - 浅析 CountDownLatch 的用法
The Java concurrency API provides a class that allows one or more threads to wait until a set of ope ...
- Tomcat - DBCP 配置
1. Database configuration Create a new test user, a new database and a single test table. Your MySQL ...
- iOS开发那些事--性能优化–内存泄露问题的解决(转)
内存泄漏问题的解决 内存泄漏(Memory Leaks)是当一个对象或变量在使用完成后没有释放掉,这个对象一直占有着这块内存,直到应用停止.如果这种对象过多内存就会耗尽,其它的应用就无法运行.这个问题 ...