7-41 Red Alert (25 points)
It is very important to maintain connectivity between cities in the war.This topic requires you to write an alarm program that will give a red alarm when the loss of a city causes the country to be divided into several disconnected areas.Note: If the country is not completely connected and is divided into K regions, and the loss of one city does not change the connectivity between other cities, do not issue an alarm.
input gives two integers n (0 < n ≤ 500) and m (≤ 5000) in the first row, which are respectively the number of cities (hence the default number of cities from 0 to N-1) and the number of passages connecting the two cities.The following m lines, each line gives the number of the two cities connected by a passage, separated by a space.The captured information is given after the city information, i.e. a positive integer k and the number of the following k captured cities.
Note: The input guarantees that the captured city numbers given are legal and not duplicated, but does not guarantee that the given routes are not duplicated.
for each captured city, if it will change the connectivity of the whole country, output redalert: citykislose!Where k is the number of the city;Otherwise, only citykislose.If the country loses its last city, add a line to export to Game Over.
1 2 0 4 3
City 1 is lost.
City 2 is lost.
Red Alert: City 0 is lost!
City 4 is lost.
City 3 is lost.
Solution: Calculate the city of connected set before the war, and then calculate the current number of connected set cities for each city in Occupation and compare them.If r>count+1, it means that the connectivity of the country has been destroyed. at this time, a red alarm should be issued.At the end of each cycle, count is equal to r (updated to the current city connected set number).If the number of cities in Occupation is equal to the total number of cities in the country, then the game is over and Game Over needs to be exported.
using namespace std;
void dfs(int i)
printf("Red Alert: City %d is lost!\n",num);
printf("City %d is lost.\n",num);