PAT1013: Battle Over Cities
1013. Battle Over Cities (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 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
思路
这道题的本质问题其实就是求去掉某一点后的图有几个连通子图。那么需要重修的道路就是——连通子图数-1(保证余下节点全连通)。所以只要在去掉一个点后dfs下这个图确定连通子图个数就行。那么:
1.用一个数组表示点是否被遍历。
2.对于一个被入侵的城市city,只需要在dfs之前将这个城市标记为已遍历就行,这样dfs相当于只遍历去掉了这个点的图,即模拟了city从图中被去掉的情况。
代码
#include<iostream>
#include<vector>
using namespace std; vector<vector<int>> graph(,vector<int>(,-)); void dfs(const int city,const int num,vector<bool>& visits)
{
visits[city] = true;
for(int i = ;i <= num;i++)
{
if(city == i)
continue;
if(graph[city][i] == && !visits[i])
{
dfs(i,num,visits);
}
}
} int main()
{
int N,M,K;
while(cin >> N >> M >> K)
{
while(M--)
{
int a,b;
cin >> a >> b;
graph[a][b] = graph[b][a] = ;
} while(K--)
{
int countRoad = ;
vector<bool> visits(N + ,false);
int city;
cin >> city;
visits[city] = true;
for(int i = ;i <= N;i++)
{
if(!visits[i])
{
countRoad++;
dfs(i,N,visits);
}
}
cout << countRoad - << endl;
}
}
}
PAT1013: Battle Over Cities的更多相关文章
- pat1013. Battle Over Cities (25)
1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...
- 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 ...
- PAT1013. Battle Over Cities(邻接矩阵、邻接表分别dfs)
//采用不同的图存储结构结构邻接矩阵.邻接表分别dfs,我想我是寂寞了吧,应该试试并查集,看见可以用并查集的就用dfs,bfs代替......怕了并查集了 //邻接矩阵dfs #include< ...
- PAT---1013. Battle Over Cities (25)
这道题目的意思是:在战争时代,如果一个城市被敌人占领了,那么和该城市相连的道路都必须关闭,我们必须把剩下的城市(即不包括被敌人占领的城市)连接起来. 举个例子,我们有3个城市,C1,C2,C3,C1和 ...
- 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-Top1001. Battle Over Cities - Hard Version (35)
在敌人占领之前由城市和公路构成的图是连通图.在敌人占领某个城市之后所有通往这个城市的公路就会被破坏,接下来可能需要修复一些其他被毁坏的公路使得剩下的城市能够互通.修复的代价越大,意味着这个城市越重要. ...
- PAT 1013 Battle Over Cities
1013 Battle Over Cities (25 分) It is vitally important to have all the cities connected by highway ...
- PAT Battle Over Cities [未作]
1013 Battle Over Cities (25)(25 分) It is vitally important to have all the cities connected by highw ...
- PTA (Advanced Level) 1013 Battle Over Cities
Battle Over Cities It is vitally important to have all the cities connected by highways in a war. If ...
随机推荐
- 为CCB中的Sprite子类化CCSprite的一个问题
这时一个特定的错误发生在运行app或者loading场景的时候: reason: '[<NameOfClass 0x7a043520> setValue:forUndefinedKey:] ...
- How Tomcat Works 读书笔记 八 载入器 上
Java的类载入器 详细资料见 http://blog.csdn.net/dlf123321/article/details/39957175 http://blog.csdn.net/dlf1233 ...
- Linux文件系统的简单操作 - df, du, ln
现在我们知道磁盘的整体数据是在 superblock 区块中,但是每个各别文件的容量则在 inode 当中记载的. 那在文字接口底下该如何叫出这几个数据呢?底下就让我们来谈一谈这两个命令: df:列出 ...
- Cocoa公历和中国农历直接的转换
看过某书上面的做法是先生成一个公历的calendar,使用的是: NSCalendar *cal = [NSCalendar currentCalendar]; 然后用它生成一个NSDateCompo ...
- 关于SharePoint2007简单随感
首先,还是要感谢我毕业以后的这第一份正式工作,当然现在也依然在做,带我走进了SharePoint的世界,很奇妙也许是有缘吧,自己不是个努力的人,从面试的时候对Moss这个东西闻所未闻,到现在一知半解, ...
- linux下64位汇编的系统调用(4)
经过上一篇的铺垫貌似可以很轻松的用汇编写出mmap的代码来,可仔细一看,还是有不少问题需要解决: 1.系统调用mmap如果出错并不直接返回MAP_FAILED(-1),而是一个"类似&quo ...
- 解析Json字符串的三种方法
在很多时候,我们的需要将类似 json 格式的字符串数据转为json, 下面将介绍日常中使用的三种解析json字符串的方法 1.首先,我们先看一下什么是 json 格式字符串数据,很简单,就是 jso ...
- 详谈linux中压缩
1.压 缩 的 用 途 和 技 术 1.1 为什么需要压缩: ①你是否有过文件档案太大,导致无法以正常的email方式发送出去(很多email都有容量大约25MB每封信的限制啊!)? ②你是否有过要备 ...
- 并发编程(五):CAS
在atomic包中,大多数类都是借助unsafe类来实现的,如以下代码 public static AtomicInteger count = new AtomicInteger(0); privat ...
- nginx flv点播服务器搭建
首先选用Nginx+Nginx-rtmp-module作为点播服务器,安装文章:https://www.atlantic.NET/community/howto/install-rtmp-ubuntu ...