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
#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的更多相关文章

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

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

  2. pat 1013 Battle Over Cities(25 分) (并查集)

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

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

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

  5. PAT甲级1013. Battle Over Cities

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

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

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

  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 (25 分)(图的遍历,统计强连通分量个数,bfs,一遍就ac啦)

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

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

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

随机推荐

  1. eclipse创建maven web项目工程步骤示例

    参考链接:https://www.cnblogs.com/noteless/p/5213075.html 需求表均同springmvc案例 此处只是使用maven 注意,以下所有需要建立在你的ecli ...

  2. UVA12558 埃及分数

    #include<iostream> #include<cstdio> #include<set> #include<memory.h> using n ...

  3. Codeforces 85 D. Sum of Medians

    题目链接:http://codeforces.com/contest/85/problem/D 做法果然男默女泪啊..... 大概就是直接开了一个$vector$每次插入删除都用自带的$insert$ ...

  4. crontab 定时执行python脚本

    每天8点30分运行命令/tmp/run.sh * * * /tmp/run.sh 每两小时运行命令/tmp/run.sh */ * * * /tmp/run.sh

  5. loglog()函数

    数据: xd = [1, 2, 3] yd = [0.6, 0.2, 0.2] matlab中双对数函数: 命令: loglog(xd, yd, 'blacko-', 'MarkerFaceColor ...

  6. JDK7+EclipseIDE+Tomcat7.0.55++mybatis3+Maven3.2.2 构建webapp 的java 的maven项目

    构建Maven项目 工具 JDK7+EclipseIDE+Tomcat7.0.55++mybatis3+Maven3.2.2 JDK 下载地址 http://www.oracle.com/techne ...

  7. 从RGB扫描图到数字化等高线矢量图

    1 用arcgis 对地形图进行校正,主要是通过判断地图的坐标系统,然后将图层的坐标系统设置正确.选择图上的经纬网交点,对原图进行校正,一般要求四角和均匀布点. 2 二值化图像.如果是RGB图像,即彩 ...

  8. Java操作Kafka

    java操作kafka非常的简单,然后kafka也提供了很多缺省值,一般情况下我们不需要修改太多的参数就能使用.下面我贴出代码. pom.xml <dependency> <grou ...

  9. python中的进程和线程

    什么是进程(process)? 程序并不能单独运行,只有将程序装载到内存中,系统为它分配资源才能运行,而这种执行的程序就称之为进程.程序和进程的区别就在于:程序是指令的集合,它是进程运行的静态描述文本 ...

  10. jenkins之从0到1利用Git和Ant插件打war包并自动部署到tomcat(第五话):总结以及build.xml文件

    前面基本上把整个配置过程都完整地串起来了,包括可能遇到的难点,按照那个套路应该可以配置好自动打包发布的功能.简单总结下我的学习过程,以及遇到问题是怎样解决的. 准备一个项目源码 刚开始在github和 ...