当前位置:网站首页>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
0
The 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;
}
边栏推荐
- Pan micro e-cology8 foreground SQL injection POC
- [database] gbase 8A MPP cluster v95 installation and uninstall
- Form form
- Numpy Foundation
- PHP和MySQL获取week值不一致的处理
- 对标注文件夹进行清洗
- Advanced mathematics | Takeshi's "classic series" daily question train of thought and summary of error prone points
- 第6天总结&数据库作业
- 220. Presence of repeating element III
- 合工大苍穹战队视觉组培训Day5——机器学习,图像识别项目
猜你喜欢
【LeetCode数据库1050】合作过至少三次的演员和导演(简单题)
Clean the label folder
Pan micro e-cology8 foreground SQL injection POC
Day06 homework -- skill question 1
《Datawhale熊猫书》出版了!
Introduction to excellent verilog/fpga open source project (30) - brute force MD5
Day06 operation -- addition, deletion, modification and query
The Child and Binary Tree-多项式开根求逆
day06 作业--增删改查
Regular expression: judge whether it conforms to USD format
随机推荐
mysql函数
[leetcode database 1050] actors and directors who have cooperated at least three times (simple question)
What are the differences in the performance of different usages such as count (*), count (primary key ID), count (field) and count (1)? That's more efficient
node的js文件引入
Study notes of automatic control principle --- stability analysis of control system
2022流动式起重机司机考试题模拟考试题库模拟考试平台操作
JDBC database connection pool (Druid Technology)
Two tips for pycharm to open multiple projects
机器学习中的概率模型
Store a group of positive and negative numbers respectively, and count the number of 0 -- assembly language implementation
Introduction to AWD attack and defense competition
力扣题DFS
数据库操作 技能6
Pytoch learning - from tensor to LR
NPM add source and switch source
本地缓存
CSDN TOP1“一个处女座的程序猿“如何通过写作成为百万粉丝博主?
Day 6 summary & database operation
redis原理和使用-安装和分布式配置
Sklearn machine learning foundation (linear regression, under fitting, over fitting, ridge regression, model loading and saving)