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 ...
随机推荐
- TortoiseGit连接github不用每次输入用户名和密码的方法
每次git clone 和push 都要输入用户名和密码.虽然安全,但在本机上每次都输有些麻烦,如何记住用户名和密码呢? 当你配置好git后,在C:\Documents and Settings\Ad ...
- delphi获得当前鼠标坐标
简单的说就是取鼠标所在位置对应的窗口句柄? procedure TForm1.Timer1Timer(Sender: TObject);vara:TPoint; //用来存放坐标hw:HWND; // ...
- java.lang.NoClassDefFoundError: javax/transaction/Synchronization (jUnit测试报错)
测试hibernate 报错原因项目缺少包 在 hibernate 解压目录下找到 jta.jar 文件 往项目中添加该 jar 包,即可解决 添加方法:[右击项目]--> ...
- <转>SpringMVC与Struts2 比较总结
原链接:http://blog.csdn.net/chenleixing/article/details/44570681 个人整理: 1.级别不同:SpringMVC :方法 Struts是 类级 ...
- windows串口通信的一个活动图
1,打开串口的活动图: 2,关闭串口的活动图:
- 无法在Web服务器上启动调试,与Web服务器通信时出现身份验证错误
问题描述: 我使用的是修改hosts,模拟真实网址来进行调试的.具体是这样的:我修改hosts文件,把某个域名,如www.163.com映射为127.0.0.1,然后在IIS信息管理器中,创建一个网站 ...
- ubuntu 运行android sdk 下的工具adb报bash: ./adb: No such file or directory
运行adb出现这种错误: bash: ./adb: No such file or directory 但adb确实存在. 可能1:你用的是64位的Linux,没装32位运行时库,安装 $ sud ...
- bigData Ecosystem Unscramble
主题>>: cloudEra hadoop Ecosystem sim Unscramble; ruiy哥个人理解总结,其实我无非也还是站在那"砖家/叫兽"的肩上瞎扯扯 ...
- 纯CSS完美实现垂直水平居中的6种方式
前言 由于HTML语言的定位问题,在网页中实现居中也不是如word中那么简单,尤其在内容样式多变,内容宽高不定的情况下,要实现合理的居中也是颇考验工程师经验的.网上讲居中的文章很多,但是都不太完整,所 ...
- 大公司最喜欢问的Java集合类面试题
看了一些所谓大公司的JAVA面试问题,发现对于JAVA集合类的使用都比较看重似的,而自己在这方面还真的是所真甚少,抽空也学习学习吧. java.util包中包含了一系列重要的集合类,而对于集合类,主要 ...