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 ...
随机推荐
- Erlang入门(三)——分布式编程
明天要回家一个星期了,好好休息下.今天找到别人翻译的Erlang编程手册,值的好好读一遍. 所谓分布式的Erlang应用是运行在一系列Erlang节点组成的网络之上.这样的系统的性质与单一节点上 ...
- (二)学习MVC之实现用户注册功能
学习地址:http://www.cnblogs.com/mzwhj/archive/2012/10/22/2720089.html 本文和学习地址不一样的地方是我自己添加了一些简单的注释和理解. 1. ...
- Ext入门学习系列(四)面板控件
第四章 使用面板 上节学习了Ext复杂对话框,更进一步了解了Ext的运行机制.本章重点来了解Ext所有控件的基础——面板控件. 一.Ext的面板是什么? 同样先来看看几个效果: 基本面板,点击右上角小 ...
- Ext入门学习系列(五)表格控件(2)
上节学习了Ext中表格控件,从创建,到定义数据源.绑定显示,大体明白了一个基本的表格控件是怎么实现的.而我们用表格控件多用于从各种数据源接收数据并显示出来,并不是写死的.本章我们就不同数据源的不同实现 ...
- java设计模式—Adapter模式
1.核心意图: 将一个类的接口转换成客户希望的另外一个接口,从而使得原本由于接口不兼容而不能一起工作的类可以一起工作. 该模式的目标是通过一个代理(这里是Adapter),在原来的类(Adap ...
- VS2008编写MFC程序--使用opencv2.4()
开始记录VS2008环境下学习OPENCV2.4 头文件: #pragma once #include "CvvImage.h" #include "opencv/cv. ...
- Objective-C之消息机制
话说2014年4月编程语言排行榜中Objective-C的使用比又增加了,看来IOS和MAX OS的开发者是真给力呀. 不过个人感觉用不了多久,IOS和Android的开发者收入就不会有那么大的差异了 ...
- codeforce 600C - Make Palindrome
练习string 最小变换次数下,且字典序最小输出回文串. #include <cstdio> #include <cstring> #include <cmath> ...
- C/C++编译预处理命令详解【转】
1. 预处理程序 按照ANSI标准的定义,预处理程序应该处理以下指令: #if #ifdef #ifndef #else #elif #endif #define #undef #lin ...
- 本地已有SVN项目导入到eclipse中
有时候在本地上checkout了项目,在eclipse中不希望重新checkout一次,可以如下操作: 1.在project上右键-> team -> share project -> ...