PAT-ADVANCED1013——Battle Over Cities

版权声明:我的GitHub:https://github.com/617076674。真诚求星! https://blog.csdn.net/qq_41231926/article/details/83511340

我的PAT-ADVANCED代码仓:https://github.com/617076674/PAT-ADVANCED

原题链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805500414115840

题目描述:

题目翻译:

1013 城市之间的战争

在战争中,所有的城市都通过高速公路连接在一起,这一点是至关重要的。如果一个城市被敌人占领了,那么所有连接这个城市的高速公路都会被封闭。我们必须马上知道为了使得余下的城市保持连接状态,我们是否需要修建其他的高速公路。给你一张城市地图,上面标识出了所有余下的高速公路,你需要快速说出需要修建的高速公路的数量。

举个例子,如果我们有3座城市,2条高速公路分别连接city1-city2、city1-city3。如果city1被敌人占领了,我们就需要修建一条高速公路,那就是city2-city3。

输入格式:

每个输入文件包含一个测试用例。对每个测试用例,第一行包含3个数字:N(<= 1000)表示城市总数量,M表示高速公路数量,K表示需要检查的城市数量。接下来的M行,每行用2个整数描述一条高速公路,这2个整数分别代表这条高速公路所连接的两个城市的编号。城市编号从1到N。最后一行有K个数字,代表了我们关注的城市。

输出格式:

对K个城市中的每一个城市,分别在1行中输出如果该城市被敌人占领所需要修建的高速公路的数量。

输入样例:

3 2 3
1 2
1 3
1 2 3

输出样例:

1
0
0

知识点:图的深度优先遍历、并查集

思路一:图的深度优先遍历(邻接矩阵实现)

本题的实质是求除去某个点之外,图中有几个连通块。用图的深度优先遍历算法即可。

时间复杂度是O(K * N)。空间复杂度是O(N ^ 2)。

注意点:

城市编号是1 ~ N,而不是0 ~ N - 1。

C++代码:

#include<iostream>
#include<vector>

using namespace std;

int n;	//城市数量
int m;	//高速公路数量
int k;	//需要检查的城市数量
int graph[1001][1001] = {0};	//无向图
bool visited[1001]; 

void dfs(int nowVisit, int city);

int main(){
	cin >> n >> m >> k;
	int city1, city2;
	for(int i = 0; i < m; i++){
		cin >> city1 >> city2;
		graph[city1 - 1][city2 - 1] = 1;
		graph[city2 - 1][city1 - 1] = 1;
	}
	int city;
	for(int i = 0; i < k; i++){
		cin >> city;
		int count = 0;
		for(int j = 0; j < n; j++){
			visited[j] = false;
		}
		for(int j = 0; j < n; j++){
			if(j == city - 1){
				continue;
			}
			if(!visited[j]){
				dfs(j, city);
				count++;
			}
		}
		cout << count - 1 << endl;
	} 
}

void dfs(int nowVisit, int city){
	visited[nowVisit] = true;
	for(int i = 0; i < n; i++){
		if(i != city - 1 && !visited[i] && graph[i][nowVisit] != 0){
			dfs(i, city);
		}
	}
}

C++解题报告:

思路二:并查集(邻接表实现)

时间复杂度是O(kN)。空间复杂度是O(N + M)。

注意为并查集添加路径压缩操作。

C++代码:

#include<iostream>
#include<vector>

using namespace std;

int n;	//城市数量
int m;	//高速公路数量
int k;	//需要检查的城市数量
vector<int> graph[1001];	//无向图
int father[1001];	//并查集数组
bool visited[1001]; //标记数组 

int findFather(int x);
bool isConnected(int x, int y); 
void unionTwo(int x, int y);
void init();

int main(){
	cin >> n >> m >> k;
	int city1, city2;
	for(int i = 0; i < m; i++){
		cin >> city1 >> city2;
		graph[city1 - 1].push_back(city2 - 1);
		graph[city2 - 1].push_back(city1 - 1);
	}
	int city;
	for(int i = 0; i < k; i++){
		cin >> city;
		init();
		for(int j = 0; j < n; j++){
			for(int l = 0; l < graph[j].size(); l++){
				int u = j, v = graph[j][l];
				if(u == city - 1 || v == city - 1){
					continue;
				}
				unionTwo(u, v);
			}
		}
		int count = 0;
		for(int j = 0; j < n; j++){
			if(j == city - 1){
				continue;
			}
			int jFather = findFather(j);
			if(!visited[jFather]){
				count++;
				visited[jFather] = true;
			}
		}
		cout << count - 1 << endl;
	} 
	return 0; 
}

int findFather(int x){
	int a = x;
	while(x != father[x]){
		x = father[x];	
	}
	while(a != father[a]){	//路径压缩 
		int z = a;
		a = father[a];
		father[z] = x;
	}
	return x;
}

bool isConnected(int x, int y){
	int xFather = findFather(x);
	int yFather = findFather(y);
	if(xFather != yFather){
		return false;
	}
	return true;
}

void unionTwo(int x, int y){
	if(!isConnected(x, y)){
		father[father[x]] = father[y];
	}
} 

void init(){
	for(int i = 0; i < n; i++){
		father[i] = i;
		visited[i] = false; 
	}
}

C++解题报告:

思路三:图的广度优先遍历(邻接矩阵实现)(测试点4会超时)

本题的实质是求除去某个点之外,图中有几个连通块。用图的广度优先遍历算法也可以实现。

时间复杂度是O(K * N)。空间复杂度是O(N ^ 2)。

C++代码:

#include<iostream>
#include<vector>
#include<queue>

using namespace std;

int n;	//城市数量
int m;	//高速公路数量
int k;	//需要检查的城市数量
int graph[1001][1001] = {0};	//无向图
bool inq[1001];

void bfs(int nowVisit, int city);

int main() {
	cin >> n >> m >> k;
	int city1, city2;
	for(int i = 0; i < m; i++) {
		cin >> city1 >> city2;
		graph[city1 - 1][city2 - 1] = 1;
		graph[city2 - 1][city1 - 1] = 1;
	}
	int city;
	for(int i = 0; i < k; i++) {
		cin >> city;
		int count = 0;
		fill(inq, inq + 1001, false);
		for(int j = 0; j < n; j++) {
			if(j == city - 1) {
				continue;
			}
			if(!inq[j]) {
				bfs(j, city);
				count++;
			}
		}
		cout << count - 1 << endl;
	}
}

void bfs(int nowVisit, int city) {
	queue<int> q;
	q.push(nowVisit);
	while(!q.empty()) {
		int now = q.front();
		inq[now] = true;
		q.pop();
		for(int i = 0; i < n; i++) {
			if(i != city - 1 && !inq[i] && graph[i][now] != 0) {
				q.push(i);
			}
		}
	}
}

C++解题报告:

猜你喜欢

转载自blog.csdn.net/qq_41231926/article/details/83511340