zoukankan      html  css  js  c++  java
  • PAT 1013 Battle Over Cities (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 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
    

    思路

    给定一个图后,封锁一个点,若要使得剩下的点联通,求添加道路的最少数量。

    用flag数组标记一个点是否访问过,用dfs可以求出封锁某点后,该图的联通分量数block(未连通的块)。答案就是block-1

    最后一组数据会超时,可以用个map记录下封锁某点的答案,可以重复使用。

    此题还可以继续优化时间复杂度,但是PAT应该可以直接过了。

    代码

    #include <stdio.h>
    #include <string>
    #include <stdlib.h>
    #include <iostream>
    #include <vector>
    #include <string.h>
    #include <algorithm>
    #include <cmath>
    #include <map>
    #include <limits.h> 
    using namespace std;
    int maze[1000+10][1000+10];
    int N, M, K;
    int block = 0;
    bool flag[1000+10];
    void dfs(int index){
    	flag[index] = 1;
    	for(int i = 1; i <= N; i++){
    		if(maze[i][index] && !flag[i])	dfs(i);
    	}
    }
    map<int, int> mmp;
    int main() {
    	cin >> N >> M >> K;
    	int e, s;
    	for(int i = 0; i < M; i++){
    		cin >> e >> s;
    		maze[e][s] = 1;
    		maze[s][e] = 1;
    	}
    	for(int i = 0; i < K; i++){
    		cin >> e;
    		block = 0;
    		if(mmp.count(e)){
    			cout << mmp[e] << endl;
    			continue;
    		}
    		memset(flag, 0, sizeof(flag));
    		flag[e] = 1;
    		for(int j = 1; j <= N; j++){
    			if(!flag[j]){
    				block++;
    				dfs(j);
    			}
    		}
    		mmp[e] = block - 1;
    		cout << block - 1 << endl;
    	}
    	return 0; 
    }
    
  • 相关阅读:
    Vim基本功
    八个最常用的正则表达式
    程序员总结:帮助你早些明白一些道理
    HttpClient
    Red.Hat.Enterprise.Linux.6.2下安装vim 、OpenOffice、JDK、Eclipse
    输出打印某个对象所有属性及属性值
    Notepad++
    写博客?
    解决操作WordPress时提示输入FTP信息
    JS通过键盘点击事件实现div移动
  • 原文地址:https://www.cnblogs.com/woxiaosade/p/12347733.html
Copyright © 2011-2022 走看看