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 (<), 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
    题目分析:这是一道利用图的遍历的题 将要去掉的城市节点的visit置为false 然后求出最大连通分量 这个最大连通分量减一就是我们要求的值
     1 #include<iostream>
     2 #include<vector>
     3 #include<algorithm>
     4 using namespace std;
     5 int G[1000][1000];
     6 bool visit[1000] = { 0 };
     7 int N, M, K;
     8 void dfs(int v)
     9 {
    10     visit[v] = true;
    11     for (int i = 1; i <=N; i++)
    12     {
    13         if (!visit[i] && G[v][i])
    14             dfs(i);
    15     }
    16 }
    17 int main()
    18 {
    19 
    20     cin >> N >> M >> K;
    21     int v1, v2;
    22     int v;
    23     int count = 0;
    24     for (int i = 0; i < M; i++)
    25     {
    26         cin >> v1 >> v2;
    27         G[v1][v2] = G[v2][v1] = 1;
    28     }
    29     for (int i = 0; i < K; i++)
    30     {
    31         fill(visit, visit + 1000, false);
    32         count = 0;
    33         cin >> v;
    34         visit[v] = true;
    35         for (int i = 1; i <=N; i++)
    36         {
    37             if (!visit[i])
    38             {
    39                 dfs(i);
    40                 count++;
    41             }
    42         }
    43         cout << count-1<<endl;
    44     }
    45 }
    View Code
  • 相关阅读:
    MongodDB数据库安装和简单使用
    比较运算符
    Java习题
    JavaScript示例
    Java面向过程练习题7
    Java面向过程练习题6
    倒金字塔
    包含contains
    String 比较
    单词表
  • 原文地址:https://www.cnblogs.com/57one/p/11917581.html
Copyright © 2011-2022 走看看