当前位置:网站首页>Solving graph problems with union search set
Solving graph problems with union search set
2022-06-11 05:03:00 【bolite】
6-2-3 Depth-first search Red Alert (25 branch )
It's very important to maintain connectivity between cities in the war . This question asks you to write an alarm program , When the loss of a city causes the country to be divided into multiple disconnected areas , Just a red alert . Be careful : If the country is not fully connected , It's divided k Regions , The loss of one city does not change the connectivity between other cities , Then don't alarm .
Input format :
Enter two integers in the first line N(0 < N ≤ 500) and M(≤ 5000), They are the number of cities ( So the default city is from 0 To N-1 Number ) And the number of roads connecting the two cities . And then M That's ok , Each line gives the number of the two cities connected by a road , In the meantime 1 Space separation . Give the captured information after the city information , It's a positive integer K And then K The number of the captured cities .
Be careful : Input the number of the occupied city to ensure that it is legal and no repetition , But there is no guarantee that the given path is not repeated .
Output format :
For every captured city , If it changes the connectivity of the whole country , The output Red Alert: City k is lost!, among k It's the number of the city ; Otherwise, only output City k is lost. that will do . If the country loses its last city , Then add a line of output Game Over..
sample input :
5 4
0 1
1 3
3 0
0 4
5
1 2 0 4 3
sample output :
City 1 is lost.
City 2 is lost.
Red Alert: City 0 is lost!
City 4 is lost.
City 3 is lost.
Game Over.
#include<iostream>
using namespace std;
int map[520][520];// Build a diagram //
int pre[520];// Previous node of this node
int n, m;// Number of cities and number of roads
void inis(int n);// Initialize the join lookup set
void join(int x, int y);// Building relationships
int find(int x);// Find the home of the node
int main()
{
cin >> n>>m;
int i = 0;
int a1, a2;// Two cities
inis(n);// Initialize and query set
for (i = 0; i < m; i++) {
cin >> a1 >> a2;
map[a1][a2] = map[a2][a1] = 1;// Construct a phase free diagram
join(a1, a2);// Build the relationship between the two cities
}
int cout1 = 0, cout2 = 0;//cout1 Record the number of city groups last time ,cout2 Record the number of current city groups
for (i = 0; i < m; i++) {
if (pre[i] == i) cout1++;// If this condition is met, it is a city group
}
int del;// Delete the number of cities
cin >> del;
int data;// Deleted city
for (int j = 0; j < del; j++) {
cin >> data;
cout2 = 0;
inis(n);// And search set initialization
for (i = 0; i < n; i++) {
map[data][i] = map[i][data] = 0;// The relationship between the city to be deleted and other cities shall be abolished
}
for (i = 0; i < n; i++) {
for (int k=0; k < n; k++) {
if (map[i][k]) join(i, k);// Rebuild the associated union lookup set
}
}
for (i = 0; i < n; i++) {
if (pre[i] == i) cout2++;// Find out the number of new city groups
}
if (cout2 <= cout1 + 1) printf("City %d is lost.\n", data);
else printf("Red Alert: City %d is lost!\n", data);
cout1 = cout2;
}
if (del == n) cout << "Game Over.";// If the number of deleted is the same as the number of cities , The game is over
}
void inis(int n)
{
int i = 0;
for (i = 0; i < n; i++) {
pre[i] = i;
}
}
void join(int x, int y)
{
int fx = find(x);
int fy = find(y);
if (fy != fx) {
pre[fx] = fy;
}
}
int find(int x)
{
if (pre[x] == x) return x;
else return pre[x] = find(pre[x]);// Path compression
}
边栏推荐
- 高斯白噪声(white Gaussian noise,WGN)
- Paper recommendation: relicv2, can the new self supervised learning surpass supervised learning on RESNET?
- 力扣(LeetCode)161. 相隔为 1 的编辑距离(2022.06.10)
- Dongmingzhu said that "Gree mobile phones are no worse than apple". Where is the confidence?
- Wechat applet uploads the data obtained from database 1 to database 2
- Yolov5 training personal data set summary
- Huawei equipment is configured with bgp/mpls IP virtual private network
- Codesys get System Time
- BP neural network derivation + Example
- Inventory | ICLR 2022 migration learning, visual transformer article summary
猜你喜欢

博途仿真时出现“没有针对此地址组态任何硬件,无法进行修改”解决办法

Using keras to build the basic model yingtailing flower

Pychart displays pictures with imshow

codesys 獲取系統時間

Differences between the four MQ

华为设备配置BGP/MPLS IP 虚拟专用网

oh my zsh正确安装姿势

【入门级基础】Node基础知识总结

Electrolytic solution for ThinkPad X1 carbon battery

Cartographer learning record: cartographer Map 3D visualization configuration (self recording dataset version)
随机推荐
Win10+manjaro dual system installation
6 questions to ask when selecting a digital asset custodian
Crmeb/v4.4 Standard Version open version mall source code applet official account h5+app mall source code
Google drive download failed, network error
Pytoch machine learning GPU usage (conversion from CPU to GPU)
Differences between the four MQ
华为设备配置BGP/MPLS IP 虚拟专用网命令
Huawei equipment is configured to access the virtual private network through GRE
华为设备配置本地虚拟专用网互访
Technical dry goods: how to select the most suitable RDMA network card
tensorflow1. X and tensorflow2 Conversion of X
[Transformer]Is it Time to Replace CNNs with Transformers for Medical Images?
Preliminary test of running vins-fusion with zed2 binocular camera
BP neural network derivation + Example
Cascade EF gan: local focus progressive facial expression editing
Thesis 𞓜 jointly pre training transformers on unpaired images and text
课程设计总结
华为设备配置通过GRE接入虚拟专用网
华为设备配置跨域虚拟专用网
Paper reproduction: pare