PAT (Advanced Level) Practise 1013. Battle Over Cities (25)
2016-06-25 09:58
621 查看
1013. Battle Over Cities (25)
时间限制400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
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
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
用一个标记数组除去检查点后,用DFS判断联通分支的数目cou,要保持联通,只要再建cou - 1条路即可
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 1000;
int N, M, K, del, cou;
vector<int>G[maxn + 10];
bool vis[maxn + 10];
void dfs(int x) {
vis[x] = true;
for (vector<int>::iterator i = G[x].begin(); i != G[x].end(); i++) {
if (!vis[*i]) {
dfs(*i);
}
}
}
int main()
{
for (int i = 0; i <= maxn + 5; i++) {
G[i].clear();
}
cin >> N >> M >> K;
while (M--) {
int u, v;
scanf("%d%d", &u, &v);
G[u].push_back(v);
G[v].push_back(u);
}
while (K--) {
memset(vis, false, sizeof(vis));
scanf("%d", &del);
vis[del] = true;
cou = 0;
for (int i = 1; i <= N; i++) {
if (!vis[i]) { //debug 前驱也要加判断
vis[i] = true;
cou++; //debug 计算标记前驱的个数
for (vector<int>::iterator j = G[i].begin(); j != G[i].end(); j++) {
if (!vis[*j]) {
//cou++; //debug: move to line 44
dfs(*j);
}
}
}
}
printf("%d\n", cou - 1);
}
return 0;
}
相关文章推荐
- Win2003利用dfs(分布式文件系统)在负载均衡下的文件同步配置方案
- win2003分布式文件系统(dfs)配置方法[图文详解]
- win2003分布式文件系统及其部署 图文教程
- libdvbpsi源码分析(四)PAT表解析/重建
- Hadoop2.6+jdk8的安装部署(1)——使用jar包安装部署【详细】
- PAT配置
- 什么是端口复用动态地址转换(PAT) 介绍配置实例
- MikroTik layer7-protocol
- Hadoop FS Shell
- PAT是如何工作的
- DFS使用方法总结
- FastDFS注意事项
- 无忧技术带您预览DFS(分布式文件系统)管理控制台
- PAT 乙级题:1002. 写出这个数 (20)
- C 语言实现迷宫 DFS算法
- PAT (Advanced Level) Practise 1001-1010
- 数据结构学习与实验指导(一)
- 一幅图弄清DFT与DTFT,DFS的关系
- HDFS---Namenode
- HDFS ---- Services startup