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(当前 ...
随机推荐
- Java异常概念
- Backbone学习记录(3)
创建视图 同前面创建模型和集合的方式一样,Backbone.View.extend()即可创建视图 var UserView=Backbone.View.extend(); var view1=new ...
- P2345 奶牛集会andP2657 低头一族
做法是一样的 题目背景 MooFest, Open 题目描述 约翰的N 头奶牛每年都会参加“哞哞大会”.哞哞大会是奶牛界的盛事.集会上的活动很 多,比如堆干草,跨栅栏,摸牛仔的屁股等等.它们参加活动时 ...
- P2676 超级书架
题目描述 Farmer John最近为奶牛们的图书馆添置了一个巨大的书架,尽管它是如此的大,但它还是几乎瞬间就被各种各样的书塞满了.现在,只有书架的顶上还留有一点空间. 所有N(1 <= N & ...
- Android设计模式——MVP
一.什么是MVP MVP:全称 Model-View-Presenter. MVP框架由3部分组成:View层负责显示,Presenter层负责逻辑处理,Model层提供数据. View:负责绘制UI ...
- mysql索引原理及创建与查询
索引介绍 一:为什么要有索引 索引是用来优化查询效率(速度)的 没有索引的话,对于大数据的表,就只能每次都遍历一遍,数据量越大,耗时越多有索引的话,可以提升好几个数量级的速度 一般的应用系统,读写比例 ...
- js实现跨域的方法
由于同源策略的限制,XMLHttpRequest只允许请求当前源(包含域名.协议.端口)的资源. json与jsonp的区别: JSON是一种数据交换格式,而JSONP是一种依靠开发人员创造出的 ...
- Android掌中游斗地主游戏源码完整版
源码大放送-掌中游斗地主(完整版),集合了单机斗地主.网络斗地主.癞子斗地主等,有史以来最有参考价值的源码,虽然运行慢了一点但是功能正常,用的是纯java写的. 项目详细说明:http://andro ...
- Android(java)学习笔记165:开发一个多界面的应用程序之不同界面间互相传递数据(短信助手案例的优化:请求码和结果码)
1.开启界面获取返回值 (1)采用一种特殊的方式开启Activity: startActivityForResult(intent , 0): (2)在被开启的Activi ...
- Django 你需要了解的入门操作
创建一个django project (我的版本是1.11.11) django-admin startproject mysite cd mysite 当前目录下会生成mysite的工程,目录结 ...