zoukankan      html  css  js  c++  java
  • [PAT] 1013 Battle Over Cities (25 分)Java

    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


     1 import java.util.Arrays;
     2 import java.util.Scanner;
     3 
     4 /**
     5  * @Auther: Xingzheng Wang
     6  * @Date: 2019/2/20 16:50
     7  * @Description: pattest
     8  * @Version: 1.0
     9  */
    10 public class PAT1013 {
    11 
    12     static int[][] map = new int[1001][1001];
    13     static boolean[] visit = new boolean[1001];
    14     static int n = 0;
    15 
    16     //DFS
    17     static public void dfs(int start) {
    18         visit[start] = true;
    19         for (int i = 1; i <= n; i++) {
    20             if (visit[i] == false && map[start][i] == 1)
    21                 dfs(i);
    22         }
    23     }
    24 
    25     public static void main(String[] args) {
    26         Scanner cin = new Scanner(System.in);
    27         int i;
    28         n = cin.nextInt();
    29         int m = cin.nextInt();
    30         int k = cin.nextInt();
    31         for (i = 0; i < m; i++) {
    32             int x = cin.nextInt();
    33             int y = cin.nextInt();
    34             map[x][y] = map[y][x] = 1;
    35         }
    36         for (int s = 0; s < k; s++) {
    37             int cnt = 0;
    38             Arrays.fill(visit, false);  //填充每个visit数组都是 false;
    39             int temp = cin.nextInt();
    40             visit[temp] = true;
    41             for (i = 1; i <= n; i++) {
    42                 if (visit[i] == false) {
    43                     dfs(i);
    44                     cnt++;
    45                 }
    46             }
    47             System.out.println(cnt - 1);
    48         }
    49     }
    50 }
  • 相关阅读:
    树莓派3下开启SSH服务
    没有显示器连接树莓派
    没有屏幕和键盘如何玩转树莓派
    无显示器通过网线连接笔记本电脑玩转树莓派
    解决某些软件无法在虚拟机中运行的方法
    sql注入-基于布尔型的盲注入门
    前台数据传到后台,日期类型序列化后与前台传过来的数据不一致
    layui修改表格行高
    jQuery动态回到顶部
    禁止选中页面内容
  • 原文地址:https://www.cnblogs.com/PureJava/p/10497996.html
Copyright © 2011-2022 走看看