【PAT甲级】1013 Battle Over Cities (25 分)

1013 Battle Over Cities

题目描述

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 city​1​ -city2 and city​1 -city​3. Then if city​​1 is occupied by the enemy, we must have 1 highway repaired, that is the highway city​2 -city​3
​​ .

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

思路:

本题是一个简单的图的遍历,使用DFS或者BFS都可以,题目要求去掉一个城市以后,计算出需要修建的道路的个数,我们可以转化为,去掉一个城市以后,计算剩下图中有多少个连通分量,如果有N个连通分量,则需要修建N-1条边使得连通分量之间相互连接

什么是连通分量:
无向图G的极大连通子图称为G的连通分量( Connected Component)。
简单来说就是,就是一个图中可以连通的一个最大的子图,如果这个图本身就是连通的,那么它的连通分量就是自己

本人使用DFS来计算一个图中的连通分量的个数

DFS函数:
从p点开始进行深度优先搜索,将搜索过的点标为true

void DFS(int p)
{
    
    
	visit[p] = true;
	for (int i = 1; i <= N; i++)
	{
    
    
		if (dis[p][i] == 1 && !visit[i])
			DFS(i);
	}
	return;
}

对所有的点进行DFS,如果可以进行一次DFS,则证明有一个连通分量

代码

注意使用scanf与printf可以有效降低运算时间

//1013 Battle Over Cities
#include<iostream>

using namespace std;

#define INF 0x3f3f3f3f
#define MAXN 1001

int N, M, K;
int dis[MAXN][MAXN];
bool visit[MAXN];

void input();
void process();
void DFS(int);//从一点开始进行DFS

int main()
{
    
    
	input();
	process();
	return 0;
}

void input()
{
    
    
	//cin >> N >> M >> K;
	scanf("%d %d %d", &N, &M, &K);
	int x, y;
	for (int i = 1; i <= N; i++)
		for (int j = 1; j <= N; j++)
			dis[i][j] = dis[j][i] = INF;
	for (int i = 0; i < M; i++)
	{
    
    
		//cin >> x >> y;
		scanf("%d %d", &x, &y);
		dis[x][y] = dis[y][x]= 1;
	}
	return;
}

void DFS(int p)
{
    
    
	visit[p] = true;
	for (int i = 1; i <= N; i++)
	{
    
    
		if (dis[p][i] == 1 && !visit[i])
			DFS(i);
	}
	return;
}

void process()
{
    
    
	int city, count;
	for (int i = 0; i < K; i++)
	{
    
    
		//cin >> city;
		scanf("%d", &city);
		count = 0;
		for (int j = 1; j <= N; j++)
			visit[j] = false;
		visit[city] = true;
		for (int i = 1; i <= N; i++)//对所有点进行DFS,如果能进行一次DFS,证明有一个连通图
		{
    
    
			if (!visit[i])
			{
    
    
				count++;
				DFS(i);
			}
		}
		//cout << count - 1 << endl;
		printf("%d\n", count - 1);
	}
	return;
}

git仓库:Battle Over Cities

猜你喜欢

转载自blog.csdn.net/qq_35779286/article/details/96899855
今日推荐