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 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 Input3 2 3 1 2 1 3 1 2 3Sample 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;
}
}