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 ...
随机推荐
- UVALive Proving Equivalences (强连通分量,常规)
题意: 给一个有向图,问添加几条边可以使其强连通. 思路: tarjan算法求强连通分量,然后缩点求各个强连通分量的出入度,答案是max(入度为0的缩点个数,出度为0的缩点个数). #include ...
- c语言关键字总结
1.关键字变更历史 1999年12月16日,ISO推出了C99标准,该标准新增了5个C语言关键字: inline restrict _Bool _Complex _Imaginary(注意bool 从 ...
- [转] python程序的调试方法
qi09 原文 python程序的调试方法 本文讨论在没有方便的IDE工具可用的情况下,使用pdb调试python程序 源码例子 例如,有模拟税收计算的程序: #!/usr/bin/python de ...
- MySQL中间层 Atlas
Atlas是由 Qihoo 360, Web平台部基础架构团队开发维护的一个基于MySQL协议的数据中间层项目.它在MySQL官方推出的MySQL-Proxy 0.8.2版本的基础上,修改了大量bu ...
- JVM内存结构之一--总体介绍
Java 虚拟机在执行Java程序的时候会把它管理的内存区域划为几部分,这一节我们就来解析一下Java的内存区域. 有的人把JVM管理的内存简单地分为堆内存和栈内存,这样分未免有些太肤浅了. Java ...
- 《深入Java虚拟机学习笔记》- 第18章 finally子句
本章主要介绍字节码实现的finally子句.包括相关指令以及这些指令的使用方式.此外,本章还介绍了Java源代码中finally子句所展示的一些令人惊讶的特性,并从字节码角度对这些特征进行了解释. 1 ...
- iPhone更新失败后如何恢复数据
iPhone5最好不要用wifi下更新ios8.1,因为该固件比较大,很容易中途出问题失败,如果失败也不要怕,想要恢复数据还是有希望的. 如果不幸进入恢复模式,还没有实现备份,千万别点恢复,那就啥都没 ...
- Codeforces Round #226 (Div. 2 )
这次精神状态不怎么好,第一题的描述看得我就蛋疼...看完就速度写了~~~最终fst了%>_<%,第二题写复杂了,一直WA pretest 3,然后就紧张,导致精神更不好了,一直纠结在第二题 ...
- java日志,(commons-loging 、log4j 、slf4j 、LogBack介绍)
如果对于commons-loging .log4j .slf4j .LogBack 等都已经非常清楚了,可以忽略本文.几次解决日志冲突问题时对这几个概念的简单总结,希望对这块基础没有理解透的同学能有所 ...
- Property cannot be found on forward class object?
I have a UIView and I'm trying to set its layer properties. self.colorSwatch = [[UIView alloc] initW ...