PAT 甲级 A1013 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 city1​-city2​ and city1​-city3​. Then if city1​ is occupied by the enemy, we must have 1 highway repaired, that is the highway city2​-city3​.

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

题意:

3个顶点,2个边长,删除3个结点(每次删一个,都是在原图内操作,删除互不影响)

 1,2 有边,1,3有边, 1 2 3 是删除的结点


在图中删除某个结点,求图内重新连通需要多少根线,其实就是求不计某个顶点,连通块的个数,因为在删除某顶点后,两两连通块内加一根线就能使得图重新连通了。

思路:

没必要真的把顶点删除,遇到就跳过就好了,这样再去找图内连通块的个数,连通起来要用的线 = = 连通块个数 - 1;

代码:

#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
int  n, m, k, deleteNode;
const int maxn = 1010;
vector<int > Adj[maxn];
bool vis[maxn] = {false}; 

void DFS(int v, int deleteNode) {
	if(v  == deleteNode) return ;
	for(int i = 0; i < Adj[v].size(); i++) {
		int next = Adj[v][i];
		if(vis[next] == false) {
			vis[next] = true;
			DFS(next,deleteNode);
		}
	}
}

int main () {
	scanf("%d %d %d", &n, &m ,&k);//结点个数,边数和要删除的结点个数;
	for(int i = 0; i < m; i++) {//建图: 
		int v1, v2;
		scanf("%d %d", &v1, &v2);
		Adj[v1].push_back(v2);
		Adj[v2].push_back(v1);		 
	}
	for(int query = 0; query < k; query++) {
		scanf("%d", &deleteNode);
		memset(vis, false, sizeof(vis));
		int num = 0;
		for(int i = 1; i <= n; i++) {
			if(i != deleteNode && vis[i] == false) {
				DFS(i, deleteNode);
				num++;
			}
		}
		printf("%d\n", num - 1);
	}
	return 0;
}

这道题也可以用并查集来求解

思路:遍历所有结点,将所有有关系的结点都放在一个集合里面(就是连通块了),最后再遍历所有结点,看看有多少个集合(连通块)就好了,连通这些集合所需要的边数就是集合数 - 1。

代码:
 

//并查集找图内连通块(集合)的个数 ;
#include<iostream>
#include<vector>
using namespace std;
const int maxn = 1010;
int father[maxn] = {0}; //存储结点所在集合的信息; 
int vis[maxn] = {false};//标记连通块; 
vector<int > node[maxn];//记录最初的结点关系; 
//并查集的三件套; 
int findRoot(int x) {
	if(x == father[x]) return x;//一输入就是根节点和遍历到根节点。 
	else {
		//压缩路径; 
		int root = findRoot(father[x]);
			father[x] = root;
			return root;//回溯,每个结点都指向root;	 
	}
}

void Union (int a, int b) {
	int fa = findRoot(a);
	int fb = findRoot(b);
	if(fa != fb) {
		father[fb] = fa;//fb的集合指向fa;
		return ; 
	}
}

void init(int n) {
	for(int i = 1; i <= n; i++) {
		father[i] = i;//初始化,自己指向自己,自己就是一个集合:
		vis[i] = false; 
	}
}
int n, m, k, deleteNode;
int main () {
	scanf("%d %d %d", &n, &m, &k);
	for(int i = 0; i < m; i++) {
		int a, b;
		scanf("%d %d", &a, &b);
		node[a].push_back(b);
		node[b].push_back(a);
	}//建图;
	for(int query = 0; query < k; query++) {
		scanf("%d",&deleteNode);
		init(n);//初始化;
		int block = 0; 
		for(int i = 1; i <=n ; i++) {//遍历所有结点; 
			for(int j = 0; j < node[i].size(); j++) {//遍历所有子结点,有关系的放到一个集合里面; 
				int u = i, v= node[i][j];
				if(u != deleteNode && v != deleteNode) {
					Union(u, v);//有关系的都放在一个集合(连通块); 
				}
			}	
		}
		for(int i = 1; i <= n; i++) {
			if(i == deleteNode) continue;
			int root = findRoot(i);
			if(vis[root] == false) {
				block++;
				vis[root] = true;
			}
		}
		printf("%d\n", block - 1); 
	} 
	return 0;
} 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值