pat甲级1013
1013 Battle Over Cities (25)(25 分)
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
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需要修的路个数就是连通分量的个数减一。
#include <iostream>
#include <vector>
using namespace std; int N, M, K;
int G[][]; int connectCnt(int city);
void dfs(int s, bool marked[]); int main()
{
int i, j, v, w, c;
cin >> N >> M >> K;
for (i = ; i < M; i++)
{
cin >> v >> w;
G[v][w] = ;
G[w][v] = ;
}
for (i = ; i < K; i++)
{
cin >> c;
vector<int> vec;
for (j = ; j <= N; j++)
{
if (G[c][j] == )
{
G[c][j] = ;
G[j][c] = ;
vec.push_back(j);
}
}
cout << connectCnt(c) << endl;
for (int j : vec)
{
G[c][j] = ;
G[j][c] = ;
}
}
return ;
} int connectCnt(int city)
{
int v, cnt = ;
bool marked[] = {};
for (v = ; v <= N; v++)
{
if (!marked[v] && v != city)
{
dfs(v, marked);
cnt++;
}
}
return cnt - ;
} void dfs(int s, bool marked[])
{
marked[s] = true;
for (int v = ; v <= N; v++)
{
if (!marked[v] && G[s][v])
dfs(v, marked);
}
}
但是查看别人的博客发现不需要每次把和被占领的城市相连的道路标记为0再标记为1,只需要把marked数组被占领的城市标记为1即可。。
#include <iostream>
using namespace std; int N, M, K;
int G[][]; int connectCnt(int city);
void dfs(int s, bool marked[]); int main()
{
int i, v, w, c;
scanf("%d%d%d", &N, &M, &K);
for (i = ; i < M; i++)
{
scanf("%d%d", &v, &w);
G[v][w] = ;
G[w][v] = ;
}
for (i = ; i < K; i++)
{
scanf("%d", &c);
printf("%d\n", connectCnt(c));
}
return ;
} int connectCnt(int city)
{
int v, cnt = ;
bool marked[] = {};
marked[city] = true;
for (v = ; v <= N; v++)
{
if (!marked[v])
{
dfs(v, marked);
cnt++;
}
}
return cnt - ;
} void dfs(int s, bool marked[])
{
marked[s] = true;
for (int v = ; v <= N; v++)
{
if (!marked[v] && G[s][v])
dfs(v, marked);
}
}
pat甲级1013的更多相关文章
- PAT甲级1013. Battle Over Cities
PAT甲级1013. Battle Over Cities 题意: 将所有城市连接起来的公路在战争中是非常重要的.如果一个城市被敌人占领,所有从这个城市的高速公路都是关闭的.我们必须立即知道,如果我们 ...
- 图论 - PAT甲级 1013 Battle Over Cities C++
PAT甲级 1013 Battle Over Cities C++ It is vitally important to have all the cities connected by highwa ...
- 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 ...
- PAT甲级1013题解——并查集+路径压缩
题目分析: 本题初步浏览题目就知道是并查集的模板题,数据输入范围N为1~1000,则M的范围为0~1000^2,通过结构体记录每一对连线的关系,p[]数组记录每个节点的跟,对于k次查询,每次都要重新维 ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT甲级考前整理(2019年3月备考)之一
转载请注明出处:https://www.cnblogs.com/jlyg/p/7525244.html 终于在考前,刷完PAT甲级131道题目,不容易!!!每天沉迷在刷题之中而不能超脱,也是一种 ...
- 【转载】【PAT】PAT甲级题型分类整理
最短路径 Emergency (25)-PAT甲级真题(Dijkstra算法) Public Bike Management (30)-PAT甲级真题(Dijkstra + DFS) Travel P ...
- PAT甲级1131. Subway Map
PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...
- PAT甲级1127. ZigZagging on a Tree
PAT甲级1127. ZigZagging on a Tree 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...
随机推荐
- 2、Jquery_事件
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- 图像的读取,显示与保存(基于skimage模块)
一 skiamge模块 skimage包的全称是scikit-image SciKit (toolkit for SciPy) ,它对scipy.ndimage进行了扩展,提供了更多的图片处理功能.它 ...
- webpack@3.6.0(3)-- 优化
本篇内容 babel配置 打包调试 第三方资源引入 静态资源的集中输出 babel配置 cnpm i -D babel-core babel-loader babel-preset-es2015 // ...
- dbms_xplan的display_cursor查看执行计划
准备工作: SQL> conn sys/root as sysdba Connected. SQL> grant select on v_$sql_plan to scott; Grant ...
- thinkphp5命令行访问
入口文件后加一个空格就行了 1,首先cd到站点目录public下,我的入口文件是默认的index.php,然后执行以下命令, 2,php要加入环境变量 访问index模块下的index控制器下的tes ...
- 练习三十八:矩阵for循环应用
习题如下: 求一个3*3矩阵对角线元素之和 利用for循环控制输出二维数组,再将a[i][j]累加后输出 a = [] sum1 = 0.0 for i in range(3): a.append([ ...
- 原svn账户清除,及使用新用户名密码操作方法
原svn账户清除,及使用新用户名密码操作方法 第一步:先清除原svn账户信息,如图示,电脑桌面右击“ToroiseSVN--Settings”. 在Settings中,选择Saved Data中的Cl ...
- Appium + Python自动化3 - 输入中文
在做app自动化过程中会踩很多坑,咱们都是用中文的app,所以首先要解决中文输入的问题!本篇通过屏蔽软键盘,绕过手机的软键盘方法,解决中文输入问题. 一.定位搜索 1.打开淘宝点击搜索按钮,进入搜索页 ...
- (转)Linux 文件和目录的属性
linux 文件属性与权限 原文:https://www.cnblogs.com/kzloser/articles/2673790.html https://www.cnblogs.com/danh/ ...
- vim配置成c++IDE
mv ~/.vimrc ~/.vimrcbak mv ~/.vim ~/.vimbak git clone https://github.com/handy1989/vim.git mv vim/.v ...