PAT 1013 Battle Over Cities
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 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 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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define maxnum 100005
int n,m,k;
int mp[][];
int temp[][];
int vis[] = {}; int findnotvis(){
for(int i=;i <= n;i++){
if(!vis[i]) return i;
}
return -;
} void dfs(int x){
vis[x] = ;
for(int i=;i <= n;i++){
if(!vis[i]&&mp[x][i]){
dfs(i);
}
}
} int main(){
cin >> n >> m >> k;
memset(mp,, sizeof(mp));
for(int i=;i < m;i++){
int x,y;scanf("%d%d",&x,&y);
mp[x][y] = mp[y][x] = ;
} while(k--){
int num;cin >> num;
vis[num] = ; //只要把点置为访问过的就可以了,不需要删去边
int cnt = ;
for(int i=;i <= n;i++){ //这个写法很好,学习了。
if(!vis[i]){
dfs(i);
cnt++;
}
}
cout << cnt- << endl;
memset(vis,, sizeof(vis));
} return ;
}
果然用cin输入数据会超时。。
其实这是考察图的问题,删除图的一个节点,是其他节点成为连通图,至少需要添加多少条线
添加最少的路线,就是连通分量数-1(例:n个互相独立的连通分量组成一个连通图,只需要连n-1条线就可以)
这道题最重要就是求除去图的一个节点后 剩余的连通分量的数目
利用邻接矩阵v存储路线,用visit数组表示城市是否被遍历过
对于每个被占领的城市,将其表示为遍历过的状态true即可
利用深度优先遍历dfs计算连通分量数目
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
#include<cmath>
#include<set>
#include<map>
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int N=;
int n,m,a[N*N],b[N*N],pre[N]; int f(int x){
if(pre[x]==x)return x;
pre[x]=f(pre[x]);
return pre[x];
} void link(int x,int y){
int r1=f(x),r2=f(y);
if(r1!=r2){
pre[r2]=r1;
}
}
void solve(int city){
for(int i=;i<=n;i++){
pre[i]=i;
}
for(int i=;i<m;i++){
if(a[i]==city||b[i]==city)continue;
link(a[i],b[i]);
}
int ans=;
for(int i=;i<=n;i++){ //查找堆的个数
if(i==city)continue;
int ri=f(i);
if(ri==i)ans++; //是个堆顶
} printf("%d\n",ans-); } int main(){ int k; scanf("%d%d%d",&n,&m,&k); for(int i=;i<m;i++){ scanf("%d%d",&a[i],&b[i]); } int x; for(int i=;i<=k;i++){ scanf("%d",&x); solve(x); } }
用并查集也挺好的思想
PAT 1013 Battle Over Cities的更多相关文章
- PAT 1013 Battle Over Cities(并查集)
1013. Battle Over Cities (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue It ...
- pat 1013 Battle Over Cities(25 分) (并查集)
1013 Battle Over Cities(25 分) It is vitally important to have all the cities connected by highways i ...
- PAT 1013 Battle Over Cities (dfs求连通分量)
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 DFS深搜
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
PAT甲级1013. Battle Over Cities 题意: 将所有城市连接起来的公路在战争中是非常重要的.如果一个城市被敌人占领,所有从这个城市的高速公路都是关闭的.我们必须立即知道,如果我们 ...
- 图论 - PAT甲级 1013 Battle Over Cities C++
PAT甲级 1013 Battle Over Cities C++ It is vitally important to have all the cities connected by highwa ...
- 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 (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 | 并查集
1013 Battle Over Cities (25分) It is vitally important to have all the cities connected by highways ...
随机推荐
- 《开始使用Linux》单元测验 1
C语言编写的应用程序,通过printf打印一个换行符\n,但在终端上执行的是回车加换行\r\n,把换行符替换为回车换行是由下面哪个软件模块完成的? Linux内核中的行律模块 下面哪个命令可以获得ma ...
- UVA 11019 Matrix Matcher(哈希)
题意 给定一个 \(n\times m\) 的矩阵,在给定一个 \(x\times y\) 的小矩阵,求小矩阵在大矩阵中出现的次数. \(1 \leq n,m \leq 1000\) \(1\leq ...
- Derek解读Bytom源码-Api Server接口服务
作者:Derek 简介 Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchain/bytom ...
- 【C#】非常重要的泛型
泛型 为什么要有泛型, 在没有泛型之前, 什么东西充当了泛型的作用? 在泛型出现之前, 代码中会有很多需要强制转换的地方. 比如 int a = (int) object, 对于这样类似的代码, 编译 ...
- localhost 和 127.0.0.1 有什么区别?
另外,主要是好友为什么两个一个有 favicon 一个没有? 127.0.0.1和localhost_180104074532.png
- Vs Code搭建 TypeScript 开发环境
一.npm install -g typescript 全局安装TypeScript 二.使用Vs Code打开已创建的文件夹,使用快捷键Ctrl+~启动终端输入命令 tsc --init 创建t ...
- 因样式冲突引起的div消失问题
工作需要,搭建一个网站的模型,简单分成三个部分,标题栏,导航栏,主界面,效果如图: 但是点击界面的任意地方,中间的div块消失了,如图所示: 调试,发现在点击界面其他地方的时候display属性有变化 ...
- git 命令 clone分支的代码
一个项目通常含有很多分支, master分支一般是经过测试,验证没有问题后,代码才会提交到master分支 develop分支,是测试经常拉下来进行测试的分支 直接复制develop分支的git 命令 ...
- Python 循环与定义函数
break for i in range(10): if i == 2: break print i 0 1 continue for i in range(10): if i == 2: conti ...
- [原][粒子特效][spark]深入浅出osgSpark
背景: 目前我使用的spark粒子特效库是2.0 这个库好像是原来鬼火引擎的一部分,需要从github上找 现在我要将其使用到我自己开发的基于osgearth开的三维地图引擎中 步骤: 1.编译spa ...