当前位置:网站首页>PAT 甲级 A1013 Battle Over Cities
PAT 甲级 A1013 Battle Over Cities
2022-07-26 08:50:00 【柘木木】
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 (<1000), 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题意:
3个顶点,2个边长,删除3个结点(每次删一个,都是在原图内操作,删除互不影响)
1,2 有边,1,3有边, 1 2 3 是删除的结点
在图中删除某个结点,求图内重新连通需要多少根线,其实就是求不计某个顶点,连通块的个数,因为在删除某顶点后,两两连通块内加一根线就能使得图重新连通了。
思路:
没必要真的把顶点删除,遇到就跳过就好了,这样再去找图内连通块的个数,连通起来要用的线 = = 连通块个数 - 1;
代码:
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
int n, m, k, deleteNode;
const int maxn = 1010;
vector<int > Adj[maxn];
bool vis[maxn] = {false};
void DFS(int v, int deleteNode) {
if(v == deleteNode) return ;
for(int i = 0; i < Adj[v].size(); i++) {
int next = Adj[v][i];
if(vis[next] == false) {
vis[next] = true;
DFS(next,deleteNode);
}
}
}
int main () {
scanf("%d %d %d", &n, &m ,&k);//结点个数,边数和要删除的结点个数;
for(int i = 0; i < m; i++) {//建图:
int v1, v2;
scanf("%d %d", &v1, &v2);
Adj[v1].push_back(v2);
Adj[v2].push_back(v1);
}
for(int query = 0; query < k; query++) {
scanf("%d", &deleteNode);
memset(vis, false, sizeof(vis));
int num = 0;
for(int i = 1; i <= n; i++) {
if(i != deleteNode && vis[i] == false) {
DFS(i, deleteNode);
num++;
}
}
printf("%d\n", num - 1);
}
return 0;
}边栏推荐
- Day06 operation -- addition, deletion, modification and query
- 【搜索专题】看完必会的搜索问题之洪水覆盖
- Day06 homework - skill question 7
- Set of pl/sql
- Day06 homework -- skill question 1
- 合工大苍穹战队视觉组培训Day5——机器学习,图像识别项目
- Day06 homework -- skill question 2
- Neo eco technology monthly | help developers play smart contracts
- Database operation skills 7
- [database] gbase 8A MPP cluster v95 installation and uninstall
猜你喜欢

Set of pl/sql -2

Form form

Vision Group Training Day5 - machine learning, image recognition project

day06 作业--增删改查

Review notes of Microcomputer Principles -- zoufengxing

【LeetCode数据库1050】合作过至少三次的演员和导演(简单题)
![[encryption weekly] has the encryption market recovered? The cold winter still hasn't thawed out. Take stock of the major events that occurred in the encryption market last week](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[encryption weekly] has the encryption market recovered? The cold winter still hasn't thawed out. Take stock of the major events that occurred in the encryption market last week

TCP solves the problem of short write

CSDN TOP1“一个处女座的程序猿“如何通过写作成为百万粉丝博主?

Database operation skills 7
随机推荐
【LeetCode数据库1050】合作过至少三次的演员和导演(简单题)
Pytoch realizes logistic regression
OA项目之我的会议(会议排座&送审)
PHP page value transfer
CSDN TOP1“一个处女座的程序猿“如何通过写作成为百万粉丝博主?
[search topics] flood coverage of search questions after reading the inevitable meeting
Mutual transformation of array structure and tree structure
Node-v download and application, ES6 module import and export
MySQL 8.0 OCP 1z0-908 certification examination question bank 1
Which financial product has the highest yield in 2022?
TypeScript版Snowflake主键生成器
uni-app 简易商城制作
Media at home and abroad publicize that we should strictly grasp the content
Oracle 19C OCP 1z0-082 certification examination question bank (51-60)
Spark SQL common date functions
node的js文件引入
Numpy Foundation
MySQL 8.0 OCP (1z0-908) has a Chinese exam
Logic of data warehouse zipper table
Form form