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 题意: 假设二叉树中的所有键都是不同的正整数.一个唯一的二叉树可以通过给定的一对后序和顺序遍历序列来确定.这是一个简单的标准程序,可以按 ...
随机推荐
- PHP开源系统学习之fluxbb_1
最近一直忙于做项目,虽说做了点新东西.感觉自己进步不是很大,总体水平还是跟半年前差不多,想到的东西跟以前差不多,写出来的东西也跟以前差不多.只是现在做的东西多些,比以前敢做了. 近期准备利用点时间,读 ...
- vue中通过cross-env插件配置三种环境(开发,测试,生产)打包,不用切换api
1. 话不多说,第一步就是安装必要的插件 npm install cross-env --save 2.修改config里面的参数,这里只展示一个test,其他类似 3.修改package.json ...
- Codeforces Round #527 (Div. 3)D2(栈,思维)
#include<bits/stdc++.h>using namespace std;int a[200007];stack<int>s;int main(){ int ...
- C#网络编程学习(6)---序列化和反序列化
1.什么是序列化和反序列化 当客户端和服务器进行远程连接时,互相可以发送各种类型的数据.但都要先把这些对象转换为字节序列,才能在网络上进行传输. 序列化:就是发送方 把对象转换为字节序列的过程. 反序 ...
- jmeter小问题解决方案合集
问题1.在http请求,post的body中输入中文,显示乱码,怎么解决? 在jmeter的bin目录下,找到这个文件jmeter.properties,把jsyntaxtextarea.font.f ...
- c# log4Net 详细说明
转载 http://www.cnblogs.com/kissazi2/p/3392605.html
- Mac开启自带的Apache服务器
OSX版本10.13.6 1.开启 sudo apachectl start 2.关闭 sudo apachectl stop 3.重启 sudo apachectl restart 默认的Apach ...
- 大数据“重磅炸弹”——实时计算框架 Flink
Flink 学习 项目地址:https://github.com/zhisheng17/flink-learning/ 博客:http://www.54tianzhisheng.cn/tags/Fli ...
- 基于nginx的FastCGI的缓存配置
废话不多说了, 直接上配置, 其实 fastcgi_cache 和 proxy_cache 的配置基本一样: # !缓存文件存放目录 # levels 缓存层次 # keys_zone 缓存空间名和共 ...
- Toad for Oracle针对于Oracle数据库的可视化管理工具使用
Toad for Oracle安装包下载地址:http://pan.baidu.com/s/1mgBOLZU 在Oracle应用程序的开发过程中,访问数据库对象和编写SQL程序是一件乏味且耗费时间的工 ...