zoukankan      html  css  js  c++  java
  • 1013. Battle Over Cities (25)(DFS遍历)

    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

    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

    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
    题目大意:给出n个城市之间有相互连接的m条道路,当删除一个城市和其连接的道路的时候,
    问其他几个剩余的城市至少要添加多少个路线才能让它们重新变为连通图,其实就是求连通分支数
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<stdlib.h>
     4 
     5 int graph[1001][1001];
     6 int visited[1001];
     7 int n,m,k;
     8 void dfs( int a)
     9 {
    10     int i;
    11     visited[a]=1;
    12     for( i=1; i<=n; i++)
    13     {
    14         if( visited[i]==0 && graph[a][i]==1)
    15             dfs(i);
    16     }
    17 }
    18 int main()
    19 {
    20     int cnt=0,temp;
    21     int i,j;
    22     int a,b;
    23     scanf("%d%d%d",&n,&m,&k);
    24     for( i=0; i<m; i++)  //创建图
    25     {
    26         scanf("%d%d",&a,&b);
    27         graph[a][b]=graph[b][a]=1;
    28     }
    29     for( i=0; i<k ; i++)
    30     {
    31         cnt=0;
    32         memset( visited,0,sizeof(visited));  //每次都将visited全置0
    33         scanf("%d",&temp);
    34         visited[temp]=1;
    35         for( j=1; j<=n; j++)
    36         {
    37             if( visited[j]==0)
    38             {
    39                 dfs(j);
    40                 cnt++;  //连通分支数
    41             }
    42         }
    43         printf("%d
    ",cnt-1);  //需要建的高速为连通分支数减1 
    44     }
    45     return 0;
    46 }
    
    
    在这个国度中,必须不停地奔跑,才能使你保持在原地。如果想要寻求突破,就要以两倍现在速度奔跑!
  • 相关阅读:
    javaWeb总结——session
    javaWeb中servlet开发——过滤器
    参数化测试
    JUnit 4中元数据的用法
    appium常用方法
    appium查看控件的方法
    appium+java+testng+maven环境搭建
    java-appium自动化测试之DesiredCapabilities
    GitHub 标星 15.3k,Java 编程思想最新中文版(On Java 8)
    【成功上岸】2年半开发经验进入梦寐以求的京东上班,附上我的上岸经验希望帮助到大家!
  • 原文地址:https://www.cnblogs.com/yuxiaoba/p/8553461.html
Copyright © 2011-2022 走看看