PAT1013删除顶点后的连通分量的个数

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 cit**y1-cit**y2 and cit**y1-cit**y3. Then if cit**y1 is occupied by the enemy, we must have 1 highway repaired, that is the highway cit**y2-cit**y3.

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

题目大意:

给定一幅无向无权图,问我们删除一个顶点和它相连的边后,连通分量是多少。题目本身不是这么说的,题目讲了一个故事,说的是,当一个城市被占领后,所有与这个城市连通的道路都要被关闭。问剩下的城市如果要连通,需要新添加多少条路。 翻译成图论问题就是前面说道的。 如果剩下的顶点和边构成的图 的连通分量是 2 那么只需要一条路就可以把整个图变得连通。也就是连通分量-1 。

思路分析:

求解连通分量一个比较简单的写法就是 深度优先遍历。一次深度优先遍历的完成 就会得到一个连通分量。 对整幅图进行深度优先遍历, 就会求出图的连通分量的个数。

要不要真的删除题目中要求的顶点呢?其实是不用的,当我们在深度优先遍历中 访问到这个顶点时,直接返回,就相当于这个顶点在图中不存在。 有同学可能会疑惑,我们在if (!used[k] && k!=checkNum)已经判断了 K!=checkNum ,为什么在 DFS的递归终止条件中又出现了 if (vertex == checkNum ) ,这是因为虽然这里可以避免我们把 checkNum 这个已经摧毁的城市作为访问起点,但是,我们依然可以通过其他顶点的间接访问到这里,(因为我们不是真的删除了这个顶点)

如果你觉得这样比较麻烦,那么更进一步,用两个数组。一个存放原始信息,也就是还没有删除过顶点的二维矩阵,一个用作每次删除一个顶点和相应边的矩阵。 比如我们这次是尝试摧毁城市3 ,那么我么就可以把原始矩阵拷贝过来,把第3行的所有值改为0 把第3列的所有元素改为0. 当然这样做是比较耗时的。

由于Java的性能原因 1个测试点超时未通过。如果您能做进一步的优化,请和大家分享!
如果您觉得哪里表述不对,或者不够清晰,欢迎和我讨论!

完整代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

//3 2 3
//1 2
//1 3
//1 2 3
public class P1013 {
     
    static int verNum, edgeNum, qurNum, checkNum;// 顶点个数、边条数、删除的顶点的个数、删除哪个顶点
    static boolean[] used; // 访问标记数组
    static int[][] graph; // 图 ,使用邻接矩阵存储

    public static void main(String[] args) throws IOException {
     
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        String[] info = bf.readLine().split(" ");
        verNum = Integer.parseInt(info[0]);
        edgeNum = Integer.parseInt(info[1]);
        qurNum = Integer.parseInt(info[2]);

        graph = new int[verNum + 1][verNum + 1]; //因为顶点编号从1开始,为了方便,我们也从1开始

        for (int i = 1; i <= edgeNum; i++) {
      // 读取边,完成图的存储
            String[] edgeInfo = bf.readLine().split(" ");
            int ver1 = Integer.parseInt(edgeInfo[0]);
            int ver2 = Integer.parseInt(edgeInfo[1]);
            graph[ver1][ver2] = 1;
            graph[ver2][ver1] = 1;
        }

        int res = 0;
        String[] qurInfo = bf.readLine().split(" "); // 
        for (int j = 0; j < qurNum; j++) {
     
            checkNum = Integer.parseInt(qurInfo[j]);// 删除哪些顶点
            used = new boolean[verNum + 1];//顶点编号1-N 每一轮都重新初始化为false

            for (int k = 1; k <= verNum; k++) {
     
               if (!used[k] && k!=checkNum) {
      // 如果顶点k没有被访问并且不是当前删除的顶点 才能进入遍历
                    DFS(k);
                    res++;
                }
            }

            System.out.println(res - 1);
            res = 0; // 每一求出一个答案,res 连通分量要归零

        }
    }
    // DFS 遍历
    static void DFS(int vertex) {
     
        if (vertex == checkNum) {
     
            return;
        }
        used[vertex] = true;

        for (int i = 1; i <= verNum; i++) {
     
            if (graph[vertex][i] == 1 &&  !used[i]) {
     
                DFS(i);
            }
        }
    }
}

你可能感兴趣的:(PAT)