zoukankan      html  css  js  c++  java
  • 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 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

    Submit01:

    #include <iostream>
    #include <vector>
    using namespace std;
    //目标:各个城市被占领,需要修复的道路对应的数量 dfs求连通分量,被敌人攻占的城市相当于被访问过
    #define maxN 1001
    bool marked[maxN];
    vector<int> graph[maxN];
    void dfs(int v) {//深度搜索
        marked[v] = true;
        for (auto lt = graph[v].cbegin();lt!=graph[v].cend();lt++) {
            if (!marked[*lt]) dfs(*lt);
        }
    }
    int main() {
        int n,m,k;
        scanf("%d %d %d",&n,&m,&k);
        int u,v;
        while(m-->0) {
            scanf("%d %d",&u,&v);
            graph[v].push_back(u);
            graph[u].push_back(v);
        }
        int t;
        for (int i=0; i<k;i++) {
            scanf("%d",&t);
            fill(marked,marked+maxN,false);
            marked[t] = true;//标记攻占的城市为已放问
            int count = 0;
            for(int v = 1;v<=n;v++) {
                if (marked[v]==false){
                    dfs(v);
                    count++;
                }
            }
            printf("%d
    ",count-1);
        }
        
        return 0;
    }

    Submit02:

    #include <iostream>
    #include <algorithm>
    using namespace std;
    //目标:n个城市m条通道,每删除一个城市即通道也删除,找到剩余城市连通的路线,重新变为连通图
    //去除某个节点之后其他的图所拥有的连通分量数
    //使用矩阵存储,每个被占领的城市,去除该节点即标记为已经访问
    int v[1010][1010];
    bool visit[1010];
    int n;
    void dfs(int node) {
        visit[node] = true;
        for(int i=1;i<=n;i++) {
            if (visit[i] == false && v[node][i] == 1) {
                dfs(i);
            }
        }
    }
    
    int main() {
        int m, k, a, b;
        scanf("%d %d %d",&n,&m,&k);
        for (int i=0;i<m;i++) {
            scanf("%d %d",&a,&b);
            v[a][b] = v[b][a] = 1;
        }
        for (int i=0;i<k;i++) {
            fill(visit,visit+1010,false);//默认visit全为false表示该城市还没有被占领,标记为未访问;每次输入重置为false
            scanf("%d",&a);
            int cnt = 0;
            visit[a] = true;//标记该城市为占领 已访问
            for (int j=1;j<=n;j++) {
                if (visit[j] == false){//对所有未访问的节点遍历,求得所有连通分量的个数
                    dfs(j);
                    cnt++;
                }
            }
            printf("%d
    ",cnt-1);//a个互相分立的连通分量变为连通图,需要a-1个路线即可相连。
        }
        return 0;
    }

    参考:

    柳婼-https://blog.csdn.net/liuchuo/article/details/54561626

    昵称五个字-https://blog.csdn.net/a617976080/article/details/89676670

  • 相关阅读:
    linux rz -e
    (转载)总结一下SQL语句中引号(')、quotedstr()、('')、format()在SQL语句中的用法
    Searching the Web论文要点
    搜索提示(search suggest)文献阅读
    C++常用数据结构(对照python)
    FM,FFM,GBDT推导
    Ranking relevance in yahoo search (2016)论文阅读
    荀子劝学篇
    不要尝试去锯木屑
    3服务器Java虚拟机配置
  • 原文地址:https://www.cnblogs.com/cgy-home/p/15166260.html
Copyright © 2011-2022 走看看