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 ...
随机推荐
- Codeforces 781C Underground Lab
题目链接:http://codeforces.com/problemset/problem/781/C 因为有${K}$个机器人,每个又可以走${\left \lceil \frac{2n}{k} \ ...
- python,函数的基本用法
一.函数 函数的概念:对功能或者动作的封装可以帮我们把一段公共的代码提取出来 语法如下 def 函数名(形参): 函数体 函数名(实参) # 函数名() def yue(): print(" ...
- Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3:compile
完整的错误信息: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.3:compile ( ...
- C++.可变参数_ZC测试
ZC:环境: Win7 x64(旗舰版),Microsoft Visual Studio 2010(版本 10.0.30319.1 RTMRel, Microsoft .NET Framework(版 ...
- Centos 7系统挂载NTFS格式移动硬盘
有些时候做大数据量迁移时,为了快速迁移大数据,有可能在Linux服务器上临时挂载NTFS格式的移动硬盘, 一般情况下,linux是识别不了NTFS格式移动硬盘的(需要重编译Linux核心才能,加挂NT ...
- QTableWidget自定义表头QHeaderView加全选复选框
1 QTableWidget自定义表头QHeaderView加全选复选框 在使用QTableWidget时需要在表头添加全选复选框,但是默认的表头无法添加复选框,只能用图片画上去一个复 ...
- 小程序歌词展示,格式lrc歌词
代码: wxml: <view class="page"> <view class="lrc" style="margin-top: ...
- 动态规划-独特的子字符串存在于Wraparound String总个数 Unique Substrings in Wraparound String
2018-09-01 22:50:59 问题描述: 问题求解: 如果单纯的遍历判断,那么如何去重保证unique是一个很困难的事情,事实上最初我就困在了这个点上. 后来发现是一个动态规划的问题,可以将 ...
- RabbitMQ消息发布时的权衡
在进行本篇文章的学习之前,你需要先阅读 https://www.cnblogs.com/duanjt/p/10057330.html.以便对Java访问RabbitMQ的基础用法有所了解. 一.失败通 ...
- Axure 第一次交互 实现跳转页面