C++ 代码:战争中的城市连通性报警程序
#include
vector<vector
void bfs(int start) {
queue
int main() {
int N, M;
cin >> N >> M;
graph.resize(N);
visited.resize(N, false);
for (int i = 0; i < M; i++) {
int city1, city2;
cin >> city1 >> city2;
graph[city1].push_back(city2);
graph[city2].push_back(city1);
}
int K;
cin >> K;
vector
for (int i = 0; i < K; i++) {
int lostCity = lostCities[i];
visited.assign(N, false);
bfs(lostCity);
bool isConnected = true;
for (int j = 0; j < N; j++) {
if (!visited[j]) {
isConnected = false;
break;
}
}
if (isConnected) {
cout << 'City ' << lostCity << ' is lost.' << endl;
} else {
cout << 'Red Alert: City ' << lostCity << ' is lost!' << endl;
}
}
if (K == N) {
cout << 'Game Over.' << endl;
}
return 0;
}
原文地址: https://www.cveoy.top/t/topic/o5BM 著作权归作者所有。请勿转载和采集!