zoukankan      html  css  js  c++  java
  • pat甲级1013

    1013 Battle Over Cities (25)(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 city~1~-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

    Each input file contains one test case. Each case starts with a line containing 3 numbers N (&lt1000), 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

    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
    需要修的路个数就是连通分量的个数减一。
     1 #include <iostream>
     2 #include <vector>
     3 using namespace std;
     4 
     5 int N, M, K;
     6 int G[1001][1001];
     7 
     8 int connectCnt(int city);
     9 void dfs(int s, bool marked[]);
    10 
    11 int main()
    12 {
    13     int i, j, v, w, c;
    14     cin >> N >> M >> K;
    15     for (i = 0; i < M; i++)
    16     {
    17         cin >> v >> w;
    18         G[v][w] = 1;
    19         G[w][v] = 1;
    20     }
    21     for (i = 0; i < K; i++)
    22     {
    23         cin >> c;
    24         vector<int> vec;
    25         for (j = 1; j <= N; j++)
    26         {
    27             if (G[c][j] == 1)
    28             {
    29                 G[c][j] = 0;
    30                 G[j][c] = 0;
    31                 vec.push_back(j);
    32             }
    33         }
    34         cout << connectCnt(c) << endl;
    35         for (int j : vec)
    36         {
    37             G[c][j] = 1;
    38             G[j][c] = 1;
    39         }
    40     }
    41     return 0;
    42 }
    43 
    44 int connectCnt(int city)
    45 {
    46     int v, cnt = 0;
    47     bool marked[1001] = {};
    48     for (v = 1; v <= N; v++)
    49     {
    50         if (!marked[v] && v != city)
    51         {
    52             dfs(v, marked);
    53             cnt++;
    54         }
    55     }
    56     return cnt - 1;
    57 }
    58 
    59 void dfs(int s, bool marked[])
    60 {
    61     marked[s] = true;
    62     for (int v = 1; v <= N; v++)
    63     {
    64         if (!marked[v] && G[s][v])
    65             dfs(v, marked);
    66     }
    67 }

    但是查看别人的博客发现不需要每次把和被占领的城市相连的道路标记为0再标记为1,只需要把marked数组被占领的城市标记为1即可。。

     1 #include <iostream>
     2 using namespace std;
     3 
     4 int N, M, K;
     5 int G[1001][1001];
     6 
     7 int connectCnt(int city);
     8 void dfs(int s, bool marked[]);
     9 
    10 int main()
    11 {
    12     int i, v, w, c;
    13     scanf("%d%d%d", &N, &M, &K);
    14     for (i = 0; i < M; i++)
    15     {
    16         scanf("%d%d", &v, &w);
    17         G[v][w] = 1;
    18         G[w][v] = 1;
    19     }
    20     for (i = 0; i < K; i++)
    21     {
    22         scanf("%d", &c);
    23         printf("%d
    ", connectCnt(c));
    24     }
    25     return 0;
    26 }
    27 
    28 int connectCnt(int city)
    29 {
    30     int v, cnt = 0;
    31     bool marked[1001] = {};
    32     marked[city] = true;
    33     for (v = 1; v <= N; v++)
    34     {
    35         if (!marked[v])
    36         {
    37             dfs(v, marked);
    38             cnt++;
    39         }
    40     }
    41     return cnt - 1;
    42 }
    43 
    44 void dfs(int s, bool marked[])
    45 {
    46     marked[s] = true;
    47     for (int v = 1; v <= N; v++)
    48     {
    49         if (!marked[v] && G[s][v])
    50             dfs(v, marked);
    51     }
    52 }
     
  • 相关阅读:
    数论数论函数基础知识
    KMP入门和简单运用
    高斯消元入门
    FFT和NTT
    AC自动机入门和简单应用
    后缀自动机入门
    线段树优化建图的速成
    爱情九十七课,降低期待
    爱情九十二课,说出你的弱
    爱情八十六课,等得不是爱情
  • 原文地址:https://www.cnblogs.com/lxc1910/p/9476388.html
Copyright © 2011-2022 走看看