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求连通块的更多相关文章

  1. 1013 Battle Over Cities (25分) DFS | 并查集

    1013 Battle Over Cities (25分)   It is vitally important to have all the cities connected by highways ...

  2. 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 ...

  3. 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 ...

  4. 【PAT甲级】1013 Battle Over Cities (25 分)(并查集,简单联通图)

    题意: 输入三个整数N,M,K(N<=1000,第四个数据1e5<=M<=1e6).有1~N个城市,M条高速公路,K次询问,每次询问输入一个被敌军占领的城市,所有和该城市相连的高速公 ...

  5. 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 ...

  6. 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 ...

  7. PAT 解题报告 1013. Battle Over Cities (25)

    1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...

  8. PAT 1013 Battle Over Cities(并查集)

    1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...

  9. DFS入门之二---DFS求连通块

    用DFS求连通块也是比较典型的问题, 求多维数组连通块的过程也称为--“种子填充”. 我们给每次遍历过的连通块加上编号, 这样就可以避免一个格子访问多次.比较典型的问题是”八连块问题“.即任意两格子所 ...

随机推荐

  1. java基础类型源码解析之String

    差点忘了最常用的String类型,我们对String的大多数方法都已经很熟了,这里就挑几个平时不会直接接触的点来解析一下. 先来看看它的成员变量 public final class String { ...

  2. MIPS 指令集(共31条)

    MIPS 指令集(共31条) MIPS 指令集(共31条) 助记符 指令格式 示例 示例含义 操作及其解释 Bit # 31..26 25..21 20..16 15..11 10..6 5..0 R ...

  3. ISO/IEC 9899:2011 条款6.4.8——预处理数字

    6.4.8 预处理数字 语法 1.pp-number: digit .    digit pp-number    digit pp-number    identifier-nondigit pp- ...

  4. python基础之:九步认识装饰器

    step1. 先看个代码吧: def f(): ') f=lambda a:a +100 #覆盖上面的函数f print(f) #函数名指函数所在内存中的位置,入带后面括号表示执行函数 print(f ...

  5. orcale11g安装

    一.centos7.5安装orcale 安装环境 内存最小1G,推荐2G或者更高 内存为1-2g,swap是内存的1.5倍左右 内存大于2G, swap和内存相等 硬盘最小为30G oracle版本 ...

  6. Java中四个作用域的可见范围

    作用域 当前类 同一包 子孙类 其他包 public √ √ √ √ protected √ √ √ × friendly或default(即没有修饰符时的权限) √ √ × × private √ ...

  7. win7下安装IIS7

    在Windows 7下如何安装IIS7,以及IIS7在安装过程中的一些需要注意的设置,以及在IIS7下配置ASP的正确方法. 在Windows 7下面IIS7的安装方法: 一.进入Windows 7的 ...

  8. C#压缩打包文件

    该控件是使用csharp写的,因此可以直接在dotnet环境中引用,不需要注册. 利用 SharpZipLib方便地压缩和解压缩文件最新版本的SharpZipLib(0.84)增加了很多新的功能,其中 ...

  9. 使用ASP.NET Core支持GraphQL( restful 配套)

    https://github.com/graphql-dotnet https://github.com/graphql GraphQL简介 官网:https://graphql.cn/code/ 下 ...

  10. Docker 持久存储介绍(十三)

    目录 一.Docker 数据存储 二.Bind mount 1.详细介绍 2.如何使用 -v or --volume 语法 --mount 语法 两者区别 3.使用场景 4.使用案例 存在目录 bin ...