• 浙大pat甲级题目---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

    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
    一、题目大意:
      是一道图算法题,大意是去掉一个图的某一个顶点及其所关联的边,求剩下的子图构成连通图所需要最少边数
    二、题目思路:
      这道题最主要是要看出实质求的是子图的连通分支个数,因为最少添加的边的个数m就等于联通分支个数-1(很好理解,比如有两个连通分支,则需要一条边使其联通,可以将联通分支看成点去理解),所以转化成求连通分支的个数,我的想法是用dfs遍历图的每一个节点,
    每次找一个未被遍历过的点,直到所有点都被遍历过,之后联通分支数加1,这样循环往复的遍历所有节点,最后所需的边数减一即可。


    代码如下:
     
    #include <iostream>
    #include<iomanip>
    #include <string.h>
    using namespace std;
    int graph[1005][1005];
    int visit[1005];
    int m,n,k;
    int vertex1,vertex2;
    //输入节点编号
    void dfs(int v)
    {
    visit[v]=1;//首先认为该顶点已被访问过
    for(int i=1;i<=n;i++)
    {
    if(visit[i]==0&&graph[i][v]==1)//遍历每一个顶点,若发现该顶点未被访问且与当前顶点有通路,则dfs该顶点
    {
    dfs(i);
    }
    }
    }
    int main()
    {
    scanf("%d%d%d",&n,&m,&k);
    for(int i=0;i<n;i++)
    {
    for(int j=0;j<n;j++)
    {
    graph[i][j]=0;
    }
    }//初始化邻接矩阵

    for(int i=0;i<m;i++)
    {
    scanf("%d%d",&vertex1,&vertex2);
    graph[vertex1][vertex2]=1;
    graph[vertex2][vertex1]=1;//由于是无向图,所以要注意两个边都要填充
    }
    int check;
    for(int i=0;i<k;i++)
    {
    int count=0;
    scanf("%d",&check);
    memset(visit,0, sizeof(visit));//每次需要将visit置0
    visit[check]=1;//将该顶点设置为访问过
    for(int j=1;j<=n;j++)//遍历每一个节点
    {
    if(visit[j]==0)//如果该点没有被遍历过
    {
    count++;//首先联通分支数++
    dfs(j);//使用dfs遍历该节点
    }
    }
    printf("%d ",count-1);
    }
    return 0;
    }

     

  • 相关阅读:
    JNI_Android项目中调用.so动态库实现详解
    Android动态加载so文件
    Android多媒体开发(3)————使用Android NKD编译havlenapetr-FFMpeg-7c27aa2
    Android的NDK开发(5)————Android JNI层实现文件的read、write与seek操作
    Android的NDK开发(4)————JNI数据结构之JNINativeMethod
    Android的NDK开发(3)————JNI数据类型的详解
    ORACLE 实验二
    ORA-12705: Cannot access NLS data files or invalid environment specified
    内存对齐的规则以及作用
    13.怎样自学Struts2之Struts2本地化[视频]
  • 原文地址:https://www.cnblogs.com/SK1997/p/8476816.html
Copyright © 2020-2023  润新知