[PAT]1013. Battle Over Cities (25)@Java

本文介绍了一种利用并查集数据结构快速计算在特定城市被占领后需要修复多少条道路以保持其他城市间连接的方法。通过实例演示了如何实现算法,并提供了完整的Java代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1013. Battle Over Cities (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

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

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



package go.jacob.day830;

import java.util.Scanner;

/**
 * 1013. Battle Over Cities (25)
 * 
 * @author Jacob 运用连通集。
 *         连通集具体数据结构参考:http://blog.youkuaiyun.com/zjkc050818/article/details/77703880
 *         这里使用quick_union
 */
public class Demo2 {
	static int count;// 记录连通集个数
	private static int[] id;// 记录每个节点属于哪个集合
	private static int[] size;// 记录每个集合的节点个数,当将两个集合连通时,将节点数较少的集合加到节点数较多的集合上

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int citys = sc.nextInt(), roads = sc.nextInt(), checked = sc.nextInt();
		if(citys<3){
			System.out.println(0);
			sc.close();
			return;
		}
		
		
		// 由于节点序号是从1开始,所以初始化的时候要注意
		id = new int[citys + 1];
		size = new int[citys + 1];

		// 保存所有路径
		int[][] road = new int[roads][2];
		for (int i = 0; i < roads; i++) {
			road[i][0] = sc.nextInt();
			road[i][1] = sc.nextInt();
		}
		int[] checkCity = new int[checked];
		for (int i = 0; i < checked; i++)
			checkCity[i] = sc.nextInt();

		for (int i = 0; i < checked; i++) {
			init();
			solve(checkCity[i], road);
		}
		sc.close();
	}

	// 初始化id和size数组.每个节点属于本节点,且个数为1
	private static void init() {
		for (int i = 1; i < id.length; i++) {
			id[i] = i;
			size[i] = 1;
		}
		count = id.length-1;
	}

	private static void solve(int root, int[][] road) {
		for (int i = 0; i < road.length; i++) {
			int p = road[i][0], q = road[i][1];
			// 如果本条路径中有root节点,则跳过此次循环。
			if (p == root || q == root)
				continue;
			union(p,q);
		}
		//count()返回连通集个数,-2的原因是:
		//删除某一个节点需要减1,删除以后,如果有N个连通集,相连需要N-1条边
		System.out.println(count()-2);
	}

	/*
	 * 查找p属于的连通集
	 */
	private static int find(int p) {
		while(id[p]!=p)
			p=id[p];
		return p;
	}
	/*
	 * 将p,q所在的两个连通集相连
	 */
	private static void union(int p, int q) {
		int pRoot=find(p),qRoot=find(q);
		if(pRoot==qRoot)
			return;
		if(size[pRoot]>size[qRoot]){
			id[qRoot]=pRoot;
			size[pRoot]+=size[qRoot];
		}else{
			id[pRoot]=qRoot;
			size[qRoot]+=size[pRoot];
		}
		count--;
	}
	/*
	 * 返回连通集数
	 */
	private static int count() {
		return count;
	}

}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值