PAT-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 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 cit**y1-cit**y2 and cit**y1-cit**y3. Then if cit**y1 is occupied by the enemy, we must have 1 highway repaired, that is the highway cit**y2-cit**y3.
Input Specification:
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 Specification:
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
题目大意 给一个图,查询删掉某一个点之后还有几个连通块
思路分析 第五个例子太迷了,写邻接表和前向星都有段错误,不知道前向星是不是开小了,这题点只有1000个,所以还是用最传统的矩阵存储比较好,第五个测试样例接近完全图了都.这种点比较少的图很容易出这种恶心的样例
#include<bits/stdc++.h>
#define de(x) cout<<#x<<" "<<(x)<<endl
#define each(a,b,c) for(int a=b;a<=c;a++)
using namespace std;
const int maxn=1000+5;
const int maxm=1e6+5;
const int inf=0x3f3f3f3f;
//vector<int>G[maxn];
/*
int head[maxn];
struct edge
{
int v;
int next;
}edge[maxm];
int cnt;
void addEdge(int u,int v)
{
edge[cnt].v=v;
edge[cnt].next=head[u];
head[u]=cnt++;
}*/
int a[maxn][maxn];
///段错误?????
/*
3 2 3
1 2
1 3
1 2 3
*/
int ban=0;
bool vis[maxn];
void dfs(int x,int n)
{
vis[x]=true;
for(int i=1;i<=n;i++)
{
if(!vis[i]&&a[x][i]==1)
{
dfs(i,n);
}
}
return;
}
int main()
{
int n,m,q;
cin>>n>>m>>q;
//memset(head,0,sizeof(head));
//cnt=1;
memset(a,0,sizeof(a));
while(m--)
{
int aa,b;
cin>>aa>>b;
a[aa][b]=1;
a[b][aa]=1;
//G[a].push_back(b);
//G[b].push_back(a);///别写vector和map了
}
while(q--)
{
int query;
cin>>query;
memset(vis,0,sizeof(vis));
vis[query]=true;
int cnt=0;
for(int i=1;i<=n;i++)
{
if(vis[i])continue;
dfs(i,n);
cnt++;
}
printf("%d\n",cnt-1);
}
}
PAT-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 ...
- 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 ...
- 【PAT甲级】1013 Battle Over Cities (25 分)(并查集,简单联通图)
题意: 输入三个整数N,M,K(N<=1000,第四个数据1e5<=M<=1e6).有1~N个城市,M条高速公路,K次询问,每次询问输入一个被敌军占领的城市,所有和该城市相连的高速公 ...
- 1013. Battle Over Cities (25)(DFS遍历)
For example, if we have 3 cities and 2 highways connecting city1-city2 and city1-city3. Then if city ...
- 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 ...
- 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 1013 Battle Over Cities(并查集)
1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...
- DFS入门之二---DFS求连通块
用DFS求连通块也是比较典型的问题, 求多维数组连通块的过程也称为--“种子填充”. 我们给每次遍历过的连通块加上编号, 这样就可以避免一个格子访问多次.比较典型的问题是”八连块问题“.即任意两格子所 ...
随机推荐
- IO之Socket网络编程
一.Socket Socket不是Java中独有的概念,而是一个语言无关标准.任何可以实现网络编程的编程语言都有Socket. 1,Socket概念 网络上的两个程序通过一个双向的通信连接实现数据的交 ...
- 网站url路径优化方法完全讲解 (url优化、基于tp5、API接口开发)
url优化可是网站开发的必备高阶技能,先看本实例优化前后效果比较: (同为调用前台模块下的index控制器下的index方法) 优化前:www.tp5.com/tp5/public/index.php ...
- 常用学习&面试资源网站
https://github.com/MZCretin/RollToolsApi 开源通用API https://github.com/SenhLinsh/Android-Hot-Libraries ...
- PS 实用技巧
1. 调整大小 ctrl + T 2. 导出透明背景图片 选择 png格式
- 阶段5 3.微服务项目【学成在线】_day07 课程管理实战_05-课程修改实战分析
3 课程信息修改 3.1 需求分析 课程添加成功进入课程管理页面,通过课程管理页面修改课程的基本信息.编辑课程图片.编辑课程营销信息等. 本小节实现修改课程. 3.2 课程管理页面说明 3.2.1 页 ...
- starUML建模C++【逆向工程】
1.下载starUML 2.打开starUML,选择default approach 3.添加 Profile,把C++添加进去 4.在右侧的工程上点右键—[C++]—-[Reverse Engine ...
- vue.js 同级组件之间的值传递方法(uni-app通用)
vue.js 兄弟组件之间的值传递方法 https://blog.csdn.net/jingtian678/article/details/81634149
- InfluxDB+Grafana大数据监控系列之数据源配置(二)
一.Grafana 配置 InfluxDB 数据源 1.1 登录 Granfana 界面选择 InfluxDB 数据源 在前面我们已经部署好相应监控环境,登录Grafana:http://10.223 ...
- 点击链接,取得href的值,但是不转向
点击链接,取得href的值,但是不转向 $('.list a').click(function (e) { e.preventDefault();//取消事件的默认动作. $.ajax({ ...
- iOS技术面试08:其他
1 客户端安全性处理方式? 1> 网络数据传输(敏感数据[账号\密码\消费数据\银行卡账号], 不能明文发送) 2> 协议的问题(自定义协议, 游戏代练) 3> 本地文件存储(游戏的 ...