1013. Battle Over Cities (25)(DFS遍历)
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
题目大意:给出n个城市之间有相互连接的m条道路,当删除一个城市和其连接的道路的时候,
问其他几个剩余的城市至少要添加多少个路线才能让它们重新变为连通图,其实就是求连通分支数
#include<stdio.h>
#include<string.h>
#include<stdlib.h> int graph[][];
int visited[];
int n,m,k;
void dfs( int a)
{
int i;
visited[a]=;
for( i=; i<=n; i++)
{
if( visited[i]== && graph[a][i]==)
dfs(i);
}
}
int main()
{
int cnt=,temp;
int i,j;
int a,b;
scanf("%d%d%d",&n,&m,&k);
for( i=; i<m; i++) //创建图
{
scanf("%d%d",&a,&b);
graph[a][b]=graph[b][a]=;
}
for( i=; i<k ; i++)
{
cnt=;
memset( visited,,sizeof(visited)); //每次都将visited全置0
scanf("%d",&temp);
visited[temp]=;
for( j=; j<=n; j++)
{
if( visited[j]==)
{
dfs(j);
cnt++; //连通分支数
}
}
printf("%d\n",cnt-); //需要建的高速为连通分支数减1
}
return ;
}
1013. Battle Over Cities (25)(DFS遍历)的更多相关文章
- 1013 Battle Over Cities (25分) DFS | 并查集
1013 Battle Over Cities (25分) It is vitally important to have all the cities connected by highways ...
- PAT 甲级 1013 Battle Over Cities (25 分)(图的遍历,统计强连通分量个数,bfs,一遍就ac啦)
1013 Battle Over Cities (25 分) It is vitally important to have all the cities connected by highway ...
- PAT 解题报告 1013. Battle Over Cities (25)
1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...
- PAT Advanced 1013 Battle Over Cities (25) [图的遍历,统计连通分量的个数,DFS,BFS,并查集]
题目 It is vitally important to have all the cities connected by highways in a war. If a city is occup ...
- 1013 Battle Over Cities (25分) 图的连通分量+DFS
题目 It is vitally important to have all the cities connected by highways in a war. If a city is occup ...
- 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 ...
- 1013. Battle Over Cities (25)
题目如下: It is vitally important to have all the cities connected by highways in a war. If a city is oc ...
- PAT A 1013. Battle Over Cities (25)【并查集】
https://www.patest.cn/contests/pat-a-practise/1013 思路:并查集合并 #include<set> #include<map> ...
- 1013. Battle Over Cities 用dfs计算联通分量
使用一个标记数组,标记 节点是否已访问 int 连通度=0 dfs(node i) {标记当前节点为以访问 for(每一个节点) {if(当前几点未访问 并且 从i到当前节点有直接路径) dfs(当前 ...
随机推荐
- Selenium中验证码处理
验证码的主要实现方法 1.读取方式:在服务器目录下保存制作好的图片文件.然后在web页面上让用户识别,这种方式完全可以通过URL来破解图片的地址,通过图片地址可以间接的知道图片表示的验证码是什么所以这 ...
- B. Code For 1 一个类似于线段树的东西
http://codeforces.com/contest/768/problem/B 我的做法是,观察到,只有是x % 2的情况下,才有可能出现0 其他的,都是1来的,所以开始的ans应该是R - ...
- 使用 Realm 和 Swift 创建 ToDo 应用
原文出处: HOSSAM GHAREEB 译文出处:Prayer’s blog(@EclipsePrayer) 智能手机的快速发展的同时,涌现出了很多对开发者友好的开发工具,这些工具不仅使得开发变 ...
- border 0px和border none的区别
border:0px这个表示的是边框为0像素,表示边框的像素 border:none 这个表示无边框(边框的绘制方式),边框的绘制方式有很多种:solid dashed等等
- ping localhost 返回 ::1的导致不能打开http://localhost的原因及解决
虽然可以在浏览器中正常访问http://localhost但用file,file_get_contents等函数打开http://localhost异常.用127.0.0.1也可以打开,本地hosts ...
- Java基础学习-一切皆为对象
一切皆为对象 引用 String s; //s是引用,并不是对象. String s = "asdf" //创建一个引用的同时便进行初始化
- [转]Java中实现自定义的注解处理器
Java中实现自定义的注解处理器(Annotation Processor) 置顶2016年07月25日 19:42:49 阅读数:9877 在之前的<简单实现ButterKnife的注解功能& ...
- 【转】java编程思想第20章的注解例子用到的com.sun.mirror的jar包
Java編程思想>中的注解代码中引入过这么一个包(com.sun.mirror),书上说的是在Jdk中有个tools.jar中,引入这个包就每这个问题了,但是笔者用的是JDK 1.8,把这个包i ...
- Macbook air 上打开cocoscreator出错
Error: EROFS: read-only file system, open '/Volumes/Cocos Creator/CocosCreator.app/Contents/Resource ...
- CSS预处理less基本使用
中文API http://lesscss.cn 变量 @变量名:变量值 @maincolor:#aeeeee; @acolor:#ffffff; @ht200:200px; @ht50:50p ...