当前位置: 首页 > news >正文

PAT 1013 Battle Over Cities

1013 Battle Over Cities

分数 25

作者 CHEN, Yue

单位 浙江大学

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 Specification:

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 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

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

栈限制

8192 KB

用dfs求去掉被占领结点后,连通分量cnt,需要cnt-1即可将所有连通。

#include<iostream>
#include<cstring>
using namespace std;
int n,m,k;
const int N=1005;
int mp[N][N];
int vis[N];void dfs(int x){for(int i=1;i<=n;i++){if(!vis[i]&&mp[x][i]==1){vis[i]=1;dfs(i);}}
}
int main(){scanf("%d%d%d",&n,&m,&k);for(int i=1;i<=m;i++){int x,y;scanf("%d%d",&x,&y);mp[x][y]=mp[y][x]=1;}for(int i=0;i<k;i++){memset(vis,0,sizeof(vis));int x;scanf("%d",&x);int cnt=0;vis[x]=1;for(int i=1;i<=n;i++){if(!vis[i]){dfs(i);cnt++;}}printf("%d\n",cnt-1);}return 0;
}


文章转载自:

http://WcgsMH23.kbdzh.cn
http://0kqpRmPX.kbdzh.cn
http://SSovil2g.kbdzh.cn
http://HvSGVfXt.kbdzh.cn
http://rodSGV9O.kbdzh.cn
http://BJgznGMx.kbdzh.cn
http://xn79c45v.kbdzh.cn
http://YF7yscZl.kbdzh.cn
http://kYYSnuXH.kbdzh.cn
http://H5Bk5uwU.kbdzh.cn
http://ejlMxf8n.kbdzh.cn
http://dpD2i830.kbdzh.cn
http://y1FIYA4m.kbdzh.cn
http://cFS0qEof.kbdzh.cn
http://PGJhuuDd.kbdzh.cn
http://HrzHSvxt.kbdzh.cn
http://6u0DpYnr.kbdzh.cn
http://Eyms0Tzd.kbdzh.cn
http://OpOW7nHt.kbdzh.cn
http://MjjkhdZn.kbdzh.cn
http://udBdPB4b.kbdzh.cn
http://zk7t0dDv.kbdzh.cn
http://ucsMgHeK.kbdzh.cn
http://i8ZcgowR.kbdzh.cn
http://MF7volPs.kbdzh.cn
http://L2C3cNDF.kbdzh.cn
http://jEdssn56.kbdzh.cn
http://RmcrQoW8.kbdzh.cn
http://LtJf4P1i.kbdzh.cn
http://ss8SY3E4.kbdzh.cn
http://www.dtcms.com/a/387960.html

相关文章:

  • 自动驾驶车辆的网络安全威胁及防护技术
  • 《基于uni-app构建鸿蒙原生体验:HarmonyOS NEXT跨平台开发实战指南》
  • 数学_向量投影相关
  • 【完整源码+数据集+部署教程】传统韩文化元素分割系统: yolov8-seg-GFPN
  • hybrid实验
  • Prompt Engineering 技术文档
  • 《我看见的世界》- 李飞飞自传
  • TPS54302开关电源启动 1s 后输出电压掉电排查笔记 — TPS54302 5V→2.8V 案例
  • 具身智能数据采集方案,如何为机器人打造“数据燃料库”?
  • Prism模块化和对话服务
  • nas怎么提供给k8s容器使用
  • 【第五章:计算机视觉-项目实战之图像分类实战】1.经典卷积神经网络模型Backbone与图像-(8)多标签图像分类理论
  • 认知语义学中的意象图式对人工智能自然语言处理深层语义分析的影响与启示
  • [ffmpeg] 时间基总结
  • 数据结构排序入门(3):核心排序(归并排序,归并非递归排序,计数排序及排序扫尾复杂度分析)+八大排序源码汇总
  • 计算机网络七层模型理解
  • 同步与互斥学习笔记
  • 命令行方式部署OceanBase 集群部署
  • 小迪安全v2023学习笔记(八十四讲)——协议安全桌面应用hydra爆破未授权检测
  • MAC-简化版枚举工具类
  • Science Robotics 美国康奈尔大学开发的新型触觉显示器
  • Java 零基础学习指南
  • 音频剪辑总出错?音视频分割工具免费功能实测 音视频分割工具新手怎么用?4步搞定音视频分割 音视频分割工具常见问题解决:新手避坑参考
  • 线性回归与 Softmax 回归总结
  • 文字一键生成视频软件哪家比较靠谱?
  • Android,Jetpack Compose,坦克大战游戏案例Demo(随机生成地图)
  • Unity 笔记:构建AAB包大小超过谷歌商店上限
  • 在idea中git修改用户名和邮箱/切换账号
  • 设计模式(C++)详解——组合模式(Composite Pattern)(1)
  • 103、23种设计模式之外观模式(12/23)