Source:

PAT A1013 Battle Over Cities (25 分)

Description:

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 city​1​​-city​2​​ and city​1​​-city​3​​. Then if city​1​​is occupied by the enemy, we must have 1 highway repaired, that is the highway city​2​​-city​3​​.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 3 numbers N (<), 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 Specification:

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

Keys:

Attention:

  • 逻辑上删除结点即可,即遍历到w时直接return

Code:

 /*
Data: 2019-05-16 21:26:01
Problem: PAT_A1013#Battle Over Cities
AC: 24:40 题目大意:
给一个图,拿掉一个顶点及其边,问至少添加几条边,可以保证图的连通
输入:
第一行给出:结点数N<1e3,边数M,查询次数K
接下来M行,给出V1和V2,表示结点间存在边
接下来一行,依次给出破坏顶点序号(1~N),输出需要添加的边数 */
#include<cstdio>
#include<algorithm>
using namespace std;
const int M=1e3+,INF=1e9;
int grap[M][M],vis[M],n,m,k,w; void DFS(int u)
{
if(u==w)
return;
vis[u]=;
for(int v=; v<=n; v++)
if(vis[v]== && grap[u][v]==)
DFS(v);
} int Travel()
{
int cnt=-;
fill(vis,vis+M,);
for(int i=; i<=n; i++)
{
if(vis[i]== && i!=w)
{
DFS(i);
cnt++;
}
}
return cnt;
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE scanf("%d%d%d", &n,&m,&k);
fill(grap[],grap[]+M*M, INF);
for(int i=; i<m; i++)
{
int v1,v2;
scanf("%d%d",&v1,&v2);
grap[v1][v2]=;
grap[v2][v1]=;
}
for(int i=; i<k; i++)
{
scanf("%d", &w);
printf("%d\n", Travel());
} return ;
}

最新文章

  1. Win10切换中英输入法问题
  2. static const vs. extern const
  3. 【转载】拒绝平庸——浅谈WEB登录页面设计
  4. 使用Spring的Property文件存储测试数据 - 编写测试和调用测试数据
  5. sql server 函数
  6. java多线程Lock接口简介使用与synchronized对比 多线程下篇(三)
  7. ScriptEngine执行复杂js报数组越界
  8. c++错误
  9. 调试dump文件
  10. WCF+Autofac 实现构造函数依赖注入
  11. Unity C#图片转换二进制流、字符串互转
  12. RabbitMQ与消息总线
  13. Cocos Creator 的Hello World
  14. CHERRY G80 3000L 使用一月有感
  15. python中join()函数、list()函数补充的用法
  16. [转] CDH6 安装文章链接收集
  17. 任何用户密码都能以sysdba角色登入
  18. Zyan 一个通信框架
  19. Oracle——创建和管理表
  20. js关于密码框强弱度的提示

热门文章

  1. pt-osc改表导致数据不一致案例分析
  2. 基于zookeeper和强一致性复制实现MySQL分布式数据库集群
  3. 利用DTrace实时检测MySQl
  4. Win10中如何把语言栏缩到系统托盘
  5. Linq查询datatable的记录集合
  6. ant+jmeter中build.xml配置详解
  7. VC UI界面库大集合
  8. [RK3288][Android6.0] 调试笔记 --- 普通串口的添加 【转】
  9. C语言程序判断文件夹是否存在
  10. Python 38 sql基础