1013 Battle Over Cities (25分) 图的连通分量+DFS

题目

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

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

题目解读

有n个城市,给出了其中几个城市之间存在路径(直接或间接),也就是连通,现在假设某个城市沦陷,和它有关联的路径全部断开,问需要修几条路才能保证剩下的城市全连通。相当于连通的城市组成一个集合,可能有好几个这样的集合,那么n个集合需要n-1条路就能全部连通。也就是说,沦陷的城市,单独作为一个集合,再去找出剩下的这些连通的集合,假如有n个,返回n-1即可。

给你一个无向图,给定某个节点,把这个节点直接关联的边全断开,然后去求连通分量的个数,最后返回连通分量的个数减1。

思路解释

一般对于求连通分量的问题,都是并查集或者DFS,我这里采用DFS,因为DFS是真的简单。

  • 用一个visit数组记录节点的访问状况,初始化全部为false
  • dfs(int i)内,完成把和i直接关联或间接关联的节点都标记为true(邻接节点继续递归找到的就是间接相关联的),这样,一次dfs就相当于一个连通分量从整个节点集中排除出去了,我们只需要统计dfs执行了多少次才使得visit数组全为false,就能得到连通分量的个数。
  • 对于这个题,加入沦陷的城市是lost,本来应该把和它关联的划分到一起,但是因为它沦陷了,需要断开全部路径,我们只需要把visit[lost]单独设置为false再去划分别的,就能达到将他排除在外的效果。

满分代码

注释很详细,大家自己看吧,啦啦啦。。。

#include <iostream>
#include <algorithm>
using namespace std;
/**
 * 有n个城市,给出了其中几个城市直接存在路(直接或间接),也就是连通,
 * 现在假设某个城市沦陷,和它有关联的路径全部断开,问需要修几条路才能保证剩下的城市全连通
 *
 * 相当于连通的城市组成一个集合,可能有好几个这样的集合,那么n个集合需要n-1条路就能全部连通
 *
 * 也就是说,沦陷的城市,单独作为一个集合,再去找出剩下的这些连通的集合,假如有n个,返回n-1即可
 *
 */
// 邻接矩阵,节点编号 1-1000
bool graph[1001][1001];
// 用是否访问来划分连通的集合
bool visit[1001];

// 有几个城市
int g_nodes;

// 每一次dfs,都把和这个城市连通的所有城市标记为以访问,这样就相当于划分出一个集合,统计这个函数执行了几次就能知道划分出了几个集合
void dfs(int node) {
    // 标记自己
    visit[node] = true;
    // 找到和他连通的,且没有标记过的,进行标记,划分为一个连通集合
    for (int j = 1; j <= g_nodes; ++j) {
        if (!visit[j] && graph[node][j])
            // 注意这里是深度优先遍历,不是直接visit[j]=true,要找到全部直接或间接连连通的
            dfs(j);
    }
}

int main() {

    int edges, k;
    cin >> g_nodes >> edges >> k;
    int s, e;
    while (edges-- > 0) {
        cin >> s >> e;
        // 无向图
        graph[s][e] = graph[e][s] = true;
    }
    // k种假设
    int lost;
    while (k-- > 0) {
        cin >> lost;
        // 重新初始化viste数组
        fill(visit, visit + g_nodes + 1, false);
        // 统计连通分量有几个
        int cnt = 0;
        // 沦陷的城市单独作为一个集合
        visit[lost] = true;
        // 统计剩下的连通分量有几个
        for (int j = 1; j <= g_nodes; ++j) {
            // 它所在的连通分量还未被划分并统计
            if (!visit[j]) {
                dfs(j);
                // 每一次dfs都会划分出一个连通分量
                cnt++;
            }
        }
        // n集合,需要n-1个边
        cout << cnt - 1 << endl;
    }
    return 0;
}