当前位置:网站首页>Pat grade a a1013 battle over cities
Pat grade a a1013 battle over cities
2022-07-26 09:03:00 【Zhemu】
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
0The question :
3 vertices ,2 Two sides , Delete 3 Nodes ( Delete one at a time , All operations are in the original drawing , Deletion does not affect each other )
1,2 Have edge ,1,3 Have edge , 1 2 3 Is the deleted node
Delete a node in the graph , Find out how many lines are needed for reconnection in the graph , In fact, it is to calculate a certain vertex , The number of connected blocks , Because after deleting a vertex , Adding a line to two connected blocks can make the graph reconnect .
Ideas :
There is no need to delete vertices , Just skip it when you encounter it , Then find the number of connected blocks in the graph , The line to connect = = Number of connected blocks - 1;
Code :
#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);// Number of nodes , The number of sides and the number of nodes to delete ;
for(int i = 0; i < m; i++) {// Drawing :
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;
}边栏推荐
- SQL入门——组合表
- ES6模块化导入导出)(实现页面嵌套)
- 机器学习中的概率模型
- unity简易消息机制
- Simple message mechanism of unity
- Advanced mathematics | Takeshi's "classic series" daily question train of thought and summary of error prone points
- Regular expression: judge whether it conforms to USD format
- 堆外内存的使用
- Learning notes of automatic control principle --- linear discrete system
- 220. Presence of repeating element III
猜你喜欢
随机推荐
Grain College of all learning source code
合工大苍穹战队视觉组培训Day5——机器学习,图像识别项目
数据库操作 题目二
ONTAP 9文件系统的限制
Day06 operation -- addition, deletion, modification and query
“No input file specified “问题的处理
JS - DataTables 关于每页显示数的控制
力扣链表题
Typescript encryption tool passwordencoder
PAT 甲级 A1076 Forwards on Weibo
220. Presence of repeating element III
[eslint] Failed to load parser ‘@typescript-eslint/parser‘ declared in ‘package. json » eslint-confi
Ueditot_ JSP SSRF vulnerability recurrence
Database operation topic 2
idea快捷键 alt实现整列操作
Overview of motion recognition evaluation
Introduction to AWD attack and defense competition
Cve-2021-3156 duplicate of sudo heap overflow privilege raising vulnerability
Center an element horizontally and vertically
Day06 homework - skill question 7








