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 city​1​​-city​2​​ and city​1​​-city​3​​. Then if city​1​​ is occupied by the enemy, we must have 1 highway repaired, that is the highway city​2​​-city​3​​.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 3 numbers N (<), 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

题目大意:

给出n个城市,城市间有m条路,k个要检查的城市
假如这k个城市被攻占,所有相关的路线全部瘫痪,要使其他城市保持连通,至少需要修缮多少条路

思路:

1.其实这是考察图的问题,删除图的一个节点,是其他节点成为连通图,至少需要添加多少条线

2.添加最少的路线,就是连通分量数-1(例:n个互相独立的连通分量组成一个连通图,只需要连n-1条线就可以)
3.这道题最重要就是求除去图的一个节点后 剩余的连通分量的数目
4.利用邻接矩阵a存储路线,用inq数组表示城市是否被遍历过

5.遍历每一个需要考虑的城市u,去掉与u相连的边,然后用bfs数有几个连通的块(类似于几个水洼)

#include<bits/stdc++.h>
using namespace std;
int N,M,K;
int is_con[][];
vector<int>a[];
int inq[];//用于bfs的标记
queue<int>q;
void clear(queue<int>& q) {//清空队列的快捷操作
queue<int> empty;
swap(empty, q);
}
int bfs(int p)//数数有几个强连通分量
{
memset(inq,,sizeof(inq));
int ans=;
for(int i=;i<=N;i++)//遍历每个城市
{
if(i!=p && inq[i]==)//该城市没被标记过
{
ans++;
clear(q);
q.push(i);inq[i]=;
while(!q.empty())
{
int x=q.front();
q.pop();
//遍历与x相连的点放进队列并标记
for(int j=;j<a[x].size();j++)
{
int y = a[x].at(j);
if(y!=p && is_con[x][y]!= && inq[y]!=) //不是p,连通的,不在队列中
{
q.push(y);inq[y]=;//放入队列并标记
}
}
}
}
}
return ans;
}
int main()
{
cin>>N>>M>>K;
memset(is_con,,sizeof(is_con));
for(int i=;i<=N;i++)
{
a[i].clear();
}
for(int i=;i<=M;i++)
{
int u,v;
cin>>u>>v;
is_con[u][v]=is_con[v][u]=;
a[u].push_back(v);
a[v].push_back(u);
}
for(int i=;i<=K;i++)
{
int u;
cin>>u;
for(int j=;j<a[u].size();j++)//去掉边
{
int v=a[u].at(j);
is_con[u][v]=;
is_con[v][u]=;
}
int ans = bfs(u);
cout<<ans-<<endl;
for(int j=;j<a[u].size();j++)//恢复去掉的边
{
int v=a[u].at(j);
is_con[u][v]=;
is_con[v][u]=;
}
}
return ;
}

PAT 甲级 1013 Battle Over Cities (25 分)(图的遍历,统计强连通分量个数,bfs,一遍就ac啦)的更多相关文章

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

  2. PAT甲级1013. Battle Over Cities

    PAT甲级1013. Battle Over Cities 题意: 将所有城市连接起来的公路在战争中是非常重要的.如果一个城市被敌人占领,所有从这个城市的高速公路都是关闭的.我们必须立即知道,如果我们 ...

  3. 图论 - PAT甲级 1013 Battle Over Cities C++

    PAT甲级 1013 Battle Over Cities C++ It is vitally important to have all the cities connected by highwa ...

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

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

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

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

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

  7. PAT A 1013. Battle Over Cities (25)【并查集】

    https://www.patest.cn/contests/pat-a-practise/1013 思路:并查集合并 #include<set> #include<map> ...

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

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

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

随机推荐

  1. NLP学习(2)----文本分类模型

    实战:https://github.com/jiangxinyang227/NLP-Project 一.简介: 1.传统的文本分类方法:[人工特征工程+浅层分类模型] (1)文本预处理: ①(中文) ...

  2. 透过字节码生成审视Java动态代理运作机制

    对于动态代理我想应该大家都不陌生,就是可以动态去代理实现某个接口的类来干一些我们自己想要的功能,但是在字节码层面它的表现是如何的呢?既然目前刚好在研究字节码相关的东东,有必要对其从字节码角度来审视一下 ...

  3. PHP 基础知识-数组

    PHP 的数组主要分为: 索引数组 - 带有数字索引的数组 关联数组 - 带有指定键的数组 多维数组 - 包含一个或多个数组的数组   索引数组:   有两种创建索引数组的方法: 索引是自动分配的(索 ...

  4. python学习之基础入门,安装,字符串,数据转换,三元运算符

    python基础 我们要开始学习新的编程语言了,加油~~ python是“世界上最好的语言”,学习它当然是认为它是最好的所以我们才学(人生苦短我学python),python运用于不同的领域,采集分析 ...

  5. 算法设计与分析 - 李春葆 - 第二版 - pdf->word v3

    1.1 第1章─概论 练习题 . 下列关于算法的说法中正确的有( ). Ⅰ.求解某一类问题的算法是唯一的 Ⅱ.算法必须在有限步操作之后停止 Ⅲ.算法的每一步操作必须是明确的,不能有歧义或含义模糊 Ⅳ. ...

  6. .npmrc 实用小技巧

    小技巧 因为每次执行 npm adduser 的时候都需要输入用户名.密码和email 很麻烦,我们都可以配置在.npmrc 文件中,在命令行中执行如下脚本 echo -n 'myuser:mypas ...

  7. mysql group_concat长度限制

    group_concat函数有长度限制 查找当前数据库长度 show variables like 'group_concat_max_len' 设置当前session的group_concat长度, ...

  8. 第九章 利用CSS3制作网页动画

    一.CSS3变形transform 1.平移:translate(x,y) translateX(x) translateY(y) 注意:如果想只向X轴平移那么可以translateX,如果想只向X轴 ...

  9. Flutter布局4--Row

    Row 简介 mainAxisAlignment:主轴布局方式,row主轴方向是水平方向 crossAxisAlignment: 交叉轴的布局方式,对于row来说就是垂直方向的布局方式 Row 是一个 ...

  10. (转)实验文档1:跟我一步步安装部署kubernetes集群

    实验环境 基础架构 主机名 角色 ip HDSS7-11.host.com k8s代理节点1 10.4.7.11 HDSS7-12.host.com k8s代理节点2 10.4.7.12 HDSS7- ...