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 ...
随机推荐
- 查看Linux系统的平均负载
1.Linux系统的平均负载的概念 有时候我们会觉得系统响应很慢,但是又找不到原因,这时就要查看平均负载了,看它是否有大量的进程在排队等待.特定时间间隔内运行队列中的平均进程数可以反映系统的繁忙程度, ...
- Linux Platform Device and Driver
从 Linux 2.6 起引入了一套新的驱动管理和注册机制 :Platform_device 和 Platform_driver . Linux 中大部分的设备驱动,都可以使用这套机制 , 设备用 P ...
- java工具类(四)之实现日期任意跳转
Java实现日期任意跳转 项目开发过程中,需要进行订单提醒日期的设置,主要包括设置每月指定的日期或者指定的天数,代码如下: public static String DateOperation(Str ...
- Media Player Classic - HC 源代码分析 6:MediaInfo选项卡 (CPPageFileMediaInfo)
===================================================== Media Player Classic - HC 源代码分析系列文章列表: Media P ...
- TCP的核心系列 — SACK和DSACK的实现(六)
上篇文章中我们主要说明如何skip到一个SACK块对应的开始段,如何walk这个SACK块包含的段,而没有涉及到 如何标志一个段的记分牌.37版本把给一个段打标志的内容独立出来,这就是tcp_sack ...
- eclipse开发安卓时logcat的绿色加号不见了
那个绿色加好的作用是添加过滤器的,如果不见了说明你不小心点到了一个按键.恢复方法: 点一下清空logcat日志信息,(右边那个按键)就好了!
- LeetCode之“散列表”:Single Number
题目链接 题目要求: Given an array of integers, every element appears twice except for one. Find that single ...
- Linux进程通信学习总结
http://blog.csdn.net/xiaoweibeibei/article/details/6552498 SYSV子系统的相关概念 引用标识符:引用标识符是一个整数,表示每一个SYSV ...
- Android 实现高仿iOS桌面效果之可拖动的GridView(上)
转载请标明出处:http://blog.csdn.net/sk719887916/article/details/40074663,作者:skay 最近项目中遇到一个LIstview的拖动效 ...
- java--交通灯管理系统
转载请申明出处:http://blog.csdn.net/xmxkf/article/details/9944947 .交通灯管理系统的业务和需求分析 交通灯管理系统的项目需求: 模拟实现十字路口的交 ...