PAT 1013
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
层次遍历,查找图中有多少个联通的子图。注意数组mTree的使用,当mTree中的元素为-1时,表示该节点为树根,当mTree中的元素为0时,表示改点被占有,其余大于0的值表示该点的父节点。
代码
1 #include <stdio.h>
2 #include <string.h>
3
4 int find_num_of_tree(int,int);
5 int map[][];
6 int mTree[];
7 int flag[];
8 int queue[];
9 int main()
{
int N,M,K;
int checked;
int i;
int s,e;
while(scanf("%d%d%d",&N,&M,&K) != EOF){
memset(map,,sizeof(map));
for(i=;i<M;++i){
scanf("%d%d",&s,&e);
map[s][e] = map[e][s] = ;
}
for(i=;i<K;++i){
scanf("%d",&checked);
printf("%d\n",find_num_of_tree(N,checked)-);
}
}
return ;
}
int find_num_of_tree(int n,int checked)
{
if(n < )
return ;
int base,top;
int i,j;
memset(flag,,sizeof(flag));
flag[checked] = ;
for(i=;i<=n;++i){
mTree[i] = -;
}
mTree[checked] = ;
for(i=;i<=n;++i){
if(flag[i])
continue;
queue[] = i;
base = ;
top = ;
while(base < top){
int x = queue[base++];
flag[x] = ;
for(j=;j<=n;++j){
if(!flag[j] && map[x][j]){
queue[top++] = j;
mTree[j] = x;
}
}
}
}
int num = ;
for(i=;i<=n;++i){
if(mTree[i] == -)
++num;
}
return num;
}
PAT 1013的更多相关文章
- PAT 1013 Battle Over Cities
1013 Battle Over Cities (25 分) It is vitally important to have all the cities connected by highway ...
- PAT 1013 数素数 (20)(代码)
1013 数素数 (20)(20 分) 令P~i~表示第i个素数.现任给两个正整数M <= N <= 10^4^,请输出P~M~到P~N~的所有素数. 输入格式: 输入在一行中给出M和N, ...
- PAT——1013. 数素数
令Pi表示第i个素数.现任给两个正整数M <= N <= 104,请输出PM到PN的所有素数. 输入格式: 输入在一行中给出M和N,其间以空格分隔. 输出格式: 输出从PM到PN的所有素数 ...
- PAT 1013 Battle Over Cities(并查集)
1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...
- pat 1013 Battle Over Cities(25 分) (并查集)
1013 Battle Over Cities(25 分) It is vitally important to have all the cities connected by highways i ...
- PAT 1013 Battle Over Cities (dfs求连通分量)
It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...
- PAT 1013. 数素数 (20)
令Pi表示第i个素数.现任给两个正整数M <= N <= 104,请输出PM到PN的所有素数. 输入格式: 输入在一行中给出M和N,其间以空格分隔. 输出格式: 输出从PM到PN的所有素数 ...
- PAT 1013 数素数
https://pintia.cn/problem-sets/994805260223102976/problems/994805309963354112 令P~i~表示第i个素数.现任给两个正整数M ...
- PAT 1013 Battle Over Cities DFS深搜
It is vitally important to have all the cities connected by highways in a war. If a city is occupied ...
随机推荐
- angularJS $resource与后台restapi的对应关系
REST(表征性状态传输,Representational State Transfer)是Roy Fielding博士在2000年他的博士论文中提出来的一种软件架构风格.RESTful风格的设计不仅 ...
- apache开源项目--ZooKeeper
ZooKeeper是Hadoop的正式子项目,它是一个针对大型分布式系统的可靠协调系统,提供的功能包括:配置维护.名字服务.分布式同步.组服务等.ZooKeeper的目标就是封装好复杂易出错的关键服务 ...
- 剑指Offer:打印从1到最大的n位数
题目:输入数值n,按顺序打印从1到最大的n位数,例如输入n=3,则从1,2,3,一直打印到999 陷阱:若使用循环遍历 1- 999...9 并依次输出,当位数n过大时,无论将其存入int或long或 ...
- 【转】Cygwin的包管理器:apt-cyg
原文网址:http://zengrong.net/post/1792.htm Cygwin的包管理工具setup.exe实在是难用的让人蛋碎.于是就有了这样一个apt-cyg,可以提供类似于 apt- ...
- [selenium webdriver Java]检查元素是否存在
Selenium WebDriver没有实现Selenium RC的isElementPresent()方法来检查页面上的元素是否存在. 在WebDriver中封装一个类似的方法,如下: public ...
- 《C Primer Plus 第五版》读书笔记
CH1-2:概述 链接器:链接库代码.启动代码(start-up code) CH3-5:数据.字符串.运算符 1 数据类型存储方式:整数类型.浮点数类型 2 浮点数存储:小数部分+指数部分 3 in ...
- 使用 cloc 统计代码行数
可能大家都知道用 `wc -l` 命令进行代码行数统计,但是它会将代码中的注释.空行所占用的文本行都统计在内.如果想查看一个 tar 包或一个项目目录中“实际”的代码行数并且不愿意自己去写一个脚本来做 ...
- 面向对象基础3(class0523)
怎么实现多态2-接口 接口是定义一种能力,规定子类能干什么和抽象类有些相似,解决类的单根继承.接口可以实现多继承 案例 鸟-麻雀sparrow,鸵鸟ostrich,企鹅penguin,鹦鹉parrot ...
- Leetcode OJ : Implement strStr() [ Boyer–Moore string search algorithm ] python solution
class Solution { public: int strStr(char *haystack, char *needle) { , skip[]; char *str = haystack, ...
- lcov收集覆盖率
1.gcov 1.1 什么是gcov 首先我们要了解什么是gcov,gcov伴随gcc 发布.gcc编译加入-fprofile-arcs -ftest-coverage 参数生成二进制程序,执行测试用 ...