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 ...
随机推荐
- Java二进制指令
转自: http://www.blogjava.net/DLevin/archive/2011/09/13/358497.html 指令从0x00-0xc9 没有0xba 常量入栈指令 指令码 操作码 ...
- 非托管C++互操作
.NET简谈互操作(一:开篇介绍) .NET简谈互操作(二:先睹为快) .NET简谈互操作(三:基础知识之DllImport特性) .NET简谈互操作(四:基础知识之Dispose非托管内存) .NE ...
- insert into table (a,b,c) select
本文为博主原创,转载请注明出处: 在项目中,需要统计数据,从基础表中的数据进行统计,并插入到汇总 表中, (1)语句形式为:Insert into Table2(field1,field2,...) ...
- Termux 一款安卓终端模拟器
Termux官网 https://termux.com/ 用处 提供了一个类似于Linux终端的界面,可以使用apt安装程序.目前我在上面跑了我以前写的一些爬虫脚本,运行完全没有问题. 玩法 玩法还是 ...
- Selenium 页面自动化测试 面试 问题汇总
1. 专业技术 在学习完Selenium的大部分接口或者方法之后,你可能会去面试自动化测试,主要是Selenium的自动化测试.下面这些问题总结,可能会对你有所帮助. 什么是Selenium? S ...
- [转][c++][跨平台]c++跨平台开发小结
转自:https://blog.csdn.net/dj0379/article/details/53577135 linux编程与windows编程的差异之处: 1. 文件与目录的大小写以及路径分隔符 ...
- HDU 1241 连通块问题(DFS入门题)
Input The input file contains one or more grids. Each grid begins with a line containing m and n, th ...
- Linux学习3-yum安装java和Tomcat环境
前言 linux上安装软件,可以用yum非常方便,不需要下载解压,一个指令就能用yum安装java和tomcat环境. 前面一篇已经实现在阿里云服务器上搭建一个禅道系统的网站,算是小有成就,但并不是每 ...
- C#数组维数及不同维数中元素个数的获取
简单理解有关数组维数的概念: 1.编程中用到的多维的数组,最多也就是二维数组了 2.数组的维数从0开始计算 using System; using System.Collections.Generic ...
- Python 编程快速上手 第十一章 Web scrapping
前言 这一章讲了如何在 Web 上抓取相关的信息,工具是三个模块: webbrowser 模块:用于打开浏览器指定页面 requests 模块:用于下载文件 Beautiful Soup 模块:用于解 ...