PAT 甲级 1013 Battle Over Cities (25 分)(图的遍历,统计强连通分量个数,bfs,一遍就ac啦)
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
题目大意:
给出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啦)的更多相关文章
- 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 ...
- 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 ...
- 1013 Battle Over Cities (25分) DFS | 并查集
1013 Battle Over Cities (25分) It is vitally important to have all the cities connected by highways ...
- 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 ...
- 【PAT甲级】1013 Battle Over Cities (25 分)(并查集,简单联通图)
题意: 输入三个整数N,M,K(N<=1000,第四个数据1e5<=M<=1e6).有1~N个城市,M条高速公路,K次询问,每次询问输入一个被敌军占领的城市,所有和该城市相连的高速公 ...
- PAT A 1013. Battle Over Cities (25)【并查集】
https://www.patest.cn/contests/pat-a-practise/1013 思路:并查集合并 #include<set> #include<map> ...
- 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 ...
- PAT 解题报告 1013. Battle Over Cities (25)
1013. Battle Over Cities (25) t is vitally important to have all the cities connected by highways in ...
随机推荐
- SQL Server CET 通用表表达式 之 精典递归
SQL2005 Common Table Expressions(CET)即通用表表达式. SQLSERVER CET递归使用案例: 1.普通案例 表结构如下: ;WITH cet_depart ...
- SmtpClient 发送邮件
利用SmtpClient 代码发送邮件. 简单测试代码: static void Main(string[] args) { MailMessage msg = new MailMessage(); ...
- spring lookup method 注入
lookup method注入是spring动态改变bean里方法的实现.方法执行返回的对象,使用spring内原有的这类对象替换,通过改变方法返回值来动态改变方法.内部实现为使用cgl ...
- spring-mybatis+spring整合
整合之前回忆一下spring和mybatis分别做了什么: spring 通过注解/xml配置,实现AOP和DI DI: 接口实现类中, 将接口私有化,从容器中读取,而不是new一个 UserDao ...
- 将CHROME WEBSTORE里的APPS和扩展下载到本地 转载自:http://ro-oo.diandian.com/post/2011-05-28/1103036
Chrome Webstore 自动改版后就不能再直接下载到本地... 下载地址: https://clients2.google.com/service/update2/crx?response=r ...
- 前端知识体系:JavaScript基础-作用域和闭包-词法作用域和动态作用域
词法作用域和动态作用域 1.作用域: 作用域是指程序代码中定义变量的区域 JavaScript采用词法作用域,也就是静态作用域 2.词法作用域和动态作用域 因为JavaScript采用的是词法作用域, ...
- exception about Kernel Panic // dirperm1 breaks the protection by the permission bits on the lower branch
问题描述: 1. K8S集群有一个worker,经常磁盘满,然后导致服务异常. 2. 查看/var/log/syslog, 发现非常多的异常如下: 1568405.455565] docker0: p ...
- 一个简单易上手的短信服务Spring Boot Starter
前言 短信服务在用户注册.登录.找回密码等相关操作中,可以让用户使用更加便捷,越来越多的公司都采用短信验证的方式让用户进行操作,从而提高用户的实用性. Spring Boot Starter 由于 S ...
- Educational Codeforces Round 74 (Rated for Div. 2) A. Prime Subtraction
链接: https://codeforces.com/contest/1238/problem/A 题意: You are given two integers x and y (it is guar ...
- 连续攻击游戏【P1640洛谷】二分图匹配变形【好题】【每次memset太慢了,用时间戳id。】
lxhgww最近迷上了一款游戏,在游戏里,他拥有很多的装备,每种装备都有2个属性,这些属性的值用[1,10000]之间的数表示.当他使用某种装备时,他只能使用该装备的某一个属性.并且每种装备最多只能使 ...