PAT 1013 Battle Over Cities

1013 Battle Over Cities(25分)

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 connectingcity​1​​-city​2​​andcity​1​​-city​3​​. Then ifcity​1​​is occupied by the enemy, we must have 1 highway repaired, that is the highwaycity​2​​-city​3​​.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 3 numbersN(<1000),MandK, which are the total number of cities, the number of remaining highways, and the number of cities to be checked, respectively. ThenMlines follow, each describes a highway by 2 integers, which are the numbers of the cities the highway connects. The cities are numbered from 1 toN. Finally there is a line containingKnumbers, which represent the cities we concern.

Output Specification:

For each of theKcities, 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

思路

  • 本题的题意是删除图中一点(包括该点相连的边)后,最少需要添加多少条边才能够使图连通。
  • 可以想象删除一个点后,图的剩余部分由若干连通分量构成,问题便转化为求两桶分量的个数,最后讲过连通分量个数减去1就是需要添加的边数。

代码

图的bfs遍历

  • 注意:该图是无向图,读入数据时两个方向的边都要读入。
  • fill(inq,inq + n + 1, false)函数:下标从1开始,第二个参数要多加上1。
#include <cstdio> 
#include <vector>
#include <queue>
#include <cstring>

#include <algorithm>

using namespace std;

const int maxn = 1010;

vector<int> Adj[maxn];
bool inq[maxn];

int n, m, k;

void bfs(int u)
{
	queue<int> q;
	q.push(u);
	inq[u] = true;
	while (!q.empty())
	{
		int now = q.front();
		q.pop();
		for (int i = 0; i < Adj[now].size(); i++)
		{
			int v = Adj[now][i];
			if (inq[v] == false)
			{
				inq[v] = true;
				q.push(v);
			}
		}
	}
}

int  bfsTrave()
{
	int sum = -1;
	for (int u = 1; u <= n; u++)
	{
		if (inq[u] == false)
		{
			bfs(u);
			sum++;
		}
	}
	return sum;
}

int main()
{
	//freopen("test.txt", "r",stdin);
	scanf("%d %d %d", &n, &m, &k);

	int id1, id2;
	for (int i = 1; i <= m; i++)
	{
		scanf("%d %d", &id1, &id2);
		Adj[id1].push_back(id2);
		Adj[id2].push_back(id1);
	}

	for (int i = 1; i <= k; i++)
	{
		int search;
		fill(inq, inq + n + 1, false);
                //memset(inq, false, sizeof inq);
		scanf("%d", &search);
		inq[search] = true;
		printf("%d
", bfsTrave());
	}
	return 0;
}

图的dfs遍历

#include <cstdio>
#include <vector>
#include <cstring>

using namespace std;

const int maxn = 1010;
vector<int> Adj[maxn];
bool vis[maxn];
int n, m, k;

int search;
void dfs(int u)
{
	if (u == search) return;
	vis[u] = true;
	for (int i = 0; i < Adj[u].size(); i++)
	{
		int v = Adj[u][i];
		if (vis[v] == false)
		{
			dfs(v);
		}
	}
}

int main()
{
	//freopen("test.txt", "r", stdin);
	scanf("%d %d %d", &n, &m, &k);
	int id1, id2;
	for (int i = 1; i <= m; i++)
	{
		scanf("%d %d", &id1, &id2);
		Adj[id1].push_back(id2);
		Adj[id2].push_back(id1);
	}

	for (int i = 1; i <= k; i++)
	{
		scanf("%d", &search);
		fill(vis, vis + n + 1, false);
		int sum = -1;
		for (int v = 1; v <= n; v++)
		{
			if (vis[v] == false && v != search)
			{
				dfs(v);
				sum++;
			}
		}
		printf("%d
", sum);
	}
	return 0;
}

使用并查集

  • 注意点:首先筛选掉需要删除的结点,把依附于同一条边的两个顶点进行合并。在统计连通分量的个数时,未被访问的父节点代表一个连通分量。
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

const int maxn = 1010;

vector<int> Adj[maxn];
bool vis[maxn];
int n, m, k;

int father[maxn];

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

void Union(int a, int b)
{
	int faA = findFather(a);
	int faB = findFather(b);
	if (faA != faB)
	{
		father[faA] = faB;
	}
}

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

int main()
{
	//freopen("test.txt", "r", stdin);
	scanf("%d %d %d", &n, &m, &k);
	int id1, id2;

	for (int i = 1; i <= m; i++)
	{
		scanf("%d %d", &id1, &id2);
		Adj[id1].push_back(id2);
		Adj[id2].push_back(id1);
	}

	int search;
	for (int i = 1; i <= k; i++)
	{
		scanf("%d", &search);
		father[search] = 0;
		init();
		for (int i = 1; i <= n; i++)
		{
			for (int j = 0; j < Adj[i].size(); j++)
			{
				int u = i, v = Adj[i][j];
				if (u == search || v == search) continue;
				Union(u, v);
			}
		}
		int sum = -1;
		for (int i = 1; i <= n; i++)
		{
			if (i == search)	continue;
			int fa = findFather(i);
			if (vis[fa] == false)
			{
				sum++;
				vis[fa] = true;
			}
		}
		printf("%d
", sum);
	}
	return 0;
}

原文地址:https://www.cnblogs.com/adios/p/12584592.html